0

我有一个带有以下参数的脚本:

[Parameter(ParameterSetName="FromPipe", Mandatory=$true, ValueFromPipelineByPropertyName=$true)] [string[]]$ComputerName,
[Parameter(ParameterSetName="FromPipe", Mandatory=$true, ValueFromPipelineByPropertyName=$true)] [string[]]$AppName,
[Parameter(ParameterSetName="FromPipe", Mandatory=$true, ValueFromPipelineByPropertyName=$true)] [string[]]$key,
[Parameter(ParameterSetName="FromPipe", Mandatory=$true, ValueFromPipelineByPropertyName=$true)] [string[]]$setting,

我想在后台运行这个脚本,但我一直在努力获取参数。这个脚本是一个模块。我可以从命令行成功运行它

start-job -InitializationScript {Import-Module ConfigureServer.psm1} -ScriptBlock {Get-ConfigureServerInput} -ArgumentList "google.com","Test1","Test2","Test3","-Verbose"

但这是我的脚本中用于启动工作的实际代码:

$JobObj = New-Object PSObject
$JobObj | Add-Member -type NoteProperty -name ComputerName -value $($obj.ComputerName)
$JobObj | Add-Member -type NoteProperty -name AppName -value $($obj.AppName)
$JobObj | Add-Member -type NoteProperty -name key -value $($obj.key)
$JobObj | Add-Member -type NoteProperty -name setting -value $($obj.setting)
$JobObj | Add-Member -type NoteProperty -name Verbose -value $true
Write-Verbose "Starting job: -Name $JobName -FilePath Job-ConfigureServer.ps1 -ArgumentList $($JobObj)"
#Start-Job -Name $JobName -InitializationScript {import-module ConfigureServer.psm1} -ScriptBlock {Get-ConfigureServerInput} -ArgumentList $($JobObj) | Out-Null
#Start-Job -Name $JobName -InitializationScript {import-module ConfigureServer.psm1} -ScriptBlock {Get-ConfigureServerInput @ARGS} -ArgumentList $($JobObj) | Out-Null
Start-Job -Name $JobName -InitializationScript {import-module ConfigureServer.psm1} -ScriptBlock {Get-ConfigureServerInput -ComputerName $($obj.ComputerName) -AppName $($obj.AppName) -key $($obj.key) -setting $($obj.setting) -Verbose}
Start-Job -FilePath TestJob.ps1 -ArgumentList $($obj)

未注释的 Start-Job 只是运行一个转储 $args 的测试脚本。

"Arguments: $($args.count)"
$args

结果是:

PS E:\Powershell\ConfigureServer> get-job | receive-job
Arguments: 1


ComputerName : server.bogus.com
AppName      : iis
key          : default site
setting      : cert
RunspaceId   : 5c72401e-3cb1-4f44-b0e1-c370c8a3c200

Arguments: 1
ComputerName : fbravo
AppName      : Net
key          : IP
setting      : 10.11.12.13
RunspaceId   : 80b2095c-6129-4d7c-8e89-528cf93ce975

Arguments: 1
ComputerName : tfs.bogus.com
AppName      : edamame
key          : DLL
setting      : edamame.dll
RunspaceId   : f7727c69-121c-4a59-b646-a4f0eac53e7d

Arguments: 1
ComputerName : falpha
AppName      : IIS
key          : IP
setting      : 1.1.1.1
RunspaceId   : cd182911-d4a6-4bfd-ba4b-103ebaab20a3

Arguments: 1
ComputerName : web.bogus.com
AppName      : Net
key          : IP
setting      : 192.168.100.102
RunspaceId   : 03d6130c-46d1-483d-833c-781720fcc0cf

如何格式化我的 Start-Job 以成功传递参数?我用 -FilePath 尝试了各种格式的 Start-Job,列出参数而不是传递对象等。

它们最终都被阻止等待用户输入提示参数。

4

0 回答 0