代码之家  ›  专栏  ›  技术社区  ›  sheldonzy

Powershell向新作业传递多个参数[重复]

  •  0
  • sheldonzy  · 技术社区  · 6 年前

    $arguments= @()
    $arguments+= ("-Name", '$config.Name')
    $arguments+= ("-Account", '$config.Account')
    $arguments+= ("-Location", '$config.Location')
    
    #do some nasty things with $config
    
    Start-Job -ScriptBlock ([scriptblock]::create("& .'$ScriptPath' [string]$arguments")) -Name "Test"
    

    它失败了

    Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
        + CategoryInfo          : InvalidData: (:) [Select-AzureSubscription], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.WindowsAzure.Commands.Profile.SelectAzureSubscriptionCommand
        + PSComputerName        : localhost
    

    即使$config.name设置正确。

    提前谢谢你!

    0 回复  |  直到 10 年前
        1
  •  2
  •   mjolinor    10 年前

    我使用此方法传递命名参数:

    $arguments = 
    @{
       Name     = $config.Name
       Account  = $config.Account
       Location = $config.Location
    }
    
    #do some nasty things with $config
    
    Start-Job -ScriptBlock ([scriptblock]::create("&'$ScriptPath'  $(&{$args}@arguments)")) -Name "Test"
    

    $(&{$args}@arguments)
    

    嵌入可扩展字符串将为参数创建参数:值对:

    $config = @{Name='configName';Account='confgAccount';Location='configLocation'}
    $arguments = 
    @{
       Name     = $config.Name
       Account  = $config.Account
       Location = $config.Location
    }
    
    "$(&{$args}@arguments)"
    
    -Account: confgAccount -Name: configName -Location: configLocation
    
        2
  •  2
  •   Eris    10 年前

    单引号是文本字符串符号,您正在将“-Name”参数设置为字符串 $config.Name Value of $config.Name . 要使用该值,请使用以下命令:

    $arguments= @()
    $arguments+= ("-Name", $config.Name)
    $arguments+= ("-Account", $config.Account)
    $arguments+= ("-Location", $config.Location)