2012-10-05 37 views
1

我有一個腳本從域服務器運行,並在域服務器上「工作」。將變量傳遞給遠程會話中使用的幾個腳本塊Powershell

將工作劃分爲腳本塊中的作業,該腳本使用存儲的PSsession通過invoke-command調用。

結果記錄在域服務器上的日誌文件中,日誌文件名中包含日期時間戳記。

現在,我需要添加另一個需要駐留在工作完成的遠程的日誌。日誌名稱格式還需要包含日期和時間戳。

我的問題是將日誌的名稱傳遞給每個作業,以便它們寫入相同的文件。我玩過-ArgumentList,@args和$ args,我可以無誤地運行,但是什麼都不做,所以我沒有正確地傳遞日誌文件的名字。

下面是我如何構建腳本的超級簡化版本。

在另一個腳本塊中嵌套Start-Job是否是一個錯誤?我如何將我的唯一日誌文件名傳遞給這些scriptblocks以捕獲成功/失敗和特定點?


#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 

回答

3

你可以傳遞參數給腳本塊這樣的:

$code = { 
    param($foo) 
    Write-Host $foo 
} 

$bar = "bar" 
Invoke-Command -ScriptBlock $code -ArgumentList $bar 
相關問題