need help with script (Full Version)

All Forums >> [Scripting Technologies] >> Windows PowerShell



Message


phenry194 -> need help with script (6/12/2008 6:34:42 PM)

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
              }
   
          }




jholbach -> RE: need help with script (6/13/2008 8:46:23 AM)

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
}
}




phenry194 -> RE: need help with script (6/16/2008 2:32:35 PM)

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??




phenry194 -> RE: need help with script (6/17/2008 7:24:54 PM)

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!  : )




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.28125