简体   繁体   English

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

[英]Passing values to PowerShell.Exiting event actions

I'm new to powershell scripting so maybe I've just missed something. 我是Powershell脚本的新手,所以也许我只是错过了一些东西。 The script log changes in file system to file and also starting & finishing script. 脚本日志在文件系统中更改为文件,并且还开始和结束脚本。

$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"
}

Passing values in IO.FileSystemWatcher events works great. 在IO.FileSystemWatcher事件中传递值非常有用。 But value in PowerShell.Exiting event doesn't pass. 但是PowerShell.Exiting事件中的值不会传递。 What's wrong? 怎么了?

This doesn't directly answer your question but may serve as a work-around. 这不能直接回答您的问题,但可以作为解决方法。 This is how I do this: 这就是我的方法:

$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()

Using GetNewClosure() the scriptblock can capture the value of the external $historyPath variable 使用GetNewClosure()可以捕获外部$historyPath变量的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM