【问题标题】:Powershell remotely execute commandsPowershell远程执行命令
【发布时间】:2021-05-10 06:43:57
【问题描述】:

我在尝试远程执行命令时遇到问题。首先,该命令试图将可执行文件复制到远程系统,我认为这是我的问题所在,因为您无法直接访问 \hostname\C$\Windows\Temp,您必须先连接到 C$,然后再转到 C :\Windows\Temp

话虽如此,我也尝试过 ($Dest = "C$"),但仍然无法正常工作

仅供参考:该文件夹可能存在也可能不存在于客户端中


$SetupFolder = "C$\Windows\Temp\Logs"

$Path = "C:\Windows\Temp\Logs\Install.exe"

$Dest = "C$"

# Remote run the install for each system

foreach ($System in $SystemList) {
    if (test-Connection -Cn $System -quiet) { 
        Copy-item $Package -Destination \\$System\$SetupFolder -recurse -Force
        if (Test-Path - Path $Path) {
            Invoke-Command -ComputerName $System -ScriptBlock {powershell.exe $Path /S} -credential $Credentials

            Write-Host -ForegroundColor Green "Installation Successful on $System"
        }

    } else {
    Write-Host -ForegroundColor Red "$System is not online, Install failed"
    }

}

【问题讨论】:

  • 尝试使用cmd /c $Path
  • 是的,您应该能够简单地在远程主机上调用命令,而无需先连接到管理共享。如果您对此感到担心,还可以针对 pssession 调用命令
  • 对不起,我有点糊涂了,在哪里使用 cmd c $Path?
  • 抱歉,在您执行Invoke-Command -ComputerName $System -ScriptBlock {cmd /c "$Path"} -credential $Credentials

标签: powershell automation scripting remote-access


【解决方案1】:

如果您不能使用Invoke-Command,您可以使用Enter-PSSession,这将启动与远程计算机的交互会话,您需要先获得远程计算机上的权限才能连接,

PowerShell 文档中的更多信息, https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/enter-pssession?view=powershell-7.1

【讨论】:

    猜你喜欢
    • 2021-12-22
    • 1970-01-01
    • 2021-07-28
    • 2012-01-16
    • 2010-10-01
    • 2011-07-06
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多