【问题标题】:Upgraded to PS3, System.Net.WebClient not working on some machines升级到 PS3,System.Net.WebClient 无法在某些机器上运行
【发布时间】:2012-11-10 04:20:16
【问题描述】:

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

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

它在我的 PS2 工作站上运行良好。我假设这可能是一个许可的事情,但我不确定。 ThrustedHosts 设置为 *,脚本执行策略设置为 Unrestricted。

这是一个 sn-p 和错误:

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 + 完全限定错误 ID:WebException + PSComputerName : 192.168.2.110

【问题讨论】:

    标签: powershell powershell-remoting powershell-3.0


    【解决方案1】:

    我找到了解决方法,

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

    这种方式适用于 PS2 和 PS3!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-17
      • 1970-01-01
      • 2021-04-16
      • 2021-10-17
      相关资源
      最近更新 更多