myITforum.com Community Forum myITforum.com Community Forum

Home  Forums  Blogs  Live Support chat  Search Articles  Wiki  FAQ  Email Lists  Register  Login  My Profile  Inbox  Address Book  My Subscription  My Forums 

Photo Gallery  Member List  Search  Calendars  FAQ  Ticket List  Log Out

All Forums RSS Feed Subscription:


  


hot fix vs. no hot fix

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
  Printable Version
All Forums >> [Scripting Technologies] >> Windows PowerShell >> hot fix vs. no hot fix Page: [1]
Login
Message << Older Topic   Newer Topic >>
hot fix vs. no hot fix - 8/22/2008 1:27:43 AM   
phenry194

 

Posts: 197
Score: 0
Joined: 3/30/2007
Status: offline
I am just learning powershell.  I am working on a script I found that scans to see if a hot fix is installed, and if so, dumps an entry into a log.  What I am trying to do is dump an entry in the same log if it DOES NOT have the hot fix installed.  There is a foreach entry, so it would dump an entry for EVERY OTHER hotfix that doesn't match up.  Can anyone help?  Here is what I have so far.  THanks.  : )

# Get content
$computers = get-content c:\powershell\labs\computers.txt
# Get all the info using WMI
$results = get-wmiobject -class “Win32_QuickFixEngineering” -namespace “root\CIMV2"
-computername $computers
# Loop through $results and look for a match then output to screen
foreach ($objItem in $results)
{
   if ($objItem.HotFixID -match “KB932168")
   {
       "PC Name: " + $objItem.CSName
       "Hotfix KB932168 installed”
       " "
   }
}
Post #: 1
RE: hot fix vs. no hot fix - 8/22/2008 9:05:07 AM   
martylist

 

Posts: 41
Score: 0
Joined: 6/10/2005
From: Colorado, USA
Status: offline
There's a few options for doing this, depends on how efficient/flexible/complicated you want your script.

# Set the hotfix to search for:
$HotFixID = "KB932168"
# Get input list:
$Computers = get-content "d:\tmp\computers.txt"
# Set output file:
$LogFile = "d:\tmp\$HotFixID.log"

foreach ($Computer in $Computers)
{
# " DEBUG: " + $Computer
# Get the HotFix info using WMI:
$Results = get-wmiobject -class "Win32_QuickFixEngineering" -namespace "root\CIMV2" -computername $Computer
$NextResult = "Unknown/Error"
# Loop through $Results and look for a match:
foreach ($objItem in $Results)
{
 # " DEBUG: " + $objItem.HotFixID
 $NextResult = "Not Installed"
 if ($objItem.HotFixID -eq $HotFixID)
 {
  $NextResult = "Installed"
  break;
 }
}

# Output is tab-delimited:
$Output = "$Computer Hotfix $HotFixID $NextResult"
Write-Host $Output
Add-Content $LogFile -value $Output
}
 This script loops through every hotfix installed, which is not very efficient.  If you will always be looking for a single hotfix, you may want to change the WMI query to something like this:

$Results = get-wmiobject -query "SELECT * FROM Win32_QuickFixEngineering WHERE HotFixID='$HotFixID'" -computername $Computer
 

(in reply to phenry194)
Post #: 2
RE: hot fix vs. no hot fix - 8/22/2008 11:06:03 AM   
phenry194

 

Posts: 197
Score: 0
Joined: 3/30/2007
Status: offline
Will the second option still dump a yay or nay into the log for each machine I query?  I want the log to reflect either one or the other, so I can tell the particular machines were actually queried, as opposed to maybe they were queried, but we're really not sure if there is no entry at all in the log.  This would account for machines not being queried because they were offline.  Thanks for your quick response!  I will give these guys a try.  : )

(in reply to martylist)
Post #: 3
RE: hot fix vs. no hot fix - 8/22/2008 1:29:54 PM   
martylist

 

Posts: 41
Score: 0
Joined: 6/10/2005
From: Colorado, USA
Status: offline
 Have you tried it yet?  There's very little error checking, but I was just expanding on the script you posted.  Some people prefer no erro checking so the script is small/simple, others prefer elaborate error checking after every command, I'll leave that up to you but I can help with specifgic questions about error checking.  The output should be one of these 3 lines: computer1 Hotfix KB932168 Installed
computer2 Hotfix KB932168 Not Installedcomputer3 Hotfix KB932168 Unknown/Error
 On a different note, look over the script carefully when using copy/paste for scripts.  For example, the script you pasted has 3 different types of double quotes:“Win32_QuickFixEngineering” -namespace “root\CIMV2" Another example, the script I pasted used tabs for the output so it would import into Excel nicely, but when I pasted it here the tabs were converted to spaces. 

(in reply to phenry194)
Post #: 4
RE: hot fix vs. no hot fix - 8/22/2008 1:50:15 PM   
phenry194

 

Posts: 197
Score: 0
Joined: 3/30/2007
Status: offline
I did try it and then had to go to a meeting, so I am just now getting back to you.  I tried both methods and the second one worked a little quicker for the reasons you mentioned.  The only thing I don't like about it is that if the machine is offline, it takes a while, times out on that machine, and enters that the machine doesn't have the hot fix, which may or may not be true, instead of just saying it's not online or leaving it off alltogether.  Can we do something so that it either reflects it is offline or doesn't make an entry into the log at all if it isn't pingable?  Boy, I'm sure demanding, aren't I?  I am just absolutely thrilled to death that you got me something so quickly.  I was fighting with it last night and couldn't get it to do it quite right, so you have been VERY helpful.  thanks so much.  : )

