Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - If and Elseif Conditional Logic

Reply
 
Old 12-19-2007   #1 (permalink)
Rey Perez


 
 

If and Elseif Conditional Logic

I have pieced together a small script to get the contents of a text file
which includes several workstations. The result I am looking for is if the
path to a particular file cannot be found, then mark as “Needs Attention” in
Excel. If it is found, to mark as “OK”.Each time I run the script it imports
correctly the systems that do have the file but it does not display the
systems that do not or are possibly offline. Also, it never displays OK or
Needs Attention. Can someone please enlighten my remedial powershell
scripting?

Thanks,
Rey

$Excel = New-Object -ComObject Excel.Application
$Excel.visible = $True
$Excel = $Excel.Workbooks.Add()
$Sheet = $Excel.Worksheets.Item(1)

$Sheet.Cells.Item(1,1) = "MACHINE NAME"
$Sheet.Cells.Item(1,2) = "OPERATING SYSTEM"
$Sheet.Cells.Item(1,3) = "STATUS"
$intRow = 2

$WorkBook = $Sheet.UsedRange
$WorkBook.Interior.ColorIndex = 5
$WorkBook.Font.ColorIndex = 19
$WorkBook.Font.Bold = $True

$Machine = Get-Content "C:\Data\ESCG\MachineList3.Txt"
$System = Get-WMIObject Win32_OperatingSystem -ComputerName $Machine
ForEach($objItem in $System)
{
$Sheet.Cells.Item($intRow, 1) = $objItem.CSName
$Sheet.Cells.Item($intRow, 2) = $objItem.Caption

$path = Test-Path "\\$Machine\c$\Program Files\Symantec Antivirus\VPC32.exe"

If ($path -eq "True"){
$Sheet.Cells.Item($intRow, 3).Interior.ColorIndex = 4
$Sheet.Cells.Item($intRow, 3) = "Good"
}
Elseif ($path -eq "False"){
$Sheet.Cells.Item($intRow, 3).Interior.ColorIndex = 3
$Sheet.Cells.Item($intRow, 3) = "Needs Attention!"
}

$intRow = $intRow + 1
}
$WorkBook.EntireColumn.AutoFit()


My System SpecsSystem Spec
Old 12-19-2007   #2 (permalink)
Brandon Shell [MVP]


 
 

Re: If and Elseif Conditional Logic

Powershell has built in Bool values $true $false

By default if looks for a $True condition so you can just do
if($path){...}else{...}

it is important to realize that test-path just returns $true or $false...
it doesnt return the path. so maybe something like

If (Test-Path "\\$Machine\c$\Program Files\Symantec Antivirus\VPC32.exe")
$Sheet.Cells.Item($intRow, 3).Interior.ColorIndex = 4
$Sheet.Cells.Item($intRow, 3) = "Good"
}
Else{
$Sheet.Cells.Item($intRow, 3).Interior.ColorIndex = 3
$Sheet.Cells.Item($intRow, 3) = "Needs Attention!"
}

Brandon Shell
---------------
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject

