Powershell IP地址

时间:2014-08-27 18:37:31

标签: powershell

请求修改以下PS脚本的一些想法。目前,我必须手动将第37行中IP地址的前3个八位字节更改为该脚本部署到的位置的IP地址。检查是否有人知道我可以修改脚本的方式所以我不必手动更改前三个八位字节,最后一个八位字节将始终为1-60。主机系统上的PS版本是2.0

Function Check-Patches{
Param($Filename)

    $logname = "C:\temp\PatchVerify\$FileName.csv"
    [xml]$x = Get-Content "C:\Users\Cambridge\SecurityScans\$FileName.mbsa"

    $PatchStatus = @()

    #This list is created based on a text file that is provided.
    $monthlyPatches = Get-Content "C:\Temp\PatchVerify\Patches_NA.txt" | ? {$_ -match "-KB(?<KB>\d+)"} | % { $matches.KB}


    #Create objects for all the patches in the updatelog that were in the monthly list.
    Switch ( $x | % {$_.SecScan} | % {$_.Check} | ? {$_.id -eq 500} | % {$_.detail} | % {$_.updatedata} | ? {$monthlyPatches -contains $_.KBID} )
    {
        {$_.isinstalled -eq "true"}
                {
                    $PatchStatus += New-Object PsObject -property @{Device=$FileName; Patch=$_.KBID; Present="YES"}
                    Continue
                }
        {$_.isinstalled -eq "false"}
                {
                    $PatchStatus += New-Object PsObject -property @{Device=$FileName; Patch=$_.KBID; Present="NO"}
                    Continue
                }
    }

    $detectedPatches = $PatchStatus | % {$_.Patch}
    #Populate all of the monthly patches that weren't found on the machine as installed or failed
    $monthlypatches | ? {$detectedPatches -notcontains $_} | % { $PatchStatus += New-Object PsObject -property @{Device=$FileName; Patch=$_; Present="Unknown"} }

    #Output results
    $PatchStatus

    }

1..60 | % { Check-Patches "172.26.210.$_" } | Export-Csv "C:\temp\PatchVerify\$env:ComputerName.csv" -NoTypeInformation

2 个答案:

答案 0 :(得分:1)

您可以执行WMI调用以获取本地IP,并执行RegEx替换以获取前三个八位字节。这取代了最后一个。###没有任何东西可以得到前三个八位字节。

$localip = @(Get-WMIObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled='TRUE'")[0].ipaddress[0] -replace "\.\d+$"
1..60 | % { Check-Patches "$localip.$_" } | Export-Csv "C:\temp\PatchVerify\$env:ComputerName.csv" -NoTypeInformation

答案 1 :(得分:0)

假设您有一个包含以下内容的文本文件

10.10.13
10.10.14
10.10.15

这些是一些内部子网。把你的最后一个语句放在一个循环中你会得到类似这样的东西

Get-Content c:\pathtotext\subnets.txt | ForEach-Object{
    $subnet = $_
    1..60 | % { Check-Patches "$subnet.$_" } | Export-Csv "C:\temp\PatchVerify\$env:ComputerName.csv" -NoTypeInformation
}

您读取文件的内容,并为文件中的每一行运行您的语句。分配$subnet = $_是必需的,因为调用另一个管道1..60 |会更改$_

中的数据