Unable to script using Multi-threading

%3CLINGO-SUB%20id%3D%22lingo-sub-1576148%22%20slang%3D%22en-US%22%3EUnable%20to%20script%20using%20Multi-threading%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1576148%22%20slang%3D%22en-US%22%3E%3CUL%3E%3CLI%3EI%20have%20one%20Csv%20and%20there%20are%2010%20VM%20and%20i%20want%20to%20run%20this%20script%20to%20all%20the%20vms%20in%20same%20time%20(using%26nbsp%3B%3CSPAN%3E%26nbsp%3BMulti-threading%26nbsp%3B%20concept%20or%20any%20other%20method%3C%2FSPAN%3E)%20not%20one%20by%20one.%3C%2FLI%3E%3CLI%3Ebecause%20if%20it%20will%20run%20one%20by%20one%20then%20take%20a%20lots%20of%20time.%3C%2FLI%3E%3C%2FUL%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24VMNAME%3DImport-Csv%20-Path%20%22C%3A%5CTest_powershell_03-07-20%5C09-08-20%5Cdemovm.csv%22%20%0Aforeach(%24vm%20in%20%24VMNAME)%0A%7B%0A%24flag%3D%22false%22%0A%24Subscriptionid%20%3D%24_.Subscriptionid%0A%24Name%20%3D%24_.VMNAME%0A%24ResourceGroupName%20%3D%20%24_.RGN%0A%24Location%3D%24_.Location%0ASelect-AzureRmSubscription%20-SubscriptionId%20%0A%24Subscriptionid%0A%24VMNAME%20%3D%20Get-azurermvm%20-status%20-Name%20%24Name%20-ResourceGroupName%20%24ResourceGroupName%20%24Status%20%20%20%3D%20%24VMNAME.Statuses%5B1%5D.DisplayStatus%0Aif(%24Status%20-eq%20%22vm%20deallocated%22)%0A%7B%20%0Astart-azurermvm%20-Name%20%24_.VMNAME%20-ResourceGroupName%20%24_.RGN%20%0A%24flag%3D%22true%22%20%20%0A%7D%0Aif(%24flag%20-eq%20%22true%22)%0A%7B%0Astop-azurermvm%20-Name%20%24_.VMNAME%20-ResourceGroupName%20%24_.RGN%20-Force%20%0A%7D%20%20%0Acatch%20%7B%20%20%20%0AWrite-Output%20%24Error%5B0%5D.Exception.Message%0A%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1576148%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowershell%20for%20Azure%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1577711%22%20slang%3D%22en-US%22%3ERe%3A%20Unable%20to%20script%20using%20Multi-threading%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1577711%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F237571%22%20target%3D%22_blank%22%3E%40Pradeep%20MISHRA%3C%2FA%3E%26nbsp%3B-%20You%20can%20use%20-AsJob%20option%20which%20will%20create%20each%20execution%20as%20a%20job%20for%20you%20and%20will%20run%20in%20the%20background%3C%2FP%3E%3CP%3EYou%20can%20then%20track%20the%20status%20of%20the%20Job%20based%20on%20the%20object%20returned.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1580708%22%20slang%3D%22en-US%22%3ERe%3A%20Unable%20to%20script%20using%20Multi-threading%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1580708%22%20slang%3D%22en-US%22%3E%3CP%3EHello%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F237571%22%20target%3D%22_blank%22%3E%40Pradeep%20MISHRA%3C%2FA%3E%3C%2FP%3E%3CP%3EIf%20you%20are%20able%20to%20use%20PowerShell%207%20you%20may%20want%20to%20consider%20the%20new%20parallel%20option%20for%20the%20foreach-object%20Cmdlet.%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fscripting%2Fwhats-new%2Fwhat-s-new-in-powershell-70%3Fview%3Dpowershell-7%23parallel-execution-added-to-foreach-object%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fscripting%2Fwhats-new%2Fwhat-s-new-in-powershell-70%3Fview%3Dpowershell-7%23parallel-execution-added-to-foreach-object%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20hope%20that%20helps%3C%2FP%3E%3CP%3ECheers%3CBR%20%2F%3ERolf%3CBR%20%2F%3E%3CBR%20%2F%3E%23MCT%20%23LearnWithRolf%20%23TheCloud42%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1590478%22%20slang%3D%22en-US%22%3ERe%3A%20Unable%20to%20script%20using%20Multi-threading%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1590478%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F659330%22%20target%3D%22_blank%22%3E%40Rolf-42%3C%2FA%3E%26nbsp%3B%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20using%20below%20powershell%20version%20and%20unable%20to%20using%20workflow%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPS%20C%3A%5CUsers%5CAdmin%26gt%3B%20Get-Host%20%7C%20Select-Object%20Version%3C%2FP%3E%3CP%3EVersion%3CBR%20%2F%3E-------%3CBR%20%2F%3E5.1.19041.1%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20you%20please%20share%20a%20sample%20%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1591588%22%20slang%3D%22en-US%22%3ERe%3A%20Unable%20to%20script%20using%20Multi-threading%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1591588%22%20slang%3D%22en-US%22%3E%3CP%3EHello%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F237571%22%20target%3D%22_blank%22%3E%40Pradeep%20MISHRA%3C%2FA%3E%3C%2FP%3E%3CP%3ESince%20your%20PowerShell%20version%20is%20to%20low%20what%20about%20running%20PowerShell%207%20in%20a%20container%3F%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fwww.thomasmaurer.ch%2F2020%2F03%2Frun-azure-powershell-in-a-docker-container%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.thomasmaurer.ch%2F2020%2F03%2Frun-azure-powershell-in-a-docker-container%2F%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20that%20is%20not%20possible%20the%20background%20job%20feature%20is%20your%20approach%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fscripting%2Fdeveloper%2Fcmdlet%2Fbackground-jobs%3Fview%3Dpowershell-5.1%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fscripting%2Fdeveloper%2Fcmdlet%2Fbackground-jobs%3Fview%3Dpowershell-5.1%3C%2FA%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22hljs-pscommand%22%3EStart-Job%3C%2FSPAN%3E%3CSPAN%20class%3D%22hljs-parameter%22%3E%20-ScriptBlock%3C%2FSPAN%3E%3CSPAN%3E%20%7B%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22hljs-pscommand%22%3EGet-Process%3C%2FSPAN%3E%3CSPAN%20class%3D%22hljs-parameter%22%3E%20-Name%3C%2FSPAN%3E%3CSPAN%3E%20powershell%20%7D%20is%20the%20example%20you%20are%20looking%20for.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3EThe%20example%20is%20taken%20from%20the%20Start-Job%20documentation%20here%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2FMicrosoft.PowerShell.Core%2FStart-Job%3Fview%3Dpowershell-5.1%23examples%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2FMicrosoft.PowerShell.Core%2FStart-Job%3Fview%3Dpowershell-5.1%23examples%3C%2FA%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3ECheers%3CBR%20%2F%3E%3CBR%20%2F%3ERolf%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%23MCT%20%23LearnWithRolf%20%23TheCloud42%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor
  • I have one Csv and there are 10 VM and i want to run this script to all the vms in same time (using  Multi-threading  concept or any other method) not one by one.
  • because if it will run one by one then take a lots of time.

 

 