RP> I have pieced together a small script to get the contents of a text
RP> file which includes several workstations. The result I am looking
RP> for is if the path to a particular file cannot be found, then mark
RP> as "Needs Attention" in Excel. If it is found, to mark as "OK".Each
RP> time I run the script it imports correctly the systems that do have
RP> the file but it does not display the systems that do not or are
RP> possibly offline. Also, it never displays OK or Needs Attention. Can
RP> someone please enlighten my remedial powershell scripting?
RP>
RP> Thanks,
RP> Rey
RP> $Excel = New-Object -ComObject Excel.Application
RP> $Excel.visible = $True
RP> $Excel = $Excel.Workbooks.Add()
RP> $Sheet = $Excel.Worksheets.Item(1)
RP> $Sheet.Cells.Item(1,1) = "MACHINE NAME"
RP> $Sheet.Cells.Item(1,2) = "OPERATING SYSTEM"
RP> $Sheet.Cells.Item(1,3) = "STATUS"
RP> $intRow = 2
RP> $WorkBook = $Sheet.UsedRange
RP> $WorkBook.Interior.ColorIndex = 5
RP> $WorkBook.Font.ColorIndex = 19
RP> $WorkBook.Font.Bold = $True
RP> $Machine = Get-Content "C:\Data\ESCG\MachineList3.Txt"
RP> $System = Get-WMIObject Win32_OperatingSystem -ComputerName $Machine
RP> ForEach($objItem in $System)
RP> {
RP> $Sheet.Cells.Item($intRow, 1) = $objItem.CSName
RP> $Sheet.Cells.Item($intRow, 2) = $objItem.Caption
RP> $path = Test-Path "\\$Machine\c$\Program Files\Symantec
RP> Antivirus\VPC32.exe"
RP>
RP> If ($path -eq "True"){
RP> $Sheet.Cells.Item($intRow, 3).Interior.ColorIndex = 4
RP> $Sheet.Cells.Item($intRow, 3) = "Good"
RP> }
RP> Elseif ($path -eq "False"){
RP> $Sheet.Cells.Item($intRow, 3).Interior.ColorIndex = 3
RP> $Sheet.Cells.Item($intRow, 3) = "Needs Attention!"
RP> }
RP> $intRow = $intRow + 1
RP> }
RP> $WorkBook.EntireColumn.AutoFit()


My System SpecsSystem Spec
Old 12-19-2007   #3 (permalink)
Shay Levi


 
 

Re: If and Elseif Conditional Logic

Rey,

If you want to test against a boolean variable ($path) for true or false
use $true or $false.
If $path equels $true then you can write just:

If ($path){...} # evaluated to $true

Instead of:

If ($path -eq $true){...}


That said, you can rewrite the if/else :

If ($path){
$Sheet.Cells.Item($intRow, 3).Interior.ColorIndex = 4
$Sheet.Cells.Item($intRow, 3) = "Good"
} else {
$Sheet.Cells.Item($intRow, 3).Interior.ColorIndex = 3
$Sheet.Cells.Item($intRow, 3) = "Needs Attention!"
}





-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> I have pieced together a small script to get the contents of a text
> file which includes several workstations. The result I am looking for
> is if the path to a particular file cannot be found, then mark as
> Needs Attention in Excel. If it is found, to mark as OK.Each time
> I run the script it imports correctly the systems that do have the
> file but it does not display the systems that do not or are possibly
> offline. Also, it never displays OK or Needs Attention. Can someone
> please enlighten my remedial powershell scripting?
>
> Thanks,
> Rey
> $Excel = New-Object -ComObject Excel.Application
> $Excel.visible = $True
> $Excel = $Excel.Workbooks.Add()
> $Sheet = $Excel.Worksheets.Item(1)
> $Sheet.Cells.Item(1,1) = "MACHINE NAME"
> $Sheet.Cells.Item(1,2) = "OPERATING SYSTEM"
> $Sheet.Cells.Item(1,3) = "STATUS"
> $intRow = 2
> $WorkBook = $Sheet.UsedRange
> $WorkBook.Interior.ColorIndex = 5
> $WorkBook.Font.ColorIndex = 19
> $WorkBook.Font.Bold = $True
> $Machine = Get-Content "C:\Data\ESCG\MachineList3.Txt"
> $System = Get-WMIObject Win32_OperatingSystem -ComputerName $Machine
> ForEach($objItem in $System)
> {
> $Sheet.Cells.Item($intRow, 1) = $objItem.CSName
> $Sheet.Cells.Item($intRow, 2) = $objItem.Caption
> $path = Test-Path "\\$Machine\c$\Program Files\Symantec
> Antivirus\VPC32.exe"
>
> If ($path -eq "True"){
> $Sheet.Cells.Item($intRow, 3).Interior.ColorIndex = 4
> $Sheet.Cells.Item($intRow, 3) = "Good"
> }
> Elseif ($path -eq "False"){
> $Sheet.Cells.Item($intRow, 3).Interior.ColorIndex = 3
> $Sheet.Cells.Item($intRow, 3) = "Needs Attention!"
> }
> $intRow = $intRow + 1
> }
> $WorkBook.EntireColumn.AutoFit()

My System SpecsSystem Spec
Old 12-19-2007   #4 (permalink)
Rey Perez


 
 

