SOLVED

How to run Powershell command in Loop

%3CLINGO-SUB%20id%3D%22lingo-sub-1186263%22%20slang%3D%22en-US%22%3EHow%20to%20run%20Powershell%20command%20in%20Loop%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1186263%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20new%20to%20Powershell%20script%20and%20Need%20your%20help%20on%20below%20requirement.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20have%20powershell%20script%20displaying%20last%20last%203%20commands%20and%20output%20of%20my%20powershell%20script.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBelow%20last%203%20command%20lines%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24FinalResource%3D%24FinalResource1%20%7C%20Where-Object%7B%24_.ResourceType%20-ne%20%24MetricType%7D%3C%2FP%3E%3CP%3EWrite-Output%20%24FinalResource%3C%2FP%3E%3CP%3ENew-AzResourceLock%20-LockLevel%20CanNotDelete%20-LockName%20Delete_Lock%20-ResourceName%20%24FinalResource.Name%20-ResourceType%20%24FinalResource.ResourceType%20-ResourceGroupName%20xxxxxx%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBelow%20is%20Output%20of%20my%20powershell%20script%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22resourcelock.PNG%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F172430i886EBE4CEE7EEB14%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22resourcelock.PNG%22%20alt%3D%22resourcelock.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20getting%20list%20of%20resources%20which%20are%20not%20having%20LOCKS%20on%20Azure%20after%20that%20I%20am%20applying%20lock%20on%20each%20resource%20by%20using%20above%20command.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EScript%20is%20running%20fine%20when%20I%20am%20passing%20single%20resource%20but%20script%20is%20failing%20when%20I%20am%20passing%20all%20resources.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHow%20to%20run%20last%20command%20in%20loop%20by%20passing%20one%20by%20one%20resource%20so%20that%20lock%20command%20will%20apply%20on%20all%20resources.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%2C%3C%2FP%3E%3CP%3EBrahma%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1186263%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%20Commands%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%20ISE%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1187682%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20run%20Powershell%20command%20in%20Loop%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1187682%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F554371%22%20target%3D%22_blank%22%3E%40Brahmaiah%3C%2FA%3E%26nbsp%3BYou%20can%20use%20Foreach%20and%20set%20the%20lock%20for%20each%20member.%20Let%20me%20know%20if%20you%20have%20any%20questions.%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%24FinalResource%20%3D%20%24FinalResource1%20%7C%20Where-Object%7B%24_.ResourceType%20-ne%20%24MetricType%7D%0A%0AWrite-Output%20%24FinalResource%0A%24Counter%20%3D%20%24FinalResource.count%0A%0AForeach(%24Item%20in%20%24FinalResource)%0A%20%7B%0A%20%20Write-Host%20%22(%24Counter)%20Locking%20Resource%3A%20%24(%24Item.Name)%22%0A%20%20New-AzResourceLock%20-LockLevel%20CanNotDelete%20-LockName%20Delete_Lock%20-ResourceName%20%24Item.Name%20-ResourceType%20%24Item.ResourceType%20-ResourceGroupName%20%24Item.ResourceGroupName%0A%20%20%24Counter--%0A%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%3CBR%20%2F%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1188405%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20run%20Powershell%20command%20in%20Loop%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1188405%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F89674%22%20target%3D%22_blank%22%3E%40Erick%20A.%20Moreno%20R.%3C%2FA%3E%26nbsp%3B%20Thank%20you%20so%20much%20Sir.%20It%20is%20working%20perfectly.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20you%20also%20help%20me%20how%20to%20avoid%20confirm%20option%20while%20running%20the%20script%2C%20I%20have%20to%20click%20Yes%20for%20each%20resource%20to%20execute%20command.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBelow%20is%20screen%20shot.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22resourcelock.PNG%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F172649i8B814B78639B397D%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22resourcelock.PNG%22%20alt%3D%22resourcelock.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1188415%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20run%20Powershell%20command%20in%20Loop%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1188415%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F554371%22%20target%3D%22_blank%22%3E%40Brahmaiah%3C%2FA%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F89674%22%20target%3D%22_blank%22%3EErick%20A.%20Moreno%20R.%3C%2FA%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F554371%22%20target%3D%22_blank%22%3E%40Brahmaiah%3C%2FA%3E%3C%2FP%3E%3CP%3E-Force%20worked%20by%20keeping%20end%20of%20statement.%20thanks%20for%20all%20your%20help%2C%20your%20help%20saved%20me%20lot%20of%20time%20and%20work.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1189880%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20run%20Powershell%20command%20in%20Loop%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1189880%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F554371%22%20target%3D%22_blank%22%3E%40Brahmaiah%3C%2FA%3E%26nbsp%3BGlad%20to%20help%2C%20for%20the%20auto-confirm%20you%20can%20use%20the%20switch%20%3CSTRONG%3E-confirm%3A%24false%26nbsp%3B%3C%2FSTRONG%3Eat%20the%20end%20of%20the%20lock%20command%2C%20let%20me%20know%20if%20you%20need%20further%20assistance.%26nbsp%3B%3CSTRONG%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3ERegards%3C%2FP%3E%3CP%3EErick%20Moreno%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

Hi,

 

I am new to Powershell script and Need your help on below requirement.

 

I am have powershell script displaying last last 3 commands and output of my powershell script.

 

Below last 3 command lines:

 

$FinalResource=$FinalResource1 | Where-Object{$_.ResourceType -ne $MetricType}

Write-Output $FinalResource

New-AzResourceLock -LockLevel CanNotDelete -LockName Delete_Lock -ResourceName $FinalResource.Name -ResourceType $FinalResource.ResourceType -ResourceGroupName xxxxxx

 

Below is Output of my powershell script:

 

resourcelock.PNG

 

I am getting list of resources which are not having LOCKS on Azure after that I am applying lock on each resource by using above command.

 

Script is running fine when I am passing single resource but script is failing when I am passing all resources.

 

How to run last command in loop by passing one by one resource so that lock command will apply on all resources.

 

Thanks,

Brahma

4 Replies
Highlighted
Best Response confirmed by Brahmaiah (Occasional Contributor)
Solution

@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--
 }


 

Highlighted

@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.

resourcelock.PNG

 

Thanks

Highlighted

@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.

Highlighted

@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