Saturday, October 15, 2016

PowerShell v3 in a Year Day 15 Get Host

,
Topic: Get-Host
Alias: None

On the surface the cmdlet Get-Host could easily be mistaken for a simple .exe like hostname.exe (which returns the local machine name and resides in C:WindowsSystem32 as well as C:WindowsSysWOW64). In PowerShell, a host represents the application running (hosting) the instance of Windows PowerShell. So, for instance, in my console host I can run this cmdlet and get the following results:
Name             :ConsoleHost
Version          :3.0
InstanceId       : 54db2bf5-0964-4660-b9c8-a68fb4331a35
UI               :System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace
Likewise, I can open up an instance of PowerShell ISE v3 and get the following, completely different result:
Name             :Windows PowerShellISE Host
Version          : 3.0
InstanceId       : f3a8ceb2-e0e3-4279-bb38-2ab95c56555c
UI               :System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.Host.ISE.ISEOptions
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace
As a quick glance reveals, each instance has its own unique properties. Seeing as PowerShell can be hosted in a wide array of other environments, it is easy to see how this cmdlet is geared mainly for internal usage, as a way to track what is really running the instance of PowerShell. That being said, it quickly explains why the help for Get-Host is so terse. As noted in the help, "The default display includes the Windows PowerShell version number and the current region and language settings that the host is using, but the host object contains a wealth of information, including detailed information about the version of Windows PowerShell that is currently running and the current culture and UI culture of Windows PowerShell. You can also use this cmdlet to customize features of the host program user interface, such as the text and background colors."

When you start to dig into the class itself, you see a clear collection of object members against which you can work. Note that you can reference the current host via the object $host as so,
PS >$host |Get-Member


   TypeName: System.Management.Automation.Internal.Host.InternalHost

Name                   MemberTypeDefinition
----                   --------------------
EnterNestedPrompt      Method     voidEnterNestedPrompt()
Equals                 Method     boolEquals(System.Objectobj)
ExitNestedPrompt       Method     voidExitNestedPrompt()
GetHashCode            Method     int GetHashCode()
GetType                Method     typeGetType()
NotifyBeginApplication Method     void NotifyBeginApplication()
NotifyEndApplication   Method     voidNotifyEndApplication()
PopRunspace            Method     voidPopRunspace(),void IHostSupportsInteractiveSession.PopRunspace()
PushRunspace           Method     voidPushRunspace(runspacerunspace),void IHostSupportsInteractiveSession.PushRun...
SetShouldExit          Method     voidSetShouldExit(intexitCode)
ToString               Method     stringToString()
CurrentCulture         Property   cultureinfoCurrentCulture {get;}
CurrentUICulture       Property   cultureinfoCurrentUICulture {get;}
InstanceId             Property   guid InstanceId {get;}
IsRunspacePushed       Property   bool IsRunspacePushed {get;}
Name                   Property   string Name{get;}
PrivateData            Property   psobjectPrivateData {get;}
Runspace               Property   runspaceRunspace {get;}
UI                     Property   System.Management.Automation.Host.PSHostUserInterfaceUI {get;}
Version                Property   versionVersion {get;}
In this case, we get a fixed list of items against which we can program. Most of the properties are read-only, hence the {get;} without the corresponding {;set}. Furthermore, hosts, as in this case, truly are one-way objects. Not a lot of access is granted the end user via these routes. Now, if you start working with runspaces in C# and hosts that way, we may have a different conversation. As for generic usage, Get-Host is for all intents and purposes read-only.

0 comments to “PowerShell v3 in a Year Day 15 Get Host”

Post a Comment

 

Computer Info Copyright © 2016 -- Powered by Blogger