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:


  


need help with script

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

Logged in as: Guest
  Printable Version
All Forums >> [Scripting Technologies] >> Windows PowerShell >> need help with script Page: [1]
Login
Message << Older Topic   Newer Topic >>
need help with script - 6/12/2008 6:34:42 PM   
phenry194

 

Posts: 187
Score: 0
Joined: 3/30/2007
Status: offline
I borrowed a script from someone that pings machines, then checks to see if a hot fix is installed, and then it populates an excel spreadsheet with the results.  Very cool.  I modified it to ping a list of machines and then check to see if a particular executable exists.  I have it working except the last part where it populates the excel spreadsheet.  I want it to tell it the executable exists or it doesn't.  No matter what I do on it, it populates it with results of NO machines having that executable, and I know a number of them do.  Can someone please take a look at the bottom half of the script and tell me where I have gone wrong?  I'm pretty new to powershell, and very close, but not close enough.  Thanks for any help you can provide.  I have remmed out a bunch of stuff since this is a borrowed script, but I wanted to preserve it for reference if necessary.  Thanks.

## *********** Beginning of function Pingme   ***********
function PingMe ($pc)
{
$ping = new-object System.Net.NetworkInformation.Ping
  $rslt = $ping.send("$pc")
  if ($rslt.status –eq “Success”) {
                                    #write-host
                                    write-host (“ping $pc worked”) -foregroundcolor "Yellow"
                                    #write-host
                                    return $true
                                  }
         else {
                ""
               write-warning “$pc is not reachable”
               return $false
              }
      # $ping = $null
}    # End of function PingMe
############################################################
$ErrorActionPreference = "SilentlyContinue"
#$Ppid = "KB932762"
#
$EXobj = New-Object -comobject Excel.Application
$EXobj.visible = $True
$book = $EXobj.Workbooks.Add()
$col = $book.Worksheets.Item(1)
$col.Cells.Item(1,1) = "Machine Name"
$col.Cells.Item(1,2) = "IClient.exe"
#$col.Cells.Item(1,3) = "InstalledBy"
#$col.Cells.Item(1,4) = "InstalledOn"
$dd = $col.UsedRange
$dd.Interior.ColorIndex = 19
$dd.Font.ColorIndex = 11
$dd.Font.Bold = $True
$dd.EntireColumn.AutoFit()
$row = 2
foreach ($pc in get-content c:\temp\laptops2.txt)
      {
        $online=PingMe($pc)
         if ($online -eq $true)
           {
             
function integrity ($pc)
{
    $newpath="\\$pc\c$\program files\checkpoint\integrity client\iclient.exe"
 get-childitem $newpath}
 $var = get-content 'c:\temp\laptops2.txt'
 foreach ($i in $var) {write-host ($i)}
 $yeppers=integrity
 if ($yeppers -eq $true)
                {
                        $col.Cells.Item($row,1) = $pc
                        $col.Cells.item($row,2) = "installed"
                        #$col.Cells.Item($row,3) = $Hotp.InstalledBy
                        #$col.Cells.Item($row,4) = $Hotp.InstalledOn                       
                }                      else
                        {
                          $col.Cells.Item($row,1) = $pc
                          $col.Cells.item($row,2) = "Not installed"
                          #$col.Cells.Item($row,3) = "None"
                          #$col.Cells.Item($row,4) = "None"                       
                        }
                    #$Hotp=""
                    $row = $row + 1
              }
   
          }
Post #: 1
RE: need help with script - 6/13/2008 8:46:23 AM   
jholbach

 

Posts: 12
Score: 0
Joined: 8/31/2003
Status: offline
Try something like this:

function PingMe ($pc)
{
$ping = new-object System.Net.NetworkInformation.Ping
$rslt = $ping.send("$pc")
if ($rslt.status -eq "Success") {
 write-host ("ping $pc worked") -foregroundcolor "Yellow"
 $true
}
else {
 write-warning "$pc is not reachable"
 $false
}
}    # End of function PingMe
function integrity ($pc)
{
$newpath="\\$pc\c$\program files\checkpoint\integrity client\iclient.exe"
get-childitem $newpath
}

$ErrorActionPreference = "SilentlyContinue"
$EXobj = New-Object -comobject Excel.Application
$EXobj.visible = $True
$book = $EXobj.Workbooks.Add()
$col = $book.Worksheets.Item(1)
$col.Cells.Item(1,1) = "Machine Name"
$col.Cells.Item(1,2) = "IClient.exe"
$dd = $col.UsedRange
$dd.Interior.ColorIndex = 19
$dd.Font.ColorIndex = 11
$dd.Font.Bold = $True
$dd.EntireColumn.AutoFit()
$row = 2
foreach ($pc in get-content c:\temp\laptops2.txt)
{
$online=PingMe($pc)
if ($online -eq $true) {
 write-host ($pc)
 $yeppers=integrity($pc)
 if ($yeppers -eq $true) {
  $col.Cells.Item($row,1) = $pc
  $col.Cells.item($row,2) = "installed"
 } else {
  $col.Cells.Item($row,1) = $pc
  $col.Cells.item($row,2) = "Not installed"
 }
$row = $row + 1
}
}


< Message edited by jholbach -- 6/13/2008 8:59:58 AM >


_____________________________

Jim Holbach
http://pathologicalscripter.wordpress.com/

(in reply to phenry194)
Post #: 2
RE: need help with script - 6/16/2008 2:32:35 PM   
phenry194

 

Posts: 187
Score: 0
Joined: 3/30/2007
Status: offline
I tried your script, and I also tried the note you appended to change
"$yeppers=integrity" to "$yeppers=integrity($i).  When I changed it, it kept pinging ALL the machines for each ping, so it pinged each machine multiple times.  I then changed it  from integrity($1) to $newpath, but that said ALL the machines had it installed instead of all the machines didn't have it installed.  About half the machines have it installed, so I should results reflecting that.  Help??

(in reply to jholbach)
Post #: 3
RE: need help with script - 6/17/2008 7:24:54 PM   
phenry194

 

Posts: 187
Score: 0
Joined: 3/30/2007
Status: offline
I finally got it to work.  Near the bottom of the script where I state if the file exists, it should be if ($? -eq $true).  The $? variable resets the loop, as it were, so it doesn't just pick up the first response and repeat it continuously throughout the process of entering the results into the excel file.  Works great!  : )

(in reply to phenry194)
Post #: 4
Page:   [1]
All Forums >> [Scripting Technologies] >> Windows PowerShell >> need help with script 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.297