在 Set-MsolUser 中使用变量作为参数

Use a variable as parameter in Set-MsolUser

为什么我不能在 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

您可以使用以下选项:

1)调用带有参数的命令

Invoke-Command -ScriptBlock {Get-ChildItem} -ArgumentList "-$($para) C:\Temp"

2) Use splatting

$Val = 'Path'
$HashArguments  = @{
    $Val = 'C:\Temp'
}

Get-ChildItem @HashArguments