Looping mess.


i'm banging head trying figure out. learning how script has been fun , frustrating challenge @ times :). hope guys/gals can me out , show me error of ways. so, i'm trying run loop matches values hash table against list that's running through foreach-object. want print $hash.key , service matches $hash.value key

key: pc-01

something stopped
something-else stopped

key: pc-02

something running
something-else running

key: pc-03

something stopped
something-else stopped


code i'm testing on

$hash = @{"pc01"="stopped";"pc02"="running";"pc03"="stopped" }      $list = get-service t* |   foreach-object {$tmp = $_.servicename;                    $tmp2 = $_.status;                       foreach      ($i in $hash.keys){          write-host $i          if( $tmp2 -eq $hash.$i ){              write-host $tmp "is" $tmp2                        }      }  }

as can see below, output quite hoping for:

thanks in advance.

from see in post assuming wanting pull @ list of 'stopped' services starting letter 't' 'pc01' , 'pc03', , list of 'running' services starting letter 't' 'pc02'.  then output results console per device.

# let's start hash table. # appear want key target device # , value filter. $hash = @{"pc01"="stopped";"pc01"="running";"pc01"="stopped"}  # next want start our loop each target device foreach($computer in $hash.keys){   # write console computer name   write-host $computer   # since $list did not appear used assigned $null   # piped in filter , piped in output.   $null = get-service -displayname "t*" -computername $computer | where-object {$_.status -eq $hash.$computer} | foreach-object {write-host $_.servicename + " " + $_.status}    }

i hope helps



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

WIMMount (HSM) causing cluster storage to go redirected (2012r2 DC)

Failed to delete the test record dcdiag-test-record in zone test.com

Azure MFA with Azure AD and RDS