我是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事件中的值不会传递。怎么了?
最佳答案
这不能直接回答您的问题,但可以作为解决方法。这就是我的方法:
$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
变量的值
关于powershell - 将值传递给PowerShell。退出事件操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23984531/