Reputation: 43
Requesting some ideas on modifying below PS script. Currently I have to manually change the first 3 octets of the IP address in line 37 to the IP's of the location that this script is being deployed to. Was checking if someone knows a way I could modify script so I don't have to manually change the first three octets, last octet will always be 1-60. Version of PS on host systems is 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
Upvotes: 0
Views: 236
Reputation: 36277
You can do a WMI call to get the local IP, and do a RegEx replace to get the first three octets. This replaces the last .### with nothing to get just the first three octets.
$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
Upvotes: 1
Reputation: 46690
Let say you have a text file with the following contents
10.10.13
10.10.14
10.10.15
Those are some internal subnets. Putting your last statement in a loop you would get something like this
Get-Content c:\pathtotext\subnets.txt | ForEach-Object{
$subnet = $_
1..60 | % { Check-Patches "$subnet.$_" } | Export-Csv "C:\temp\PatchVerify\$env:ComputerName.csv" -NoTypeInformation
}
You read the contents of the file and for each line in the file run your statement. The assignment $subnet = $_
is required since another pipe 1..60 |
is called which would change the data in $_
Upvotes: 0