$ExecutionContext.SessionState.InvokeCommand.PreCommandLookupAction = { # is called whenever a command is ready to execute param($command, $eventArgs)
# not executed internally by PowerShell if ($command-eq'Get-Service'-and$eventArgs.CommandOrigin -eq'Runspace') { # tell PowerShell what to do instead of # running the original command $eventArgs.CommandScriptBlock = { # run the original command without "*", and # submit original arguments if there have been any $command = 'dir'
$code = { # submit the host process RawUI interface and the execution context param($RawUi, $ExecContext)
do { # find the current location in the host process $location = $ExecContext.SessionState.Path.CurrentLocation # compose the time and date display $time = Get-Date-Format'HH:mm:ss dddd MMMM d' # compose the title bar text $title = "$location$time" # output the information to the title bar of the host process $RawUI.WindowTitle = $title # wait a half second Start-Sleep-Milliseconds500 } while ($true) } $ps = [PowerShell]::Create() $null = $ps.AddScript($code).AddArgument($host.UI.RawUI).AddArgument($ExecutionContext) $handle = $ps.BeginInvoke()
$code = { # submit the host process RawUI interface and the execution context param($RawUi)
do { # compose the time and date display $time = Get-Date-Format'HH:mm:ss dddd MMMM d' # compose the title bar text $title = "Current Time: $time" # output the information to the title bar of the host process $RawUI.WindowTitle = $title # wait a half second Start-Sleep-Milliseconds500 } while ($true) } $ps = [PowerShell]::Create() $null = $ps.AddScript($code).AddArgument($host.UI.RawUI) $handle = $ps.BeginInvoke()
但是对某些 DLL 文件,这个命令会执行失败,PowerShell 返回一个““Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.”异常。
当您在 Windows 机器上的 “Windows PowerShell” 中运行这段脚本,将会报错:
1 2 3 4 5 6 7 8 9
PS> C:\Users\abc\requires core.ps1 The script 'requires core.ps1' cannot be run because it contained a "#requires" statement for PowerShell editions 'Core'. The edition of PowerShell that is required by the script does not match the currently running PowerShell Desktop edition. + CategoryInfo : NotSpecified: (requires core.ps1:String) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : ScriptRequiresUnmatchedPSEdition