升级到PS3,System.Net.WebClient无法在某些机器上运行

时间:2012-11-09 19:36:28

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

我在实验室环境中运行,需要自动化大约50台机器。我试图从服务器恢复.xml无线网络配置文件,然后安装它。此命令从1台服务器发送到50台客户端。

我最近重新构建了我的一些客户并从PS2升级到PS3,现在我的下载脚本不再工作了。

它在我的PS2工作站上运行正常。我假设它可能是一个许可的东西,但我不确定。 ThrustedHosts设置为*,脚本执行策略设置为Unrestricted。

这是一个片段和错误:

function InstallProfile(){

clear

$fonction = 
@'
param($profileName)
$File = "c:\profiles\profile.xml"
$webclient = New-Object System.Net.WebClient
$webclient.Proxy = $NULL
$ftp = "ftp://anonymous:anonymous@192.168.2.200/profiles/$profileName"
$uri = New-Object System.Uri($ftp)
Write-Host (hostname)
$webclient.DownloadFile($uri, $File)
write-host (hostname) (netsh wlan add profile filename="c:\profiles\profile.xml")
'@

$profileName = Read-Host "Enter the profile name(XML file must be present in c:\share\profiles\)"

ExecCmd -fonction $fonction -argument $profileName

func_done
}

function ExecCmd
{
param(
$fonction, 
$argument
)

$PingTest = RetrieveStatus
$results = @{}
$results = $PingTest.up
$results | sort -uniq | out-Null

$fonctionSB = ConvertTo-ScriptBlock($fonction)

    foreach($result in $results)
    {
        $os = "Windows"
            try{
                $session = New-PSSession -ComputerName $result.address -Credential $credentials -EA stop
                }
            catch{
            $os = "Not Windows"
            }
        if($os -eq "Windows"){
        Invoke-Command $result.address -ScriptBlock $fonctionSB -Arg $argument -Credential $credentials
        Get-PSSession | Remove-PSSession
        }
        else{
        Write-Host $result.address "does not support Powershell commands"
        }
    }

}

错误:

  

使用“2”参数调用“DownloadFile”的异常:“WebClient请求期间发生异常。”       + CategoryInfo:NotSpecified:(:) [],MethodInvocationException       + FullyQualifiedErrorId:WebException       + PSComputerName:192.168.2.110

1 个答案:

答案 0 :(得分:0)

我找到了解决方法,

我为webclient.Download添加了一个try / catch,适用于PS2。在catch部分,我用Invoke-WebRequest $ uri -OutFile $ File

替换了命令

以这种方式适用于PS2和PS3!