2013-10-15 31 views
1

我一直在打破我的頭。使用WMI獲取portalinformation和ipv4地址。與PS和Python

我想要的是做一個小的Python程序這確實本安輸出ipadress與它們對應的網卡

在PowerShell中,這是由這樣可能的列表,我發現在互聯網上這個腳本:

function Get-IscsiPortNumber { 
    $PortalSummary = @() 
    $portalInfo = get-wmiobject -namespace root\wmi -class msiscsi_portalinfoclass 
    $eScriptBlock ={([Net.IPAddress]$_.ipaddr.IPV4Address).IPAddressToString} 
    $customLabel = @{Label="IpAddress"; expression = $eScriptBlock} 
    foreach ($portal in $portalInfo) { 
     foreach ($p in ($portal.portalinformation)) { 
      $CurrentPort = New-Object PsObject -Property @{ 
       Instance = ($portal.instancename).ToUpper() 
       Port  = $p.port 
       IP  = ([net.ipaddress]$p.ipaddr.IpV4Address).IPAddressToString 
      } 
      $PortalSummary += $CurrentPort 
     } 
    } 
    return $PortalSummary 
} 

Get-IscsiPortNumber | ft -AutoSize 

雖然這並不適用於所有的Windows版本。比如我得到這個錯誤消息在Windows Server 2003中:

PS C:\Documents and Settings\Administrator\Desktop> .\test.ps1 
New-Object : A parameter cannot be found that matches parameter name 'Property'. 
At C:\Documents and Settings\Administrator\Desktop\test.ps1:8 char:57 
+    $CurrentPort = New-Object PsObject -Property <<<< @{ 
New-Object : A parameter cannot be found that matches parameter name 'Property'. 
At C:\Documents and Settings\Administrator\Desktop\test.ps1:8 char:57 
+    $CurrentPort = New-Object PsObject -Property <<<< @{ 
New-Object : A parameter cannot be found that matches parameter name 'Property'. 
At C:\Documents and Settings\Administrator\Desktop\test.ps1:8 char:57 
+    $CurrentPort = New-Object PsObject -Property <<<< @{ 
New-Object : A parameter cannot be found that matches parameter name 'Property'. 
At C:\Documents and Settings\Administrator\Desktop\test.ps1:8 char:57 
+    $CurrentPort = New-Object PsObject -Property <<<< @{ 
PS C:\Documents and Settings\Administrator\Desktop> 

我有ps的幾乎爲零的經驗,所以我真的不知道爲什麼......最後幾個小時我都在努力探索WMI用ps和wmi對象瀏覽器。在對象瀏覽器中,我可以完美地看到我需要的所有統計信息。看截圖。因爲我幾乎不知道數組和屬性等在ps中如何工作,所以我希望有人能幫助我。

問候

http://i.imgur.com/iEbI3ok.png

+2

「不工作」意味着什麼?錯誤信息?不正確的結果?意外的結果? – vonPryz

+0

對不起,沒有澄清。我在原帖中添加了錯誤代碼! –

+0

你可以給你的服務器2003盒子的PowerShell版本嗎? – JPBlanc

回答

0

這也許是一個錯字,但你在這裏有一個語法錯誤:

你可以嘗試更換:

 $CurrentPort = New-Object PsObject -Property @{ 
      Instance = ($portal.instancename).ToUpper() 
      Port  = $p.port 
      IP  = ([net.ipaddress]$p.ipaddr.IpV4Address).IPAddressToString 
     } 

通過

 $CurrentPort = New-Object PsObject -Property @{ ` 
      Instance = ($portal.instancename).ToUpper();` 
      Port  = $p.port;` 
      IP  = ([net.ipaddress]$p.ipaddr.IpV4Address).IPAddressToString ` 
     } 

$CurrentPort = New-Object PsObject -Property @{Instance = ($portal.instancename).ToUpper();Port= $p.port;IP= ([net.ipaddress]$p.ipaddr.IpV4Address).IPAddressToString   } 
+0

感謝您的幫助,但我得到同樣的錯誤! –