use*_*678 6 powershell powershell-remoting
我运行以下命令时遇到问题
$x = "c:\Scripts\Log3.ps1"
$remoteMachineName = "172.16.61.51"
Invoke-Command -ComputerName $remoteMachineName -ScriptBlock {& $x}
The expression after '&' in a pipeline element produced an invalid object. It must result in a command name, script
block or CommandInfo object.
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : BadExpression
+ PSComputerName : 172.16.61.51
Run Code Online (Sandbox Code Playgroud)
如果我不使用$x变量,则不会出现问题
Invoke-Command -ComputerName $remoteMachineName -ScriptBlock {& 'c:\scripts\log3.ps1'}
Directory: C:\scripts
Mode LastWriteTime Length Name PSComputerName
---- ------------- ------ ---- --------------
-a--- 7/25/2013 9:45 PM 0 new_file2.txt 172.16.61.51
Run Code Online (Sandbox Code Playgroud)
小智 9
PowerShell会话中的变量不会传输到使用的会话 Invoke-Command
您需要使用该-ArgumentList参数来发送您的命令变量,然后使用该$args数组在脚本块中访问它们,这样您的命令将如下所示:
Invoke-Command -ComputerName $remoteMachineName -ScriptBlock {& $args[0]} -ArgumentList $x
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6950 次 |
| 最近记录: |