在 powershell 退出时运行脚本

XAM*_*cky 3 windows powershell

我将如何运行可在exit命令或 shell 关闭上运行的 powershell 脚本。我希望这个脚本在每个关闭的 shell 上运行,而不仅仅是一个 shell。

脚本

Avs*_*lom 5

使用Register-EngineEvent您可以这样做:

Register-EngineEvent PowerShell.Exiting –Action { 'Type your Code' }

# Alternatively with hiding the event: 
Register-EngineEvent PowerShell.Exiting –Action -SupportEvent { yourExitFunction; }
Run Code Online (Sandbox Code Playgroud)


Avs*_*lom 2

将其从函数中取出并在脚本块中运行它,如下所示:

\n\n
Register-EngineEvent PowerShell.Exiting \xe2\x80\x93Action {\n\n$foo = @"\n .d8888b.  8888888888 8888888888      Y88b   d88P  .d88888b.  888     888\nd88P  Y88b 888        888              Y88b d88P  d88P" "Y88b 888     888\n "Y888b.   8888888    8888888            Y888P    888     888 888     888\n    "Y88b. 888        888                 888     888     888 888     888\n      "888 888        888                 888     888     888 888     888\nY88b  d88P 888        888                 888     Y88b.  d88P Y88b. .d88P\n "Y8888P"  8888888888 8888888888          888      "Y88888P"   "Y88888P"\n .d8888b.  8888888b.     d8888  .d8888b.  8888888888\nd88p  Y88b 888   Y88b   d88888 d88P  Y88b 888\n "Y888b.   888   d88P d88P 888 888        8888888\n    "Y88b. 8888888P" d88P  888 888        888\n      "888 888      d88P   888 888    888 888\nY88b  d88P 888     d8888888888 Y88b  d88P 888\n "Y8888P"  888    d88P     888  "Y8888P"  8888888888\n .d8888b.   .d88888b.  888       888 888888b.    .d88888b. Y88b   d88P\nd88P  Y88b d88P" "Y88b 888   o   888 888  "88b  d88P" "Y88b Y88b d88P\n888        888     888 888 d888b 888 8888888K.  888     888   Y888P\n888        888     888 8888888888888 888  "Y88b 888     888    888\n888    888 888     888 88888P Y88888 888    888 888     888    888\nY88b  d88P Y88b. .d88P 8888P   Y8888 888   d88P Y88b. .d88P    888\n "Y8888P"   "Y88888P"  888P     Y888 8888888P"   "Y88888P"     888\n"@\n $file="c:\\.seeyouspacecowboy"\n $foo | out-file $file\n $colour=("red","yellow","darkyellow","green","cyan","darkcyan","darkmagenta")\n [int]$num=-1\n $info = (get-content $file)\n Write-Host ""\n foreach($i in $info) \n    {\n     [int]$perspective=($num / 3)\n     write-host $i -foregroundcolor $colour[$perspective]\n     $num++   \n    }\n Write-Host ""\n\n }\n
Run Code Online (Sandbox Code Playgroud)\n\n

*当然,请确保您在写入 c:\\ 时具有管理员权限

\n