繁体   English   中英

Powershell IP地址

[英]Powershell IP address

请求在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

您可以进行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

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

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 | 被调用,它将更改$_的数据

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM