我正在尝试在远程计算机上运行任何想要的命令。例如:gpupdate / force或将file1复制到file2等...所以我有这个代码:
$ComputerName = Read-Host "Enter a remote computer name"
$RemoteCommand = Read-Host "Enter a remote command to run: Example gpupdate /force"
$s = New-PSSession -ComputerName $ComputerName
Invoke-Command -Session $s -ScriptBlock {$RemoteCommand}
Invoke-Command -Session $s -ScriptBlock { $? }
它运行没有错误,实际上它返回TRUE。但是我在c:\ temp中拥有的文件永远不会复制到c:\ temp \ tmp
为什么不呢?
尝试像这样运行脚本:
Invoke-Command -Session $s -ScriptBlock { powershell.exe -Command "$RemoteCommand"}
如果您在转义字符时遇到问题,还可以使用-encodedCommand开关。从powershell帮助中:
# To use the -EncodedCommand parameter:
$command = 'dir "c:\program files" '
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
powershell.exe -encodedCommand $encodedCommand