微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Powershell 远程执行命令

如何解决Powershell 远程执行命令

我在尝试远程执行命令时遇到问题。首先,该命令试图将可执行文件复制到远程系统,我认为这就是我的问题所在,因为您无法直接访问 \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"
    }

}

解决方法

如果您不能使用 form_class #this one excluded 'company',rank_form # this one includes 'company'. ,您可以使用 Enter-PSSession,这将启动与远程计算机的交互会话,您需要先拥有远程计算机的权限才能连接,

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。