I've written a script to help me delete Azure VMs and all the things that go with them (NICs, IPs, disks, etc.). Currently, the script works great, but as a VM can take 4-5 minutes to remove, I'm trying to update the script to run in parallel when removing multiple VMs.
The params of the script are as follows, with things like IDs and names stripped out for security's sake.
[CmdletBinding()]
Param (
[Parameter(Mandatory=$false)]
[string]$SubscriptionId = '[an id]',
[Parameter(Mandatory=$true)]
[string]$ResourceGroupName,
[string]$VmName,
[ValidateScript({
if ($VmName -eq $null -or $VmName -eq '') {
throw "The -Force switch cannot be used without specifying -VmName."
}
else { $true }
})]
[switch]$Force,
[Parameter(Mandatory=$false)]
[string]$ScriptPath
)
I need to use $ScriptPath because $PSScriptRoot isn't populated when calling a script using Start-Job.
I'm calling this script using the following:
$jobArgs = @($SubscriptionId,$ResourceGroupName,$VmName,$true)
$newJob = Start-Job -FilePath ".\Remove-VmAndAssets2.ps1" -ArgumentList $jobArgs
The reason I'm not sending the $ScriptPath over is because if I do, it claims it can't find a parameter to bind to. This seemed weird to me, so I cut it down to four arguments, and my first segment of code in the script is to dump the parameters to a file (since I can't see live output in a Job). Checking out the parameters that are taken in by the script after calling it as above, shows this:
Subscription ID: [the right ID]
Resource Group Name: [the right name]
VM Name: [the right name]
Force: False
Script Path: True
It's taking my fourth argument, $true, and passing it in to the fifth position. No matter what I do, I can't make something hit that -Force switch. I've tried "-Force:$true", "Force", "-Force", "-Force $true", "-Force=$true". Everything I could think of. I've tried sending the parameters in as "-SubscriptionId $SubscriptionID", etc, but then the values of the parameters end up being literally those strings.
What the heck am I missing?
Edit: Updated the ValidateScript on $Force to reflect what I actually have correctly.