【发布时间】: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