$VMNAME=Import-Csv -Path "C:\Test_powershell_03-07-20\09-08-20\demovm.csv" 
foreach($vm in $VMNAME)
{
$flag="false"
$Subscriptionid =$_.Subscriptionid
$Name =$_.VMNAME
$ResourceGroupName = $_.RGN
$Location=$_.Location
Select-AzureRmSubscription -SubscriptionId 
$Subscriptionid
$VMNAME = Get-azurermvm -status -Name $Name -ResourceGroupName $ResourceGroupName $Status   = $VMNAME.Statuses[1].DisplayStatus
if($Status -eq "vm deallocated")
{ 
start-azurermvm -Name $_.VMNAME -ResourceGroupName $_.RGN 
$flag="true"  
}
if($flag -eq "true")
{
stop-azurermvm -Name $_.VMNAME -ResourceGroupName $_.RGN -Force 
}  
catch {   
Write-Output $Error[0].Exception.Message
}
}

 

 

 

 

 

4 Replies
Highlighted

@Pradeep MISHRA - You can use -AsJob option which will create each execution as a job for you and will run in the background

You can then track the status of the Job based on the object returned.

Highlighted

Hello @Pradeep MISHRA

If you are able to use PowerShell 7 you may want to consider the new parallel option for the foreach-object Cmdlet.

https://docs.microsoft.com/en-us/powershell/scripting/whats-new/what-s-new-in-powershell-70?view=pow... 

I hope that helps

Cheers
Rolf
#MCT #LearnWithRolf #TheCloud42 

Highlighted

@Rolf-42 ,

 

I am using below powershell version and unable to using workflow

 

PS C:\Users\Admin> Get-Host | Select-Object Version

Version
-------
5.1.19041.1

 

Can you please share a sample ?

Highlighted

Hello @Pradeep MISHRA

Since your PowerShell version is to low what about running PowerShell 7 in a container? https://www.thomasmaurer.ch/2020/03/run-azure-powershell-in-a-docker-container/ 

If that is not possible the background job feature is your approach https://docs.microsoft.com/en-us/powershell/scripting/developer/cmdlet/background-jobs?view=powershe...

Start-Job -ScriptBlock { Get-Process -Name powershell } is the example you are looking for.

The example is taken from the Start-Job documentation here: https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Core/Start-Job?view=powershe...

Cheers
Rolf
#MCT #LearnWithRolf #TheCloud42