I have some problem passing the PowerShell variables to Invoke-Command
(running CMD /C
). The problem is, that the $script_location
parameter is empty.
My script creates a batch file on an UNC path with an unique name (20190423T1152100840Z.bat) with some commands in it. At the end, I would like to run this generated batch file on a server.
$datum = Get-Date -Format FileDateTimeUniversal
$script_location = "\\uncpath\batchfile_$datum.bat"
$Username = 'user'
$Password = 'pass'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
Add-Content $script_location " preferences_manager -u=user -p=pass -target=$env:username"
Invoke-Command -ComputerName "SERVERNAME" -Credential $cred -ScriptBlock {
Invoke-Expression -Command:"cmd /c $script_location"
}
The expected result should be that the Invoke-Command
runs the $script_location
batch file.
Currently the result is that that the $script_location
variable is empty.
Any clue on how to do that?