VMware Cloud Community
Ryzz
Enthusiast
Enthusiast
Jump to solution

ForEach Loop with If/ElseIf failling

Hoping someone could point out what im doing here that is wrong.

$VMServers = @("Server01", "Server02", "Server03", "Server04", "Server05", "Server06", "Server07", "Server08", "Server09", "Server10", "Server11", "Server12", "Server13", "Server14", "Server15", "Server16", "Server17", "Server18")

foreach($VM in $VMServers) {

  Write-Host "Server Name in Loop is "$VM

  notepadIF ($VM = "Server01") {

  Set-Annotation $VM -CustomAttribute "01 - System Role" -Value "DB Server SAP Node 1"}

  ElseIF ($VM = "Server02") {

  Set-Annotation $VM -CustomAttribute "01 - System Role" -Value "DB Server SAP Node 2"}

  ElseIF ($VM = "Server03") {

  Set-Annotation $VM -CustomAttribute "01 - System Role" -Value "DB Server SAP Node 3"}

  ElseIF ($VM = "Server04") {

  Set-Annotation $VM -CustomAttribute "01 - System Role" -Value "DB Server SAP Node 4"}

  ElseIF ($VM = "Server05") {

  Set-Annotation $VM -CustomAttribute "01 - System Role" -Value "HCP (CRM) Central Instance"}

  ElseIF ($VM = "Server06") {

  Set-Annotation $VM -CustomAttribute "01 - System Role" -Value "HCP (CRM) App Server 1"}

  ElseIF ($VM = "Server07") {

  Set-Annotation $VM -CustomAttribute "01 - System Role" -Value "HCP (CRM) App Server 1"}

  ElseIF ($VM = "Server08") {

  Set-Annotation $VM -CustomAttribute "01 - System Role" -Value "HJP (ADS + BW Portal) "}

  ElseIF ($VM = "Server09") {

  Set-Annotation $VM -CustomAttribute "01 - System Role" -Value "HGP (GW)"}

  ElseIF ($VM = "Server10") {

  Set-Annotation $VM -CustomAttribute "01 - System Role" -Value "HLP (Livecache)"}

  ElseIF ($VM = "Server11") {

  Set-Annotation $VM -CustomAttribute "01 - System Role" -Value "HXP (PI)"}

  ElseIF ($VM = "Server12") {

  Set-Annotation $VM -CustomAttribute "01 - System Role" -Value "HOP (BOE)"}

  ElseIF ($VM = "Server13") {

  Set-Annotation $VM -CustomAttribute "01 - System Role" -Value "Arezzo DB Node 1 (& IIS)"}

  ElseIF ($VM = "Server14") {

  Set-Annotation $VM -CustomAttribute "01 - System Role" -Value "Arezzo DB Node 2 (& IIS)"}

  ElseIF ($VM = "Server15") {

  Set-Annotation $VM -CustomAttribute "01 - System Role" -Value "OpenText Archive"}

  ElseIF ($VM = "Server16") {

  Set-Annotation $VM -CustomAttribute "01 - System Role" -Value "OpenText Content"}

  ElseIF ($VM = "Server17") {

  Set-Annotation $VM -CustomAttribute "01 - System Role" -Value "OpenText Admin"}

  Else {

  Set-Annotation $VM -CustomAttribute "01 - System Role" -Value "HBP (BW)"}

}

This i thought should be pretty self explanatory, the loop checks the name of the server in the variable, and depending on what server name, returns the correct description.

However the output shows that on every loop, it still thinks the variable = Server1, which we know isnt true by the output of the write-host.

Server Name in Loop is  Server01

AnnotatedEntity Name                 Value

--------------- ----                 -----

Server01   01 - System Role     DB Server SAP Node 1

Server Name in Loop is  Server01

Server01   01 - System Role     DB Server SAP Node 1

Server Name in Loop is  Server02

Server01   01 - System Role     DB Server SAP Node 1

Server Name in Loop is  Server03

Appreciate if someone could tell em what im doing here that is wrong.

Thanks in advance!

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
a_p_
Leadership
Leadership
Jump to solution

Replacing the "=" sign in

$VM = "ServerXX"

with

$VM -eq "ServerXX"

should work.

André

View solution in original post

0 Kudos
4 Replies
a_p_
Leadership
Leadership
Jump to solution

Replacing the "=" sign in

$VM = "ServerXX"

with

$VM -eq "ServerXX"

should work.

André

0 Kudos
Ryzz
Enthusiast
Enthusiast
Jump to solution

Arrrgh rookie mistake! :smileyblush:

That worked, thanks!

0 Kudos
mattboren
Expert
Expert
Jump to solution

Hello, Ryzz-

Like André said, using the comparison operator (-eq) instead of the assignment operator (=) was the problemo.

I am replying, though, to provide a bit different approach, so as to get away from that tedious if/elseif.  By using a data structure (hashtable, here) to hold the VM names and their corresponding CustomAttribute values, you can simplify that foreach() statement.  Like:

## create hashtable with customAttrib values for each VM name
$hshVMsInfo = @{
    Server01
= "DB Server SAP Node 1"
    Server02
= "DB Server SAP Node 2"
    Server03
= "DB Server SAP Node 3"
    Server04
= "DB Server SAP Node 4"
    Server05
= "HCP (CRM) Central Instance"
    Server06
= "HCP (CRM) App Server 1"
    Server07
= "HCP (CRM) App Server 1"
    Server08
= "HJP (ADS + BW Portal)"
    Server09
= "HGP (GW)"
    Server10
= "HLP (Livecache)"
    Server11
= "HXP (PI)"
    Server12
= "HOP (BOE)"
    Server13
= "Arezzo DB Node 1 (& IIS)"
    Server14
= "Arezzo DB Node 2 (& IIS)"
    Server15
= "OpenText Archive"
    Server16
= "OpenText Content"
    Server17
= "OpenText Admin"
    Server18
= "HBP (BW)"
}
## end hashtable

foreach($strVMName in $hshVMsInfo.Keys) { 
   
Write-Verbose -Verbose "Server Name in Loop is '$strVMName'"
   
## set the appropriate CustomAttribute value for the corresponding VM
    Set-Annotation $strVMName -CustomAttribute "01 - System Role" -Value $hshVMsInfo[$strVMName]
}
## end foreach

By having the data separate from the actual "doing" part, one can maintain / update / change / expand a bit more easily.  For instance, you could have a CSV full of VM --> Cust Attrib values, import that, and loop through the foreach on each item, with very little change to the portion of the code that does the actual work.  Again, just wanted to give an example of another way to do it.

Ryzz
Enthusiast
Enthusiast
Jump to solution

Thanks for the additional feedback. Always good to know additional ways of doing the same thing via different methods.

Cheers

Ryan

0 Kudos