我有一个从域服务器运行的脚本,并且在域服务器上“可以工作”。
该工作分为多个脚本块中的作业,这些脚本块使用存储的PSsession通过invoke-command调用。
结果将记录在域服务器上的日志文件中,该日志文件的日志文件名中包含日期时间戳。
现在,我需要添加另一个日志,该日志需要驻留在完成工作的远程服务器上。日志名称格式还需要包含日期和时间戳。
我的问题是将日志名称传递给每个作业,以便它们写入同一文件。我玩过-ArgumentList,@ args和$ args,它们可以正常运行,但是什么也不做,所以我没有正确传递日志文件名。
以下是我脚本结构的 super 简化版本。
将Start-Job嵌套在另一个脚本块中是错误的吗?如何将我唯一的日志文件名传递给这些脚本块中的多个,以捕获成功/失败以及特定点?
#log file names, ps session and other variables declared here
$DoDomainWorkScriptBlock = {
Try {
start-job -name DoDomainWorkjob -scriptblock{
$command = "C:\Program Files\someprogram\someprogram.exe"
& $command -f someargs
If ($? -ne "True") {Throw 'Do work failed’}
" Do non-domain work job completed. "
}
} Catch {$Error[0] | Out-File $ErrorLog -Append}
}
#other jobs nested in other scriptblocks like the one above here
Invoke-Command -session $RemoteSession -scriptblock $DoDomainWorkScriptblock | Out-File $DomainProgressLog -Append
Invoke-Command -session $RemoteSession -command{Wait-Job -name DoDomainWorkjob } | Out-File $DomainProgressLog -Append
Invoke-Command -session $RemoteSession -command{Receive-Job -Name DoDomainWorkjob } | Out-File $DomainProgressLog –Append
#invoke start, wait, and receive job commands for the other jobs
最佳答案
您可以像这样将参数传递给脚本块:
$code = {
param( $foo )
Write-Host $foo
}
$bar = "bar"
Invoke-Command -ScriptBlock $code -ArgumentList $bar
关于session - 将变量传递给远程 session 中使用的几个脚本 block Powershell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12743840/