I've recently been testing a Powershell module I developed to see if it worked as expected via Powershell Remoting.
The module must run in Single Threaded Apartment mode (STA) because of a dependency on the MSBuild engine. Normally, when running locally you would need to launch Powershell with the -STA switch. However you cannot specify this switch when using remoting since Powershell is started and hosted by WSMan/WinRM (wsmprovhost.exe)
In this case the Powershell startup configuration is managed via the following family of cmdlets:
Set-PSSessionConfiguration
New-PSSessionConfiguration
You can also explore the session configuration in the WSMAN namespace:
cd WSMan::localhost\Plugin
WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Plugin
Name Type Keys
---- ---- ----
Event Forwarding Plugin Container {Name=Event Forwarding Plugin}
microsoft.powershell Container {Name=microsoft.powershell}
Microsoft.PowerShell32 Container {Name=Microsoft.PowerShell32}
microsoft.ServerManager Container {Name=microsoft.ServerManager}
SEL Plugin Container {Name=SEL Plugin}
WMI Provider Container {Name=WMI Provider}
The default configuration for a Remoting Session is 'microsoft.powershell', which will start Powershell in MTA.
To start a remote powershell session in STA mode you need to set the following InitializationParameters in the configuration.
pssessionthreadapartmentstate = STA
pssessionthreadoptions = UseNewThread
To test my module created a new PSSessionConfiguration, which also specified a StartupScript to automatically perform the import, and then specified the configuration when starting the remoting session:
New-PSSession -Computername SERVER1 -ConfigurationName TESTCONFIG
Incidentally, the configuration is persisted as an XML stream in the following registry location:
HKLM\Software\Microsoft\Windows\CurrentVersion\WSMAN\Plugin
0 comments:
Post a Comment