如何创建支持设置窗口标题的自定义Powershell主机
我试图使用C#中的VMWare PowerCLI内容,但在这样做时,我遇到了以下错误:如何创建支持设置窗口标题的自定义Powershell主机,powershell,vmware,Powershell,Vmware,我试图使用C#中的VMWare PowerCLI内容,但在这样做时,我遇到了以下错误: An unhandled exception of type 'System.Management.Automation.RuntimeException' occurred in System.Management.Automation.dll Additional information: Exception setting "WindowTitle": "Cannot invoke this func
An unhandled exception of type 'System.Management.Automation.RuntimeException' occurred in System.Management.Automation.dll
Additional information: Exception setting "WindowTitle": "Cannot invoke this function because the current host does not implement it."
我正在尝试实现一个最小的自定义主机来实现这一点,但是,没有什么真正让我吃惊的是,“这是要覆盖的内容,以支持设置主机窗口标题。我已经浏览了以下文档:
有人能告诉我如何解决这个问题的正确方向吗?你可以使用PowerShell更改标题栏。我没有提到我在哪里抓到了这个脚本,但我没有编写它。我使用过它,但它确实有效
# Setup the objects for titlebar manipulation
# Append a #1,#2,#(n-1) to the process name if there are n pre-existing processes with the same
# name, as this is how the Performance Counter system references the instances.
$psProcess = gps -id $PID
$psInstances = (gps -Name $psProcess.name).count
if ($psInstances -gt 1)
{
$psName = "{0}#{1}" -f $psProcess.name,$($psInstances - 1)
}
else
{
$psName = $psProcess.name
}
# Create the Performance Counter Object to track our sessions CPU usage
$Global:psPerfCPU = new-object System.Diagnostics.PerformanceCounter( "Process","% Processor Time", $psName )
# Get the first 'NextValue', which will be zero
$psPerfCPU.NextValue() | Out-Null
# Create a timer object and set the interval to 1 second
$Global:psTimer = New-Object System.Timers.Timer
$psTimer.Interval = 1000
# Update the window's title bar every time the timer object raises the
# elapsed event
Register-ObjectEvent -InputObject $psTimer -EventName Elapsed -Action {
$psInfo = Get-Process -id $pid
[int]$ws = $psInfo.workingset/1MB
[int]$cpu = $psPerfCPU.NextValue() / $env:NUMBER_OF_PROCESSORS
$Host.ui.rawui.WindowTitle = "$($CurrentUser.Name) $($Host.Name) $($Host.Version) | $((get-location).ProviderPath) | RAM: $ws MB CPU: $cpu%"
} | Out-Null
$psTimer.start()
问题是.Net Powershell的东西(System.Management.Automation)不支持开箱即用的标题。我当时能够理解它,现在它可以工作了,我只是误读了文档:)