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

标签 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事件中的值不会传递。怎么了?

最佳答案

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

$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/

相关文章:

mysql - 无法在 Windows 中执行 mysql 命令并在 mysql docker 中使用 $MYSQL_ROOT_PASSWORD

powershell - 通过 Powershell 设置 AzureAD 的 StrongAuthenticationUserDetails PhoneNumber?

powershell - 抑制灾难性Powershell进程故障的错误窗口

powershell - 如何将多个参数传递到 PowerShell 中的函数中?

powershell - 使用 catch 在 PowerShell 中选择前 x 个字符

c# - PowerShell 的 Get-ExecutionPolicy 返回不同的值

powershell - 如何限制 Get-ChildItem 搜索的文件(或限制递归深度)?

powershell - 通过 powershell 在多台机器上刷新 DNS 和注册 DNS

powershell - 如何在 PowerShell 中读取多行多字符串注册表项?

linux - 如何在 SSH 下的 Powershell 中启动一些文件管理器?