# Select Template and add ID for easy selection
$templates = Get-Folder Templates01 |
Get-Template |
Select name |
% {$counter = -1} {
$counter++;
$_ | Add-Member -Name Template_ID -Value $counter -MemberType NoteProperty -PassThru
}
$templates | ft -Auto
$MyTemplate = Read-Host "select VM Template_ID"
$VMTemplate = $templates[$MyTemplate]
$VMNAME = Read-Host "Specify VM Name"
New-Vm -Name $VMNAME -Template $vmtemplate.Name
The above script works great. I want now to use Start-Job
to run it in the background, so I modified it to:
# Select Template and add ID for easy selection
$templates = Get-Folder Templates01 |
Get-Template |
Select name |
% {$counter = -1} {
$counter++;
$_ | Add-Member -Name Template_ID -Value $counter -MemberType NoteProperty -PassThru
}
$templates | ft -Auto
$MyTemplate = Read-Host "select VM Template_ID"
$VMTemplate = $templates[$MyTemplate]
$VMNAME = Read-Host "Specify VM Name"
$scriptblock = {
Param( $1, $2 )
New-Vm -Name $1 -Template $2
}
Start-Job -ScriptBlock $scriptblock -ArgumentList $vmname $vmtemplate.Name
and I get this error:
Start-Job : Cannot bind parameter 'InitializationScript'. Cannot convert the "template01" value of type "System.String" to type "System.Management.Automation.ScriptBlock". At line:1 char:59 + ... -Job -ScriptBlock $scriptblock -ArgumentList $vmname $vmtemplate.Name + ~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Start-Job], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.StartJobCommand
How do I fix that?