使用Invoke-Command远程安装代理

时间:2017-05-03 11:23:25

标签: powershell

我有一个需要安装代理的服务器列表。对于远程安装,我从客户端计算机运行以下脚本。

客户端计算机使用New-PSSession创建与远程计算机的连接,并且脚本运行时没有任何错误,但安装永远不会发生。

$csvFilePath = "C:\Demo\Agent-Installation-Servers.csv"
$aLoginId = "abc@d.com"
$aPassword = "123456"

$urlToDownloadTheAgent = "https://someexample.com"

Import-Csv $csvFilePath | ForEach-Object {
    $hostName = $_.Host
    $loginId = $_.LoginId
    $password = $_.Password

    $securePassword = ConvertTo-SecureString $password -AsPlainText -Force
    $loginCredentials = New-Object System.Management.Automation.PSCredential ($loginId, $securePassword)

    $sessionOption = New-PsSessionOption –SkipCACheck -SkipCNCheck
    $sessionObject = New-PSSession -ComputerName $hostName -Credential $loginCredentials -UseSSL -SessionOption $sessionOption 

    Invoke-Command -Session $sessionObject -ScriptBlock {
        $agentDownloadDirectory = "C:\NewAgent"
        md -Force $agentDownloadDirectory | Out-Null
        Invoke-WebRequest -Uri $urlToDownloadTheAgent -OutFile "C:\NewAgent\win.exe"
        & C:\NewAgent\win.exe --no-prompt -u $aLoginId -p $aPassword
    }
}

还试过替换

  & C:\NewAgent\win.exe --no-prompt -u $aLoginId -p $aPassword

$arguments = "--no-prompt -u $aLoginId -p $aPassword"
start-process "C:\NewAgent\win.exe"  -ArgumentList $arguments

0 个答案:

没有答案