Forum Discussion
How to run Powershell command in Loop
- Feb 21, 2020
Brahmaiah You can use Foreach and set the lock for each member. Let me know if you have any questions.
$FinalResource = $FinalResource1 | Where-Object{$_.ResourceType -ne $MetricType} Write-Output $FinalResource $Counter = $FinalResource.count Foreach($Item in $FinalResource) { Write-Host "($Counter) Locking Resource: $($Item.Name)" New-AzResourceLock -LockLevel CanNotDelete -LockName Delete_Lock -ResourceName $Item.Name -ResourceType $Item.ResourceType -ResourceGroupName $Item.ResourceGroupName $Counter-- }
Brahmaiah You can use Foreach and set the lock for each member. Let me know if you have any questions.
$FinalResource = $FinalResource1 | Where-Object{$_.ResourceType -ne $MetricType}
Write-Output $FinalResource
$Counter = $FinalResource.count
Foreach($Item in $FinalResource)
{
Write-Host "($Counter) Locking Resource: $($Item.Name)"
New-AzResourceLock -LockLevel CanNotDelete -LockName Delete_Lock -ResourceName $Item.Name -ResourceType $Item.ResourceType -ResourceGroupName $Item.ResourceGroupName
$Counter--
}
- BrahmaiahFeb 22, 2020Brass Contributor
Erick A. Moreno R. Thank you so much Sir. It is working perfectly.
Can you also help me how to avoid confirm option while running the script, I have to click Yes for each resource to execute command.
Below is screen shot.
Thanks
- Erick A. Moreno R.Feb 23, 2020Iron Contributor
Brahmaiah Glad to help, for the auto-confirm you can use the switch -confirm:$false at the end of the lock command, let me know if you need further assistance.
Regards
Erick Moreno
- BrahmaiahFeb 22, 2020Brass Contributor
BrahmaiahErick A. Moreno R.Brahmaiah
-Force worked by keeping end of statement. thanks for all your help, your help saved me lot of time and work.