Re: If and Elseif Conditional Logic

Thank you. I am getting a better understanding of how this logic works. I get
an OK now for the systems that are online, but it does not display Needs
Attention for the others which may be offline.

Here's the error.

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT:
0x800706BA)
At C:\data\PSScripts\GetOSTypeFromAFileExportToExcel.ps1:17 char:24
+ $System = Get-WMIObject <<<< Win32_OperatingSystem -ComputerName $Machine
Get-WmiObject : Invalid parameter
At C:\data\PSScripts\GetOSTypeFromAFileExportToExcel.ps1:17 char:24
+ $System = Get-WMIObject <<<< Win32_OperatingSystem -ComputerName $Machine
True

Any suggestions?

Thanks again for the education.
Rey

"Shay Levi" wrote:
Quote:

> Rey,
>
> If you want to test against a boolean variable ($path) for true or false
> use $true or $false.
> If $path equels $true then you can write just:
>
> If ($path){...} # evaluated to $true
>
> Instead of:
>
> If ($path -eq $true){...}
>
>
> That said, you can rewrite the if/else :
>
> If ($path){
> $Sheet.Cells.Item($intRow, 3).Interior.ColorIndex = 4
> $Sheet.Cells.Item($intRow, 3) = "Good"
> } else {
> $Sheet.Cells.Item($intRow, 3).Interior.ColorIndex = 3
> $Sheet.Cells.Item($intRow, 3) = "Needs Attention!"
> }
>
>
>
>
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
>
>
>
Quote:

> > I have pieced together a small script to get the contents of a text
> > file which includes several workstations. The result I am looking for
> > is if the path to a particular file cannot be found, then mark as
> > “Needs Attention” in Excel. If it is found, to mark as “OK”.Each time
> > I run the script it imports correctly the systems that do have the
> > file but it does not display the systems that do not or are possibly
> > offline. Also, it never displays OK or Needs Attention. Can someone
> > please enlighten my remedial powershell scripting?
> >
> > Thanks,
> > Rey
> > $Excel = New-Object -ComObject Excel.Application
> > $Excel.visible = $True
> > $Excel = $Excel.Workbooks.Add()
> > $Sheet = $Excel.Worksheets.Item(1)
> > $Sheet.Cells.Item(1,1) = "MACHINE NAME"
> > $Sheet.Cells.Item(1,2) = "OPERATING SYSTEM"
> > $Sheet.Cells.Item(1,3) = "STATUS"
> > $intRow = 2
> > $WorkBook = $Sheet.UsedRange
> > $WorkBook.Interior.ColorIndex = 5
> > $WorkBook.Font.ColorIndex = 19
> > $WorkBook.Font.Bold = $True
> > $Machine = Get-Content "C:\Data\ESCG\MachineList3.Txt"
> > $System = Get-WMIObject Win32_OperatingSystem -ComputerName $Machine
> > ForEach($objItem in $System)
> > {
> > $Sheet.Cells.Item($intRow, 1) = $objItem.CSName
> > $Sheet.Cells.Item($intRow, 2) = $objItem.Caption
> > $path = Test-Path "\\$Machine\c$\Program Files\Symantec
> > Antivirus\VPC32.exe"
> >
> > If ($path -eq "True"){
> > $Sheet.Cells.Item($intRow, 3).Interior.ColorIndex = 4
> > $Sheet.Cells.Item($intRow, 3) = "Good"
> > }
> > Elseif ($path -eq "False"){
> > $Sheet.Cells.Item($intRow, 3).Interior.ColorIndex = 3
> > $Sheet.Cells.Item($intRow, 3) = "Needs Attention!"
> > }
> > $intRow = $intRow + 1
> > }
> > $WorkBook.EntireColumn.AutoFit()
>
>
>
My System SpecsSystem Spec
Old 12-19-2007   #5 (permalink)
Shay Levi


 
 

Re: If and Elseif Conditional Logic

Try to ping each machine before trying to query it:

$server="google.com" # name or ip
$ping = new-object System.Net.NetworkInformation.Ping;

if($ping.Send($server)){
write-host "online"
} else {
write-host "offline"
}


