I’m struggling with if/elseif/else within a foreach loop. The basic idea of the script is to check to see if SSH is enabled on clusters, and if it is, tell me about it. If it’s not enabled, things are good, nothing to do. There are some clusters that are with the implementations team and I don’t care about them… they’re not under my purview yet. I connect to the API, get a list of clusters, then run a “check firewall” script against them. The output of that script is saved to a text file. I then use “select-string” to look for a specific text string…depending on what I find, I do “something”.<\/p>\n
The problem I have is that while the first “if” statement works exactly as I’d expect, whatever I put for the second “elseif” takes over…all clusters are either compliant or not compliant. Script is below (I’ve generalized a few things for privacy)<\/p>\n
#Variables. Edit these to fit the environment\n#Set the path to the script\n$path=\"C:\\\"\n\n#Who is the email being sent to?\n$email=\"\"\n\n#Which email server should be used to send email?\n$emailserver=\"\"\n\n#Set the directory to store the transcripts in\n$transcript=\"C:\\Transcripts\" \n\n#Get a list of clusters still with the Implementation team so we don't check them for compliance\n$implementations= @(Get-Content -Path 'C:\\Implementationclusters.txt')\n\n### Run the script ####\n#Change to the script directory\ncd $path\n\n#Clean up the results from last time\nRemove-item $transcript\\*.txt\n\n# Source the API\n. ./api.ps1\n\n#Connect to the API\napiauth\n\n#Get a list of all of the current clusters\n$clusters =@(Clusters | select -Property name)\n\n#Page through the clusters, one at a time and run the firewall tool script against them\n foreach ($cluster in $clusters.name) {\n #Start the transcript\n Start-Transcript -Path $transcript\\$cluster.txt\n C:\\firewalltool.ps1 -vip site.com -profileName ssh -clusterName $cluster \n#Stop the transcript\n Stop-Transcript\n\n}\n #Examine the results files. \n foreach ($cluster in $clusters.name) { \n \n if ($implementations -contains $cluster) {\n Write-Host \"$cluster is with Implementations team\" \n } \n elseif (\n Select-String -path $transcript\\*.txt -Pattern 'allow' -simplematch ) \n {Write-Host \"$cluster is not compliant!\" -ForegroundColor White -BackgroundColor Red\n } \n elseif (Select-String -path $transcript\\*.txt -Pattern 'All IP Addresses(*) (deny)' -simplematch ) \n {Write-Host \"$cluster is compliant!\" \n } \n else {Write-Host \"$cluster is not compliant\" \n }\n \n }\n<\/code><\/pre>","upvoteCount":3,"answerCount":12,"datePublished":"2025-06-16T17:09:51.333Z","author":{"@type":"Person","name":"Carl-Holzhauer","url":"https://community.spiceworks.com/u/Carl-Holzhauer"},"acceptedAnswer":{"@type":"Answer","text":"
Advertisement
But you are checking ALL files in the folder<\/p>\n
Select-String -path \"$transcript\\*.txt\" -Pattern 'Allow' -simplematch\n<\/code><\/pre>\nrather than the one that matches the cluster name<\/p>\n
Select-String -path \"$transcript\\$cluster.txt\" -Pattern 'Allow' -simplematch\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2025-06-16T19:14:10.644Z","url":"https://community.spiceworks.com/t/help-with-if-elseif-else/1215659/10","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},"suggestedAnswer":[{"@type":"Answer","text":"I’m struggling with if/elseif/else within a foreach loop. The basic idea of the script is to check to see if SSH is enabled on clusters, and if it is, tell me about it. If it’s not enabled, things are good, nothing to do. There are some clusters that are with the implementations team and I don’t care about them… they’re not under my purview yet. I connect to the API, get a list of clusters, then run a “check firewall” script against them. The output of that script is saved to a text file. I then use “select-string” to look for a specific text string…depending on what I find, I do “something”.<\/p>\n
The problem I have is that while the first “if” statement works exactly as I’d expect, whatever I put for the second “elseif” takes over…all clusters are either compliant or not compliant. Script is below (I’ve generalized a few things for privacy)<\/p>\n
#Variables. Edit these to fit the environment\n#Set the path to the script\n$path=\"C:\\\"\n\n#Who is the email being sent to?\n$email=\"\"\n\n#Which email server should be used to send email?\n$emailserver=\"\"\n\n#Set the directory to store the transcripts in\n$transcript=\"C:\\Transcripts\" \n\n#Get a list of clusters still with the Implementation team so we don't check them for compliance\n$implementations= @(Get-Content -Path 'C:\\Implementationclusters.txt')\n\n### Run the script ####\n#Change to the script directory\ncd $path\n\n#Clean up the results from last time\nRemove-item $transcript\\*.txt\n\n# Source the API\n. ./api.ps1\n\n#Connect to the API\napiauth\n\n#Get a list of all of the current clusters\n$clusters =@(Clusters | select -Property name)\n\n#Page through the clusters, one at a time and run the firewall tool script against them\n foreach ($cluster in $clusters.name) {\n #Start the transcript\n Start-Transcript -Path $transcript\\$cluster.txt\n C:\\firewalltool.ps1 -vip site.com -profileName ssh -clusterName $cluster \n#Stop the transcript\n Stop-Transcript\n\n}\n #Examine the results files. \n foreach ($cluster in $clusters.name) { \n \n if ($implementations -contains $cluster) {\n Write-Host \"$cluster is with Implementations team\" \n } \n elseif (\n Select-String -path $transcript\\*.txt -Pattern 'allow' -simplematch ) \n {Write-Host \"$cluster is not compliant!\" -ForegroundColor White -BackgroundColor Red\n } \n elseif (Select-String -path $transcript\\*.txt -Pattern 'All IP Addresses(*) (deny)' -simplematch ) \n {Write-Host \"$cluster is compliant!\" \n } \n else {Write-Host \"$cluster is not compliant\" \n }\n \n }\n<\/code><\/pre>","upvoteCount":3,"datePublished":"2025-06-16T17:09:51.400Z","url":"https://community.spiceworks.com/t/help-with-if-elseif-else/1215659/1","author":{"@type":"Person","name":"Carl-Holzhauer","url":"https://community.spiceworks.com/u/Carl-Holzhauer"}},{"@type":"Answer","text":"Hm do those ‘Select-String’ cmdlets actually return what you expect them to if you test them individually?<\/p>","upvoteCount":1,"datePublished":"2025-06-16T17:30:31.020Z","url":"https://community.spiceworks.com/t/help-with-if-elseif-else/1215659/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
They do. Exactly. They just seem to fall apart when I put them in the loop<\/p>","upvoteCount":1,"datePublished":"2025-06-16T17:36:57.151Z","url":"https://community.spiceworks.com/t/help-with-if-elseif-else/1215659/3","author":{"@type":"Person","name":"Carl-Holzhauer","url":"https://community.spiceworks.com/u/Carl-Holzhauer"}},{"@type":"Answer","text":"
If there are multiple .txt files in the $transcript directory when this runs, you only need one file to contain “Allow” for that elseif block to always be true regardless of the cluster name. You will need to parse out the data that corresponds to the current cluster or write your files to be one file per cluster.<\/p>","upvoteCount":3,"datePublished":"2025-06-16T17:58:04.655Z","url":"https://community.spiceworks.com/t/help-with-if-elseif-else/1215659/4","author":{"@type":"Person","name":"adminofthings","url":"https://community.spiceworks.com/u/adminofthings"}},{"@type":"Answer","text":"
I mean if ANY of the text files ($transcript\\*.txt) in the transcript folder have ‘allow’ in them it would match and stop evaluating.<\/p>","upvoteCount":4,"datePublished":"2025-06-16T17:58:38.679Z","url":"https://community.spiceworks.com/t/help-with-if-elseif-else/1215659/5","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
I do have one file per cluster…that file only contains the results for that cluster, it does not contain results for any other cluster.<\/p>","upvoteCount":1,"datePublished":"2025-06-16T18:27:36.293Z","url":"https://community.spiceworks.com/t/help-with-if-elseif-else/1215659/6","author":{"@type":"Person","name":"Carl-Holzhauer","url":"https://community.spiceworks.com/u/Carl-Holzhauer"}},{"@type":"Answer","text":"
I for sure have one in there with “allow” in it as a test case. How would I deal with that?<\/p>","upvoteCount":1,"datePublished":"2025-06-16T18:28:16.341Z","url":"https://community.spiceworks.com/t/help-with-if-elseif-else/1215659/7","author":{"@type":"Person","name":"Carl-Holzhauer","url":"https://community.spiceworks.com/u/Carl-Holzhauer"}},{"@type":"Answer","text":"
As AdminOfThings suggested, you can try to create one file per cluster and give the transcript the same name as the cluster (e.g. ‘transcript-cluster123.txt’) and then in your foreach, you just have it check the corresponding transcript, rather than ALL files in the folder.
\nOf if the transcript files have the cluster name inside, you can try to parse that out and then have it it check or clustername AND allow, or clustername AND deny, or whatever you are trying to match.<\/p>\n
EDIT: added example
\ne.g.<\/p>\n
Select-String -path \"$transcript\\$cluster.txt\" -Pattern 'allow' -simplematch\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2025-06-16T18:42:33.484Z","url":"https://community.spiceworks.com/t/help-with-if-elseif-else/1215659/8","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"That’s what I have now…I have one text file per cluster and the text file is named $cluster.txt.<\/p>","upvoteCount":0,"datePublished":"2025-06-16T19:00:19.841Z","url":"https://community.spiceworks.com/t/help-with-if-elseif-else/1215659/9","author":{"@type":"Person","name":"Carl-Holzhauer","url":"https://community.spiceworks.com/u/Carl-Holzhauer"}},{"@type":"Answer","text":"
Ah, I see what you’re saying. I\"ll try that<\/p>","upvoteCount":0,"datePublished":"2025-06-16T19:16:49.220Z","url":"https://community.spiceworks.com/t/help-with-if-elseif-else/1215659/11","author":{"@type":"Person","name":"Carl-Holzhauer","url":"https://community.spiceworks.com/u/Carl-Holzhauer"}},{"@type":"Answer","text":"
That was exactly my issue. Thank you for the help, it was driving me nuts<\/p>","upvoteCount":0,"datePublished":"2025-06-17T11:54:07.962Z","url":"https://community.spiceworks.com/t/help-with-if-elseif-else/1215659/12","author":{"@type":"Person","name":"Carl-Holzhauer","url":"https://community.spiceworks.com/u/Carl-Holzhauer"}}]}}