Quantcast
Channel: /r/powershell – How to Code .NET
Viewing all articles
Browse latest Browse all 8793

Reacting to Boolean values in a variable with if, else, and elseif statements.

$
0
0

The whole point of this is to see if there are any dead/non-responsive entries list on the server. I’ve got this script that basically:

  • Gets a list of printers for my location from our printer server.

  • Alphabetizes the list and spits it out to a TXT.

  • Takes the IP address that’s listed on the server and pings each address.

  • The -Quiet parameter makes Test-Connection return either a True or False value for a successful or unsuccessful ping respectively.

  • (Still needs to be written) It reacts one way to a successful ping and another way to an unsuccessful ping.

FYI, the whole “Ping is good/ping is bad” thing is my attempt at testing to see I could get the if statements to work.

The Problem: I keep getting “Ping is bad, Ping is bad, Ping is bad…” despite the fact that I know some of these pings are successful.

How can I get the if statements to react to “true” and “false” values? Google isn’t helping much.

Side question: There’s got to be an easier way to put a big code block in than indenting every line one by one. Right?

#________________________________________________________________ # Set the "Printer List.txt" filepath. $DESKTOP = "$env:USERPROFILEDesktop" # Set the human-readable printer list filepath. $FILE = "$DESKTOPPrinter List.txt" # Set the printer server address. $SERVER = "\PrinterServer01" #________________________________________________________________ # Collect a printer list. Output it to a human-readable file to the user's Desktop. # NOTE: The correct static IP address is listed in the Comment column. $PRINTER_LIST = Get-Printer -Name *MyLocationName* -ComputerName $SERVER | Select-Object -Property Name,Location,PortName,Comment | Sort-Object -Property PortName $PRINTER_LIST | Out-File -FilePath $FILE # Ping each printer once and list the results in a file. $DEVICE = 1 Clear-Variable -Name PING_STATUS $IP_ADDRESS = $PRINTER_LIST | Select-Object -Property PortName -ExpandProperty PortName | ForEach-Object -Process { Write-Host Pinging device `#$DEVICE... Test-Connection -ComputerName $_ -Count 1 -Quiet -OutVariable $PING_STATUS | Out-Host If ($PING_STATUS = $true) {Write-Host Ping is good} ElseIf ($PING_STATUS = $false) {Write-Host Ping is bad} $DEVICE = $($DEVICE)+1 Clear-Variable -Name PING_STATUS } 

submitted by /u/Troubleshooter5000
[link] [comments]

The post Reacting to Boolean values in a variable with if, else, and elseif statements. appeared first on How to Code .NET.


Viewing all articles
Browse latest Browse all 8793

Latest Images

Trending Articles



Latest Images