Another option is to set get-wmiobject -erro
-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> Thank you. I am getting a better understanding of how this logic
> works. I get an OK now for the systems that are online, but it does
> not display Needs Attention for the others which may be offline.
>
> Here's the error.
>
> Get-WmiObject : The RPC server is unavailable. (Exception from
> HRESULT:
> 0x800706BA)
> At C:\data\PSScripts\GetOSTypeFromAFileExportToExcel.ps1:17 char:24
> + $System = Get-WMIObject <<<< Win32_OperatingSystem -ComputerName
> $Machine
> Get-WmiObject : Invalid parameter
> At C:\data\PSScripts\GetOSTypeFromAFileExportToExcel.ps1:17 char:24
> + $System = Get-WMIObject <<<< Win32_OperatingSystem -ComputerName
> $Machine
> True
> Any suggestions?
>
> Thanks again for the education.
> Rey
> "Shay Levi" wrote:
>
Quote:

>> Rey,
>>
>> If you want to test against a boolean variable ($path) for true or
>> false
>> use $true or $false.
>> If $path equels $true then you can write just:
>> If ($path){...} # evaluated to $true
>>
>> Instead of:
>>
>> If ($path -eq $true){...}
>>
>> That said, you can rewrite the if/else :
>>
>> If ($path){
>> $Sheet.Cells.Item($intRow, 3).Interior.ColorIndex = 4
>> $Sheet.Cells.Item($intRow, 3) = "Good"
>> } else {
>> $Sheet.Cells.Item($intRow, 3).Interior.ColorIndex = 3
>> $Sheet.Cells.Item($intRow, 3) = "Needs Attention!"
>> }
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com
>> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
Quote:

>>> I have pieced together a small script to get the contents of a text
>>> file which includes several workstations. The result I am looking
>>> for is if the path to a particular file cannot be found, then mark
>>> as Needs Attention in Excel. If it is found, to mark as OK.Each
>>> time I run the script it imports correctly the systems that do have
>>> the file but it does not display the systems that do not or are
>>> possibly offline. Also, it never displays OK or Needs Attention. Can
>>> someone please enlighten my remedial powershell scripting?
>>>
>>> Thanks,
>>> Rey
>>> $Excel = New-Object -ComObject Excel.Application
>>> $Excel.visible = $True
>>> $Excel = $Excel.Workbooks.Add()
>>> $Sheet = $Excel.Worksheets.Item(1)
>>> $Sheet.Cells.Item(1,1) = "MACHINE NAME"
>>> $Sheet.Cells.Item(1,2) = "OPERATING SYSTEM"
>>> $Sheet.Cells.Item(1,3) = "STATUS"
>>> $intRow = 2
>>> $WorkBook = $Sheet.UsedRange
>>> $WorkBook.Interior.ColorIndex = 5
>>> $WorkBook.Font.ColorIndex = 19
>>> $WorkBook.Font.Bold = $True
>>> $Machine = Get-Content "C:\Data\ESCG\MachineList3.Txt"
>>> $System = Get-WMIObject Win32_OperatingSystem -ComputerName $Machine
>>> ForEach($objItem in $System)
>>> {
>>> $Sheet.Cells.Item($intRow, 1) = $objItem.CSName
>>> $Sheet.Cells.Item($intRow, 2) = $objItem.Caption
>>> $path = Test-Path "\\$Machine\c$\Program Files\Symantec
>>> Antivirus\VPC32.exe"
>>> If ($path -eq "True"){
>>> $Sheet.Cells.Item($intRow, 3).Interior.ColorIndex = 4
>>> $Sheet.Cells.Item($intRow, 3) = "Good"
>>> }
>>> Elseif ($path -eq "False"){
>>> $Sheet.Cells.Item($intRow, 3).Interior.ColorIndex = 3
>>> $Sheet.Cells.Item($intRow, 3) = "Needs Attention!"
>>> }
>>> $intRow = $intRow + 1
>>> }
>>> $WorkBook.EntireColumn.AutoFit()

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Logic is failing me. PowerShell
Sign in logic Live Mail
Re: If and Elseif Conditional Logic PowerShell
Re: If and Elseif Conditional Logic PowerShell
Logic 3 game controller Vista Games


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46