【问题标题】:netsh not working when run via Invoke-Command通过 Invoke-Command 运行时 netsh 不工作
【发布时间】:2018-04-26 11:07:33
【问题描述】:

当我在服务器上本地运行以下命令时,它工作得很好。

$NetshArgumentList = 'advfirewall firewall add rule name="Zabbix Agent" dir=in action=allow program="C:\zabbix\bin\win64\zabbix_agentd.exe" enable=yes'
Start-Process -FilePath 'netsh' -ArgumentList $NetshArgumentList

但是当我尝试像这样远程运行它时,它就不起作用了。

$ComputerName = 'Remote-Host'
$NetshArgumentList = 'advfirewall firewall add rule name="Zabbix Agent" dir=in action=allow program="C:\zabbix\bin\win64\zabbix_agentd.exe" enable=yes'
Invoke-Command -ComputerName $ComputerName -ScriptBlock {Start-Process -FilePath 'netsh' -ArgumentList $using:NetshArgumentList}

关于为什么会这样以及如何解决它的任何建议?

【问题讨论】:

  • 您收到了哪些消息/错误?
  • 我没有收到任何错误。它似乎只是执行命令。如何从该命令中获取返回值?我现在也尝试使用-Authentication CredSSP,但没有运气。

标签: powershell remoting invoke-command


【解决方案1】:

$NetshArgumentList 在远程会话中不可见。您可以要求 PowerShell 使用 using: scope modifier 复制它:

$ComputerName = 'Remote-Host'
    $NetshArgumentList = 'advfirewall firewall add rule name="Zabbix Agent" dir=in action=allow program="C:\zabbix\bin\win64\zabbix_agentd.exe" enable=yes'
    Invoke-Command -ComputerName $ComputerName -ScriptBlock {Start-Process -FilePath 'netsh' -ArgumentList $using:NetshArgumentList}

【讨论】:

  • 对不起,我确实使用using: 运行它,只是在我的问题中复制粘贴错误
【解决方案2】:

我终于明白了。使用 Invoke-Expression 现在可以工作了。这是工作代码:

$ComputerName = 'Remote-Host'
$NetshArgumentList = 'advfirewall firewall add rule name="Zabbix Agent" dir=in action=allow program="C:\zabbix\bin\win64\zabbix_agentd.exe" enable=yes'
Invoke-Command -ComputerName $ComputerName -ScriptBlock {Invoke-Expression "netsh $using:NetshArgumentList"}

【讨论】:

    猜你喜欢
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多