Forum Discussion

Pradeep MISHRA's avatar
Pradeep MISHRA
Copper Contributor
Aug 09, 2020

Unable to script using Multi-threading

  • 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

Resources