Link to home
Start Free TrialLog in
Avatar of chad
chad

asked on

skip sections of powershell script

I am very new to powershell scripting (started today) and using tutorials and samples online I have a decent script that will populate a xls with computer specs.  I would like to have portions skipped over if the computer does not reply to pings.  Any ideas on how to do this?

Thanks in advanced.  

#$erroractionpreference = "SilentlyContinue"

# Create a New Excel Object for storing Data

$a = New-Object -comobject Excel.Application


$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)

# Create the title row

$c.Cells.Item(1,1) = "Machine Name" 
$c.Cells.Item(1,2) = "Location"
$c.Cells.Item(1,3) = "IP Address"
$c.Cells.Item(1,4) = "Dell Service Tag"
$c.Cells.Item(1,5) = "Computer Model"
$c.Cells.Item(1,6) = "OS / SP"
$c.Cells.Item(1,7) = "CPU Type"
$c.Cells.Item(1,8) = "CPU Speed"
$c.Cells.Item(1,9) = "RAM"
$c.Cells.Item(1,10) = "HD Free"


$d = $c.UsedRange
$d.Interior.ColorIndex = 23
$d.Font.ColorIndex = 2
$d.Font.Bold = $True

$intRow = 2

# read list of computer names or IP addresses into array
$colComputers = get-content "list.txt"

# process all of below once for each hostname or IP address
foreach ($strComputer in $colComputers)
{

write-host ""
write-host "Attempting to ping "  $strComputer
# Now ping the system
$ping = get-wmiobject -Query "select * from win32_pingstatus where Address='$strcomputer'"
# Display Results
if ($ping.statuscode -eq 0) {
"Response time: {0}ms" -f $ping.responsetime
}
else {
"no response"

}






    $obj32NIC = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -namespace "root\CIMV2" -computername $strComputer -Filter "IPEnabled = 'True'"
    $col32OS =Get-WmiObject -class Win32_OperatingSystem -computername $Strcomputer
    $col32System = get-wmiobject -class "Win32_ComputerSystem" -namespace "root\CIMV2" -computername $strcomputer
    $col32Bios = Get-WmiObject -Class Win32_BIOS -namespace "root\CIMV2" -computername $strComputer
    $col32Proc = get-wmiobject -class "Win32_Processor" -namespace "root\CIMV2" -computername $strComputer

 write-host "Gathering Info for " $strComputer
 foreach ($objNACItem in $obj32NIC)
        {
         $c.Cells.Item($intRow, 1) = $objNACItem.DNSHostname
         $c.Cells.Item($intRow, 3) = $objNACItem.IPAddress
        }
        
 foreach ($objBiosItem in $col32Bios)
        {
        $c.Cells.Item($intRow, 4) = $objBiosItem.SerialNumber
        } 
        
 foreach ($objSYSitem in $col32System)
        {
         $c.Cells.Item($intRow, 5) = $objSYSitem.Manufacturer + $objSysItem.model
         $c.Cells.Item($intRow, 9) = "GB: " + $objSYSitem.TotalPhysicalMemory/1000000000
  
        } 
        
 foreach ($objOSitem in $col32OS)
        {
        
        if ($col32System.domainrole = 1 -or 0)
           {
            switch ($objOSitem.Version)
                {
                 "5.0.2195"{$c.Cells.Item($intRow, 6) = "2000 " + $objOSitem.ServicePackMajorVersion}            
                 "5.1.2600"{if ($objOSitem.ServicePackMajorVersion -gt 0)
                               {$c.Cells.Item($intRow, 6) = "Win XP SP " + $objOSitem.ServicePackMajorVersion }
                               else 
                                {$c.Cells.Item($intRow, 6) = "Win XP"} 
                            }    
                 "6.1.7600"{if ($objOSitem.ServicePackMajorVersion -gt 0)
                               {$c.Cells.Item($intRow, 6) = "Win Vista SP " + $objOSitem.ServicePackMajorVersion }
                               else 
                                {$c.Cells.Item($intRow, 6) = "Win Vista"} 
                            } 
                 "6.1.7600"{if ($objOSitem.ServicePackMajorVersion -gt 0)
                               {$c.Cells.Item($intRow, 6) = "Win 7 SP " + $objOSitem.ServicePackMajorVersion }
                               else 
                                {$c.Cells.Item($intRow, 6) = "Win 7"} 
                            }            
                 }
           }
        
        
         #$c.Cells.Item($intRow, 6) = $objOSitem.Version
         #$c.Cells.Item($intRow, 26) = $objOSitem.ServicePackMajorVersion
        }      
                         
 foreach ($objProcItem in $col32Proc)
        {
         $c.Cells.Item($intRow, 7) = $objProcItem.Name + " X " + $objSysitem.NumberOfProcessors  
         $c.Cells.Item($intRow, 8) = $objProcItem.CurrentClockSpeed
        }  
        
    
    $intRow = $intRow + 1
}


$d = $c.UsedRange
$d.EntireColumn.AutoFit()
$a.visible = $True

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of soostibi
soostibi
Flag of Hungary image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chad
chad

ASKER

uhg, so it was there the whole time.

is it possible to make an if statement with this?
$obj32NIC = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -namespace "root\CIMV2" -computername $strComputer -Filter "IPEnabled = 'True'"
Avatar of chad

ASKER

okay.  I used a $null check on $obj32nic

if ($obj32nic) {write-host "not null"} else {write-host "null"}
Avatar of chad

ASKER

duh, I had the answer already I just needed someone to point to the tree within the forest.