【问题标题】:Use a variable as parameter in Set-MsolUser在 Set-MsolUser 中使用变量作为参数
【发布时间】:2020-03-04 23:14:49
【问题描述】:

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

【问题讨论】:

    标签: powershell powershell-4.0 azure-powershell


    【解决方案1】:

    您可以使用以下选项:

    1) 使用参数调用命令

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

    2) Use splatting

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

    【讨论】:

    • 喷溅确实是要走的路;第一种方法根本不起作用:您只能通过 -ArgumentList 传递 positional 参数(外部程序除外),顺便说一句,您甚至没有引用脚本块内的参数.我建议您删除第一个解决方案。 (您唯一能做的就是无变量喷溅,例如Invoke-Command { param($ht) Get-ChildItem @ht } -args @{ Path = 'c:\temp' } 或更简单的& { param($ht) Get-ChildItem @ht } @{ Path = 'c:\temp' })。
    猜你喜欢
    • 2018-08-28
    • 2015-01-27
    • 2014-02-03
    • 2021-05-27
    • 2020-01-21
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多