为什么不能在PowerShell中使用变量作为参数?
Set-MsolUser -UserPrincipalName john.doe@contoso.com -$parameter Stockholm
在这种情况下,$ parameter等于City
Set-MsolUser : A positional parameter cannot be found that accepts argument '-City'.
At line:1 char:1
+ Set-MsolUser -UserPrincipalName john.doe@contoso.com
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-MsolUser], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.Online.Administration.Automation.SetUser
答案 0 :(得分:1)
您可以使用以下选项:
1)使用参数调用命令
Invoke-Command -ScriptBlock {Get-ChildItem} -ArgumentList "-$($para) C:\Temp"
$Val = 'Path'
$HashArguments = @{
$Val = 'C:\Temp'
}
Get-ChildItem @HashArguments