How to invoke Powershell version 2 on remote computer via Invoke-Command

3.1k views Asked by At

I'm trying to remotely setup websites on web servers using powershell. The web servers I'm attempting to configure are Windows Server 2008 R2 SP1 which has powershell v2 on it by default.

To make things easier, I'm using the Snapin "WebAdministration".

Whenever I attempt to invoke a the following command:

PS C:\p4\eacp4wireframe\ReleaseEngineering\DL_Powershell\Infrastructure\PowerShell\IIS>     Invoke-Command -ComputerName web4 -Credential $admin -ScriptBlock {add-pssnapin     WebAdministration; Get-Website} 
No snap-ins have been registered for Windows PowerShell version 2.
+ CategoryInfo          : InvalidArgument: (WebAdministration:String) [Add-PSSnapin], PSArgumentException
+ FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand

I get the dreaded "No snap-ins have been registered for Windows PowerShell version 2" error.

Logically, I check the version of powershell by running this command:

PS C:\ Invoke-Command -ComputerName web4 -Credential $admin -ScriptBlock {(Get-Host).version}
Major  Minor  Build  Revision PSComputerName                                                                                                                                                                   
-----  -----  -----  -------- --------------                                                                                                                                                                   
1      0      0      0        web4       

I can remotely login to the web server and run the Powershell commands fine.

Any ideas?

1

There are 1 answers

0
Keith Hill On BEST ANSWER

That version number is the version number of the host implementation used by remoting e.g.:

C:\PS> Invoke-Command . {Get-Host | Get-Member}


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

You have to be using at least the PowerShell 2.0 engine because that is when PowerShell Remoting was introduced. It is more likely that you're invoking the 64-bit PowerShell remoting endpoint and you have snapins that either haven't been registered for the 64-bit PowerShell or won't run in 64-bit PowerShell (or vice versa).

If you need to invoke the 32-bit remoting endpoint try this:

C:\PS> Invoke-Command . {[intptr]::size} -ConfigurationName Microsoft.PowerShell32
4