(in reply to martylist)
Post #: 5
RE: hot fix vs. no hot fix - 8/22/2008 3:02:30 PM   
martylist

 

Posts: 41
Score: 0
Joined: 6/10/2005
From: Colorado, USA
Status: offline
 I don't know of a way to make the WMI timeout shorter.  You could try pinging each machine first, and then skip the machine if it does not reply to the ping.  But it's possible that a machine is accesible to you via WMI but it won't reply to pings because of a local firewall settting or a firewall in between the two machines, so you would miss those machines - but that is not common. http://www.google.com/search?q=powershell+win32%5Fpingstatus


The first script I posted was supposed to report "Unknown/Error" for offline machines but it's not working.  This script has another check, "if ($Results -ne $null)" which should fix that problem, but adding the ping check would take more time than I have.
# Set the hotfix to search for:
$HotFixID = "KB932168"
# Get input list:
$Computers = get-content "d:\tmp\computers.txt"
# Set output file:
$LogFile = "d:\tmp\$HotFixID.log"

foreach ($Computer in $Computers)
{
# " DEBUG: " + $Computer
# Get the HotFix info using WMI:
$Results = get-wmiobject -query "SELECT * FROM Win32_QuickFixEngineering WHERE HotFixID='$HotFixID'" -computername $Computer
$NextResult = "Unknown/Error"
if ($Results -ne $null)
{
 # Loop through $Results and look for a match:
 foreach ($objItem in $Results)
 {
  # " DEBUG: " + $objItem.HotFixID
  $NextResult = "Not Installed"
  if ($objItem.HotFixID -eq $HotFixID)
  {
   $NextResult = "Installed"
   break;
  }
 }
}

$Output = "$Computer Hotfix $HotFixID $NextResult"
Write-Host $Output
Add-Content $LogFile -value $Output
}

(in reply to phenry194)
Post #: 6
RE: hot fix vs. no hot fix - 8/22/2008 3:07:23 PM   
phenry194

 

Posts: 197
Score: 0
Joined: 3/30/2007
Status: offline
Terrific!  I will give this a go.  Thanks so much for all your help.  You're great!  : )

(in reply to martylist)
Post #: 7
Page:   [1]
All Forums >> [Scripting Technologies] >> Windows PowerShell >> hot fix vs. no hot fix Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts



  
Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI

0.281