Solved

skip sections of powershell script

Posted on 2010-08-23
4
550 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
duh, I had the answer already I just needed someone to point to the tree within the forest.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article will help you understand what HashTables are and how to use them in PowerShell.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now