I wanted to start a new thread on this since I'm confused about how/why PowerShell works. This post is not looking for an answer to an issue but to understand why PowerShell is doing what it's doing.
$pc = "mg2014"
foreach ($machine in $pc) {$getip = (Test-Connection $machine -Count 1).IPV4Address.IPAddressToString;
if ($getip -LIKE '*156*') {$location = (Get-ADComputer $machine -Properties CanonicalName).CanonicalName; if ($location -contains 'Main Campus') {EXIT} }}
When I run the above code it returns nothing, which is correct. My machine, mg2014, is on the .156. vLan and is in an OU that contains "Main Campus" (note: same result if I use -LIKE instead of -contains). So everything is in order and it runs the EXIT command.
Now, the below code returns "Wrong OU" and all I've changed is the {Write-host "Wrong OU"} AFTER the EXIT command. Why? Why does adding this one command after the exit change the output?
$pc = "mg2014"
foreach ($machine in $pc) {$getip = (Test-Connection $machine -Count 1).IPV4Address.IPAddressToString;
if ($getip -LIKE '*156*') {$location = (Get-ADComputer $machine -Properties CanonicalName).CanonicalName; if ($location -LIKE 'Main Campus') {EXIT} ELSE {Write-host "Wrong OU"}}}