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

Looping Problems

$
0
0

$NoPing = "Can't Ping Machine"
$erroractionpreference = “SilentlyContinue”
$a = New-Object -comobject Excel.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) = “Computer OS Version”
$c.Cells.Item(1,3) = “Admin$”
$c.Cells.Item(1,4) = “Description”
$d = $c.UsedRange
$d.Interior.ColorIndex = 37 
$d.Font.ColorIndex = 1     
$d.Font.Bold = $True
$d.EntireColumn.AutoFit()
$intRow = 2
$NoPing = "Can't Ping Machine"

$machine = get-content 'c:\temp\tmp\ping\CMPing.txt'
foreach ($pc in $machine) {$getip = ((Test-Connection $pc -Count 1));
    if ($getip -like '*Source*')   {
           
     $computerOS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $pc | Select-Object -     ExpandProperty version;
            $admindollar = "Admin$ Reachable"
            $desc = (Get-ADComputer $pc -Properties Description).description
     $c.Cells.Item($intRow, 1) = $pc
            $c.Cells.Item($intRow, 2) = $computerOS
            $c.Cells.Item($intRow, 4) = $desc
            $intRow += 1
            $d.EntireColumn.AutoFit()  }

    Else {
        $c.Cells.Item($intRow, 1) = $pc;
        $c.Cells.Item($intRow, 4).Interior.ColorIndex = 6
        $c.Cells.Item($intRow, 4).Font.ColorIndex = 1  
        $c.Cells.Item($intRow, 4) = $NoPing
    }}

I have more code than this but this is the meat of the script.  Before this runs I have some code that opens Excel and creates the headers.

Here is my problem.   If the machine cannot be pinged then the ELSE should run and does.  The value for $NoPing is entered into cell .d but then 1 second later it is overwritten with the value from $desc.   I was watching this as it happened.   I see "Can't Ping Machine" and then 1 second later it is overwritten with the Description data from AD.

how come? 

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles