I have a PS script that runs a script in a list of VMs within some subscription and gets a state of some agents and exports them on a CSV file.
The problem is that the script keeps iterating in loop after going through all the subscriptions an keeps running the script again on all the VMs.
Here is my script:
# List of your Azure subscription IDs$subscriptionIds = @("c0978b9d-b809-xxxxxx-xxxx-391ceb2cfdba","c0978b9d-b809-xxxxxx-xxxx-391ceb2cfdba")$scriptPath = "C:\Users\xxxxx\Desktop\Windows\CHECK_WINDOWS.ps1"$outputFile = "C:\Users\xxxxx\Desktop\Windows\Outputfiles\StateWindows.csv"# Set table header$line1 = @""Server Name","GPO_HARMOR","BIOMEDIC_AGENT","EDWARD_STATUS","SENSOR_STATUS","SENTINEL_STATUS","VM_STATE""@# Write table header$line1 | Out-File -FilePath $outputFileforeach ($subscriptionId in $subscriptionIds) { # Set the subscription context Set-AzContext -SubscriptionId $subscriptionId # Get resource groups with the specified tag $resourceGroups = Get-AzResourceGroup | Where-Object { $_.Tags -ne $null -and $_.Tags['ApplicationName'] -like "*US*" } foreach ($group in $resourceGroups) { # Disable RG lock $rgLock = Get-AzResourceLock -ResourceGroupName $group.ResourceGroupName -ErrorAction SilentlyContinue if ($rgLock) { Remove-AzResourceLock -LockId $rgLock.LockId -Force Write-Host "Removed existing lock on $($group.ResourceGroupName)" } # Get all the VMs in the subscription $vms = Get-AzVM # Loop through each VM and run the script if it's a Windows VM foreach ($vm in $vms) { # Disable VM lock $lock = Get-AzResourceLock -ResourceName $vm.Name -ResourceType "Microsoft.Compute/virtualMachines" -ResourceGroupName $group.ResourceGroupName -ErrorAction SilentlyContinue if ($lock) { Remove-AzResourceLock -LockId $lock.LockId -Force Write-Host "Removed existing lock on $($vm.Name) in $($group.ResourceGroupName)" } $statuscheck = Get-AzVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name -Status # Check if the VM is ON and running a Windows OS if ($vm.StorageProfile.OSDisk.OSType -eq "Windows" -And $statuscheck.Statuses.DisplayStatus[1] -eq "VM running" -And $vm.Tags['ApplicationName'] -like "*OSS.NORAM.US*") { # Run the VARIASUIT_CHECK_WINDOWS.ps1 script on the VM Write-Host ">>>>>>> Getting state of $($vm.Name)" $scriptResult = Invoke-AzVMRunCommand -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name -CommandId 'RunPowerShellScript' -ScriptPath $scriptPath # Parse file to get state $state = @($scriptResult | Out-String -Stream) -match '^".*AZ-.*"$' $state >> $outputFile Write-Host "$($vm.Name) is RUNNING and it's $state" } elseif ($vm.StorageProfile.OSDisk.OSType -eq "Windows" -And $statuscheck.Statuses.DisplayStatus[1] -ne "VM running" -And $vm.Tags['ApplicationName'] -like "*OSS.NORAM.US*") { # "`""+$vm.Name+"`","+$state+"`""+"`""+$statuscheck.Statuses.DisplayStatus[1]+"`"">> $outputFile"`""+$vm.Name+"`","+"`"-`",`"-`",`"-`",`"-`",`"-`""+",`""+$statuscheck.Statuses.DisplayStatus[1]+"`"" >> $outputFile Write-Host "$($vm.Name) is STOPPED and it's $state" } elseif ($vm.Tags['ApplicationName'] -notlike "*OSS.NORAM.US*") { Write-Host "Skipping $($vm.Name) not a NORAM VM" } else { Write-Host "Skipping $($vm.Name) as it is not running a Windows OS" } # $scriptResult | Out-File $outputFileTemp # # Enable lock # New-AzResourceLock -LockLevel CanNotDelete -LockName "PreventDel" -LockNotes "PreventDel" -ResourceName $vm.Name -ResourceType "Microsoft.Compute/virtualMachines" -ResourceGroupName $group.ResourceGroupName -Force # Write-Host "Activated lock on $($vm.Name) in $($group.ResourceGroupName)" } # Enable RG lock New-AzResourceLock -LockLevel CanNotDelete -LockName "PreventDel" -LockNotes "PreventDel" -ResourceGroupName $group.ResourceGroupName -Force Write-Host "Activated lock on $($group.ResourceGroupName)" # $results | Export-Csv -Path C:\Users\joyw\Desktop\tag.csv -NoTypeInformation } Write-Host "VM States of the subscription $subscriptionId are done"}
Any help on how to break out of the loops once script go through all resources once? Thanks!