Monday, 6 May 2013

Import Powershell Module From A Remote Machine


We've all had scenarios where we need to incorporate cmdlets in our script that are not available on the local machine. The cmdlets below will import the Exchange module from a remote server :

$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "http://ServerName/powershell" 
$mod = Import-PSSession $session


The first line creates a session to the Exchange server's powershell. Line 2 imports the session and load the AD module to the powershell console on the local machine, thus providing us access to the AD cmdlets.


"WARNING: Some imported command names include unapproved verbs which might make them less discoverable. Use the Verbose parameter for more detail or type Get-Verb to see the list of approved verbs."

The warning message is displayed by default when an imported module exports cmdlets or functions that have unapproved verbs in the name parameter. Although the message is displayed, the complete module along with the non conforming cmdlets are imported.



To supress the warning message, '-DisableNameChecking' switch can be added to the import command.




'Get-Command' can be used to list all the cmdlets available through the imported module:

Get-Command -Module $mod.Name



The script here assumes that the user has domain admin credentials. Credentials can be supplied explicitly by adding the '-credential ' switch :

$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "http://ServerName/powershell"  -Credential $cred

No comments:

Post a Comment