在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
sb3006 回答:在Set-MsolUser中使用变量作为参数

您可以使用以下选项:

1)使用参数调用命令

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

2)Use splatting

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

Get-ChildItem @HashArguments
本文链接:https://www.f2er.com/3138777.html

大家都在问