Solved

skip sections of powershell script

Posted on 2010-08-23
4
571 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:kabaam
  • 3
4 Comments
 
LVL 13

Accepted Solution

by:
soostibi earned 500 total points
ID: 33508420
It is there in your code:
# 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  
##################################
# Here is the part that will run if computer could be pinged
##################################
}  
else {  
"no response"  
##################################
# This is the part that will run if computer could not be pinged
##################################

}  

##################################
# And this is the part that will run anyway
##################################

Open in new window

0
 
LVL 11

Author Comment

by:kabaam
ID: 33511036
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'"
0
 
LVL 11

Author Comment

by:kabaam
ID: 33511229
okay.  I used a $null check on $obj32nic

if ($obj32nic) {write-host "not null"} else {write-host "null"}
0
 
LVL 11

Author Closing Comment

by:kabaam
ID: 33511242
duh, I had the answer already I just needed someone to point to the tree within the forest.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question