Sunday, 15 August 2010

Powershell assistance -



Powershell assistance -

i using below ps script check if currents months ms patches installed on system. script set check $env:computername.mbsa , patch_na.txt file , send result $env:computername.csv file.

i need modify script pull info other pos devices in same location (c:\users\cambridge\securityscans) , send results $env:computername.csv file. pos devices listed this:

172.26.210.1.mbsa 172.26.210.2.mbsa 172.26.210.3.mbsa

and forth. ip range @ our locations (last octet) 1 - 60. ideas on how can set up?

script:

$logname = "c:\temp\patchverify\$env:computername.csv" [xml]$x=type "c:\users\cambridge\securityscans\$env:computername.mbsa" #this list created based on text file provided. $montlypatches = type "c:\temp\patchverify\patches_na.txt"| foreach{if ($_ -mat"-kb(? <kb>\d+)"){$matches.kb}} $patchesnotinstalled=$x.secscan.check | {$_.id -eq 500} |foreach{` $_.detail.updatedata|where {$_.isinstalled -eq "false"}}|select -expandproperty kbid $patchesinstalled =$x.secscan.check | {$_.id -eq 500} |foreach{` $_.detail.updatedata|where {$_.isinstalled -eq "true"}}|select -expandproperty kbid "store,patch,present"> $logname $store = "$env:computername" foreach ($patch in $montlypatches) { $result = "unknown" if ( $patchesinstalled -contains $patch) { $result = "yes" } if ( $patchesnotinstalled -contains $patch) { $result = "no" } "$store,kb$($patch),$result" >>$logname }

you can find lots of info on creating functions on web, simple illustration be:

function check-patches{ param($filename) $logname = "c:\temp\patchverify\$filename.csv" [xml]$x=type "c:\users\cambridge\securityscans\$filename.mbsa" rest of existing code goes here... } check-patches "$env:computername" for($i=1;$i -le 60;$i++){ check-patches "172.26.210.$i" }

if need me break downwards in allow me know , i'll go farther explanation, have looks have decent grasp on powershell theory , needed know resources available.

edit: updated illustration improve fit script, having take file name, , applying file name $logname , $x variables within function.

the break down...

first declare creating function using function keyword. next name of function utilize later phone call it, , opening curly brace start scriptblock makes actual function.

next param line, in case simple declaring 1 variable input. alternatively done function check-patches ($filename){ when start getting more advanced functions gets confusing, recommendation stick putting parameters within function's scriptblock. first thing want within of function in cases, excluding help write function.

then have updated lines $logname , [xml]$x utilize $filename function gets input.

after comes of code parses patch logs, , outputs csv, , closing curly brace ends scriptblock, , function.

then phone call computername, , run loop. loop runs between 1 , 60, , each loop uses number lastly octet of file name feed function , check files.

a few comments on rest of code. $monthlypatches = changed = type | ?{$_ -match "-kb(? <kb>\d+)"}|%{$matches.kb} results filtered before foreach loop, cutting downwards on time.

on $patchesinstalled , $patchesnotinstalled lines don't need backtick @ end of line. can naturally have linebreak after origin of scriptblock foreach loop. having there can hard see later if script breaks, , if there after (including space) script can break , throw errors hard track down.

lastly, loop through $x twice, , $monthlypatches once, , lot of individual writes log file. suggest creating array, filling custom objects have 3 properties (store, patch, , present), , outputting @ end of function. changes things little bit, function outputs object, pipe export-csv, or maybe later want else, @ to the lowest degree you'd have it. i'd run $x through switch see if things installed, i'd flush out array setting of monthlypatches aren't in array unknown. go like:

function check-patches{ param($filename) $logname = "c:\temp\patchverify\$filename.csv" [xml]$x=type "c:\users\cambridge\securityscans\$filename.mbsa" $patchstatus = @() #this list created based on text file provided. $monthlypatches = gc "c:\temp\patchverify\patches_na.txt"|?{$_ -match "-kb(? <kb>\d+)"} | %{$matches.kb} #create objects patches in updatelog in monthly list. switch($x.secscan.check|?{$_.kbid -in $monthlypatches -and $_.id -eq 500}){ {$_.detail.updatedata.isinstalled -eq "true"}{$patchstatus+=[pscustomobject][ordered]@{store=$filename;patch=$_.kbid;present="yes"};continue} {$_.detail.updatedata.isinstalled -eq "false"}{$patchstatus+=[pscustomobject][ordered]@{store=$filename;patch=$_.kbid;present="no"};continue} } #populate of monthly patches weren't found on machine installed or failed $monthlypatches | ?{$_ -notin $patchstatus.patch} | %{$patchstatus += [pscustomobject][ordered]@{store=$filename;patch=$_;present="unknown"}} #output results $patchstatus } #check patches on current computer check-patches "$env:computername"|export-csv "c:\temp\patchverify\$env:computername.csv" -notypeinformation #check patches on pos devices for($i=1;$i -le 60;$i++){ check-patches "172.26.210.$i"|export-csv "c:\temp\patchverify\172.26.210.$i.csv" -notypeinformation }

powershell

No comments:

Post a Comment