# 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
Run Code Online (Sandbox Code Playgroud)
上面的脚本效果很好。我现在想用来Start-Job在后台运行它,所以我把它修改为:
# Select Template and add ID for easy selection
$templates = Get-Folder Templates01 |
Get-Template |
Select …Run Code Online (Sandbox Code Playgroud) 我的剧本:
$computername=$args[0]
if ($args -eq $null) { $computername = Read-Host "enter computer name" }
Get-ADComputer -Id $computername -Properties * | select name,description
Run Code Online (Sandbox Code Playgroud)
如果我通过脚本传递参数,即:
get-ComputerName.ps1 computer01
它工作正常。但是,如果我跳过计算机,则希望它提示我,但出现此错误:
Get-ADComputer:无法验证参数“ Identity”上的参数。争论
一片空白。为参数提供有效值,然后尝试运行
再次命令。
在U:\ get-ADComputer-assigned-user.ps1:9 char:20
+ Get-ADComputer -Id $计算机名-属性* | 选择名称,描述
+ ~~~~~~~~~~~~~~
+ CategoryInfo:InvalidData:(:) [Get-ADComputer],ParameterBindingValidationException
+ FullyQualifiedErrorId:ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADComputer
我不知道如何使它工作。