# 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
Das obige Skript funktioniert großartig. Ich möchte jetzt Start-Job
verwenden, um es im Hintergrund laufen zu lassen, so dass ich es geändert zu:Wie übergeben Sie Variablen an Start-Job?
# 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
und ich bekomme diese Fehlermeldung:
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
Wie kann ich das Problem lösen?
Haben Sie versucht, die Argumente als Array übergeben, wie dieser -ArgumentList @ ($ vmname, $ vmtemplate.name) –
yep, das gleiche Resultat – Paul
@ Paul Das ist eindeutig nicht möglich. –