How to create a custom Powershell host that supports setting the Window Title

StackOverflow https://stackoverflow.com/questions/23116750

  •  04-07-2023
  •  | 
  •  

Pergunta

I'm attempting to use the VMWare PowerCLI stuff from C#, but in doing so, I'm getting the following error:

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."

I'm attempting to implement a minimal custom host that does this, however, nothing really jumps out at me as "this is what to override to support setting the host window title. I've looked through the following docs:

http://msdn.microsoft.com/en-us/library/system.management.automation.host.pshost_methods(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/system.management.automation.host.pshostuserinterface(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/system.management.automation.host.pshostrawuserinterface(v=vs.85).aspx

Can someone point me in the right direction on how to resolve this?

Foi útil?

Solução

You could just use PowerShell to change the titlebar. I don't have reference as to where I snagged this script, but I did not write it. I have used it, and it does work though.

# 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()
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top