Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

skip sections of powershell script

Posted on 2010-08-23
4
Medium Priority
?
670 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:chad
  • 3
4 Comments
 
LVL 13

Accepted Solution

by:
soostibi earned 2000 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:chad
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:chad
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:chad
ID: 33511242
duh, I had the answer already I just needed someone to point to the tree within the forest.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
Loops Section Overview
Screencast - Getting to Know the Pipeline

782 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