I'm having some trouble with my PowerShell script that uses if/else statements. I have a directory full of text files, and I'm using "select-string" to check for certain conditions related to SSH access on my clusters. Essentially, I want to confirm if SSH is allowed on my clusters: if it is allowed, I want to show a warning, and if anything other than "All IP Addresses(*) (deny)" is present, I want it to display as "Not Compliant." Here's a snippet of my code:
```powershell
$implementations = @(Get-Content -Path 'C:pathImplementationclusters.txt')
foreach ($cluster in $clusters.name) {
if ($implementations -contains $cluster) {
Write-Host "$cluster is with Implementations team"
}
elseif (Select-String -Path $transcript*.txt -Pattern 'All IP Addresses(*) (deny)' -SimpleMatch) {
Write-Host "$cluster is compliant!"
}
elseif (Select-String -Path $transcript*.txt -Pattern '(*allow)' -SimpleMatch) {
Write-Host "$cluster is not compliant!" -ForegroundColor White -BackgroundColor Red
}
else {
Write-Host "$cluster is not compliant"
}
}
```
The issue arises when I allow SSH on a test cluster; the script still labels the cluster as compliant. The relevant output in the text file is "All IP Addresses(*) (allow)." I think my problem lies in the order of the checks or the specifics of what I'm searching for. Any insights?
4 Answers
Make sure to check what’s in `$implementations`. It could be a key part of the issue. Also, stepping through your code while debugging will give you more clarity. And just to clarify, what exactly is in `$clusters`? If `$cluster` is just a name and not an actual cluster object, that might also be tripping you up.
It looks like you need to include the `-Quiet` switch in your Select-String calls. This way, it returns a boolean value instead of match objects. Here's a modified version of your code:
```powershell
$implementations = @(Get-Content -Path 'C:pathImplementationclusters.txt')
foreach ($cluster in $clusters.name) {
if ($implementations -contains $cluster) {
Write-Host "$cluster is with Implementations team"
}
elseif (Select-String -Path "$transcript*.txt" -Pattern 'All IP Addresses(*) (deny)' -Quiet) {
Write-Host "$cluster is compliant!" -ForegroundColor Green
}
elseif (Select-String -Path "$transcript*.txt" -Pattern '(*allow)' -Quiet) {
Write-Host "$cluster is not compliant!" -ForegroundColor White -BackgroundColor Red
}
else {
Write-Host "$cluster is not compliant" -ForegroundColor Red
}
}
```
Try this version out!
I would suggest you debug your script! Step through it line by line to see if your results match your expectations. This should help you identify where things are going wrong pretty quickly.
You might want to adjust your foreach loop to this format: `foreach ($cluster in $clusters) {...}` Then start your if statement with `if ($implementations -contains $cluster.name) ...`. This might make your logic flow better.
Related Questions
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically
[Centos] Delete All Files And Folders That Contain a String