将值传递给PowerShell。退出事件操作

时间:2014-06-01 21:12:17

标签: powershell

我是PowerShell脚本的新手,所以也许我错过了一些东西。脚本日志在文件系统中更改为文件,并且还启动&整理剧本。

$logPath = "C:\temp\watcher.log"
Register-EngineEvent -SourceIdentifier PowerShell.Exiting -MessageData $logPath -Action {
    $timeStamp = $Event.TimeGenerated.ToString("s")
    Out-File -FilePath $Event.MessageData -Append -InputObject "$timeStamp    Finished"
}

在IO.FileSystemWatcher事件中传递值非常有用。但是PowerShell.Exiting事件中的值不会通过。怎么了?

1 个答案:

答案 0 :(得分:1)

这并不能直接回答您的问题,但可以作为解决方法。这就是我这样做的方式:

$historyPath = Join-Path (split-path $profile) "history-$(Get-Date -f o).clixml"
Register-EngineEvent -SourceIdentifier powershell.exiting -SupportEvent -Action {
    Get-History | Export-Clixml $historyPath
}.GetNewClosure()

使用GetNewClosure()脚本块可以捕获外部$historyPath变量的值