Reputation: 1883
I'm writing my first PowerShell script to load data from a CSV and then restart the following VMs using the PowerShell script.
Please find the Excel sheet sample data
Here is the Powershell script
param(
[Parameter(Mandatory=$true)]$SubscriptionId
)
$relativePath = Get-Item Data.xlsx | Resolve-Path -Relative
$inputs = Import-Excel $relativePath
#Connecting to particular Az Subscription
Connect-AzAccount -SubscriptionId $SubscriptionId
#Restart VM function
function RestartVM{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]$VMName,
[Parameter(Mandatory=$true)]$ResourceGroupName
)
Restart-AzVM -ResourceGroupName $ResourceGroupName -Name $VMName
}
# for($i=0; $i -lt $inputs.Count; $i=$i+1)
# {
# Write-Host "Process has been started for VM:" + $inputs[$i].VMName + "in the following resource group: " + $inputs[$i].ResourceGroup
# RestartVM -VMName $inputs[$i].VMName -ResourceGroupName $inputs[$i].ResourceGroup
# }
Workflow TestParallel{
Param(
[Parameter(Mandatory = $true)]$inputs
)
Write-Output $inputs
Write-Output $inputs.Count
Foreach -parallel($i in 0..1 ){
Write-Output "Process has been started for VM:" $inputs[$i].VMName " in the following resource group: " $inputs[$i].ResourceGroup
RestartVM -VMName $inputs[$i].VMName -ResourceGroupName $inputs[$i].ResourceGroup
}
}
TestParallel -inputs $inputs
When I am executing the script in parallel for restarting the VM, I am getting the following error
Microsoft.PowerShell.Utility\Write-Error : The Azure PowerShell session has not been properly initialized. Please import the module and try again.
At TestParallel:42 char:42
Note: I am getting the following error when I am trying to run the commands in Parallel foreach (i.e to restart the VM)
Please help me to resolve the issue. Thanks in advance
Upvotes: -1
Views: 347
Reputation: 1046
First of all I think you need to take a step back and evaluate what you're trying to achieve and then solve each step individually before trying to get the entire script working.
You advise that you're trying to pull data from a CSV file but then you're using the Import-Excel
module to load an xlsx.
If you are actually pulling from a CSV file then you can use Import-CSV
without the need for Import-Excel
at all.
Given that you're attempting to use Foreach -Parallel
I would draw the conclusion that you're attempting to use the newer PowerShell 7 features and therefore you can't use workflows anyway.
If you are attempting to create your script inside an Azure Automation runbook then there is a recommendation that you should only use a workflow if you need to make use of checkpoints:
https://learn.microsoft.com/en-us/azure/automation/automation-powershell-workflow
If you are planning on using parallelism to execute the restart and you are wanting to use the newer language features as part of PowerShell 7 then you can look at this article which explains using Foreach-Object -Parallel
https://devblogs.microsoft.com/powershell/powershell-foreach-object-parallel-feature/
Upvotes: 1