Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Odd Behavior

$
0
0

I am reading a .txt file and this .txt file has a list of over 100 PC names.   I want to read the .txt file, check to see if the PC is online and if it is write data to an Excel spreadsheet.  What is very odd is the script END's after only a few PC's.    My Excel sheet has 5 rows and then the script ends.   But, if I remove the if (Test-Connection -ComputerName $machine -Count 1 -Quiet) LOOP and the ELSE statement then my Excel spreadsheet will have 95 rows written out. 

Why would this code exit after only reading a few records?  on my ELSE statement I do see "Cannot Ping" being written out twice and then the script ends.   

 

$a=New-Object-comobjectExcel.Application
$a.visible=$True
$b=$a.Workbooks.Add()
$c=$b.Worksheets.Item(1)
$c.Cells.Item(1,1) =“Computer Name”
$c.Cells.Item(1,2) =“OS Version”
$c.Cells.Item(1,3) =“IP vLan”
$c.Cells.Item(1,4) =“Description”
$c.Cells.Item(1,5) =“Model”
$d=$c.UsedRange
$d.Interior.ColorIndex= 37
$d.Font.ColorIndex= 1
$d.Font.Bold=$True
$d.EntireColumn.AutoFit()
$intRow= 2


$pc=Get-Content"c:\temp9\ping.txt"


foreach ($machinein$pc){

if (Test-Connection-ComputerName$machine-Count 1 -Quiet)
{

$computerName=Get-WMIObject-class Win32_ComputerSystem -computername$machine | Select-Object-ExpandProperty name;
$computerOS=Get-WmiObject-Class Win32_OperatingSystem -ComputerName$machine | Select-Object-ExpandProperty version;
$getip= ([version](Test-Connection$machine-Count 1).IPV4Address.IPAddressToString);
$getip= ([version](Test-Connection$machine-Count 1).IPV4Address.IPAddressToString).Build; this gets just the 3rd Octet
$desc= (Get-ADComputer$machine-Properties Description).description
$model= (Get-WmiObject-Class Win32_ComputerSystem -ComputerName$machine).model

# if ComputerOS contains *6.1* then make the background color in Excel gray
$c.Cells.Item($intRow, 1) =$computerName
if ($computerOS.contains("6.1")) {
$c.Cells.Item($intRow, 2).Interior.ColorIndex= 3
$c.Cells.Item($intRow, 2).Font.ColorIndex= 2
}
$c.Cells.Item($intRow, 2) =$computerOS



$c.Cells.Item($intRow, 3) =$getip
$c.Cells.Item($intRow, 4) =$desc
# if $model contains 171 then make the background color in Excel yellow
if ($model.contains("171")) {
$c.Cells.Item($intRow, 5).Interior.ColorIndex= 6
$c.Cells.Item($intRow, 5).Font.ColorIndex= 1
}
$c.Cells.Item($intRow, 5) =$model

$intRow+= 1
$d.EntireColumn.AutoFit()


} ELSE {write-host"Cannot Ping"}


}


Viewing all articles
Browse latest Browse all 6937

Trending Articles