尝试使用powershell脚本在多台计算机上安装puppet代理

时间:2018-02-18 17:06:24

标签: powershell powershell-v3.0 powershell-remoting

当我在服务器本地运行时,效果很好:

   [Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}; $webClient = New-Object System.Net.WebClient; $webClient.DownloadFile('https://bod-T2.com:8140/packages/current/install.ps1', 'install.ps1'); .\install.ps1 extension_requests:pp_role=utility extension_requests:pp_environment=e1 agent:noop=true

但是当我试图大量运行它时,我创建了这个脚本,你可以找到以下但不起作用。你能帮帮我吗?

    $InputServerList=Import-Csv -Delimiter ";" .\ServerList.txt
    $pw = convertto-securestring 'PASSWORD' -AsPlainText -Force
    $mycreds = new-object -typename 
    System.Management.Automation.PSCredential -argumentlist 
    "domain\myuser",$pw

    $ErrorActionPreference = "Stop"
    $InputServerList | ForEach {
if ($session_id=new-pssession -computername $_.IPAddress -credential $mycreds)
{
    #If we reached here it is because credentials were OK on the first attempt so we now silently continue on errors
    $ErrorActionPreference = "SilentlyContinue"
    Set-ExecutionPolicy -Scope Process -ExecutionPolicy AllSigned
    [Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}; $webClient = New-Object System.Net.WebClient; $webClient.DownloadFile('https://bod-T2.com:8140/packages/current/install.ps1', 'install.ps1'); .\install.ps1 extension_requests:pp_role=utility extension_requests:pp_environment=e1 agent:noop=true
    write-host "$($_.ServerName) was configured"  -BackgroundColor 00 -ForegroundColor 10
    Remove-PSSession $Session_id
} else {
    write-host "$($_.ServerName) $($_.IPaddress) Unable to connect"  -BackgroundColor RED -ForegroundColor Yellow
}
} 

所以基本上我需要在多个服务器上运行它:

 [Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}; $webClient = New-Object System.Net.WebClient; $webClient.DownloadFile('https://bod-T2.com:8140/packages/current/install.ps1', 'install.ps1'); .\install.ps1 extension_requests:pp_role=utility extension_requests:pp_environment=e1 agent:noop=true

1 个答案:

答案 0 :(得分:0)

也许我错过了某些内容,但您似乎并未真正使用 $session_id,请查看invoke-command

您需要执行以下操作:

$MySB = {
    [Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
    $webClient = New-Object System.Net.WebClient
    $webClient.DownloadFile('https://bod-T2.com:8140/packages/current/install.ps1', 'install.ps1')
    .\install.ps1 extension_requests:pp_role=utility extension_requests:pp_environment=e1 agent:noop=true
}

invoke-command -Session $session_id -ScriptBlock $MySB