2014-10-28 38 views
0

此腳本完美工作,直到我把$Server作爲計算機名稱,而不是實際的計算機名稱。請指教。我得到的錯誤是ERROR: Exception calling "DownloadFile" with "2" argument(s): "URI formats are not supported."腳本工作正常,直到我使用變量爲服務器名稱

$Servers = Get-Content -Path C:\temp\servers.txt 

foreach ($Server in $Servers) 
{ 
    $web = New-Object System.Net.WebClient 
    $web.DownloadFile("http://$server:1055/sinfo?gr=1","c:\temp\Test.xml") 

    [xml] $xdoc = Get-Content c:\Temp\test.xml 
    $properties = @{ 
    Serialnumber = $xdoc.sinfo.systeminfo.bios.SerialNumber; 
      Model = $xdoc.sinfo.systeminfo.sys.Model; 
    Manufacturer = $xdoc.sinfo.systeminfo.sys.Manufacturer; 
    ComputerName = $xdoc.sinfo.systeminfo.sys.Name; 
      Domain = $xdoc.sinfo.systeminfo.sys.Domain; 
    } 
} 

$obj = new-object psobject -property $properties 
$obj | Select-Object model, manufacturer, ComputerName, domain, SerialNumber | export-csv c:\temp\results.csv -NoTypeInformation -Append 
+0

有沒有在這些線路的任何空白?也許使用'$($ server.Trim())'代替'$ server'。你是否單獨查看了「http:// $ server:1055/sinfo?gr = 1」的輸出,而不是「DownloadFile」的一部分? – Matt 2014-10-28 17:37:42

+0

如果你這樣做,那麼每次循環運行時,properties變量都會被覆蓋,因此$ obj將只包含最後一個服務器的結果 – Paul 2014-10-28 17:40:28

+0

@Matt我已經看過這個文件,它是xml格式。我試過修剪,並且一放入$服務器變量,仍然得到「URI格式不受支持」 – NobleMan 2014-10-28 18:02:04

回答

1

結腸是在PowerShell中scope operator。您的腳本正在查找變量$server:1055,這意味着它在server的作用域或名稱空間中查找$1055

嘗試:

"http://$server`:1055/sinfo?gr=1" 

或者:

"http://$($server):1055/sinfo?gr=1" 

或者:

"http://${server}:1055/sinfo?gr=1" 
+0

這就是它@BaconBits!這個工作喜歡一種魅力。「http:// $ server':1055/sinfo?gr = 1」 – NobleMan 2014-10-28 18:10:25

相關問題