2013-10-15 32 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

感谢您的帮助,但我得到同样的错误! –