使用WMI获取端口信息和ipv4地址。用ps和python

时间:2013-10-15 11:36:50

标签: python powershell iscsi

我一直在打破这个。

我想要的是制作一个小型python程序,它会输出一个ipadress列表及其相应的NIC

在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几乎没有经验,所以我真的不知道为什么......过去几个小时我试图用ps和wmi对象浏览器来探索wmi。在对象浏览器中,我可以完美地看到我需要的所有统计数据。看截图。因为我完全不知道数组和属性等如何在ps中工作,我希望有人可以帮助我。

问候

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

1 个答案:

答案 0 :(得分: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            } 
相关问题