New ‘Import-SessionCommands’ function added to SBTools module


SBTools module can be downloaded from the Microsoft Script Center Repository.

To install it, extract the .rar file, and run install-SBTools.ps1 in the folder where you extracted the .rar file.

The Import-SessionCommands function will import modules from other computers and export them locally if the -Keep switch us selected.

NAME
Import-SessionCommands

SYNOPSIS
Function to import commands from another computer

SYNTAX
Import-SessionCommands [-ModuleName] <String[]> [-ComputerName] <String> [[-Keep]] [-WhatIf] [-Confirm] [<CommonParameters>]

DESCRIPTION
Function will import commands from remote computer from the module(s) listed.

PARAMETERS
-ModuleName <String[]>
Name(s) of the module(s) that we want to import their commands into the current
PS console.
Note that session commands will not be available in other PS instances.

Required? true
Position? 1
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

-ComputerName <String>
Computer name that has the module(s) that we need to import their commands.

Required? true
Position? 2
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

-Keep [<SwitchParameter>]
This is a switch. When selected, the function will export the imported module(s)
locally under “C:\Program Files\WindowsPowerShell\Modules” if it’s in the PSModulePath,
otherwise, it will export it to the default path “$home\Documents\WindowsPowerShell\Modules”
– Note 1: Exported modules and their commands can be used directly from any PS instance
after a module has been exported with the -keep switch
– Note 2: Even though a module has been exported locally, everytime you try to use one of
its commands, PS will start an implicit remoting session to the server where the
module was imported from.

Required? false
Position? 3
Default value False
Accept pipeline input? false
Accept wildcard characters? false

-WhatIf [<SwitchParameter>]

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

-Confirm [<SwitchParameter>]

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).

NOTES
Function by Sam Boutros
Requires PS 3.0
v1.0 – 08/17/2014
Although we need to eventually run:
Remove-PSSession -Session $Session
We cannot do that in the function as we’ll lose the imported session commands
Two things to consider:
1. The session will be automatically removed when the PS console is closed
2. If in the parent script that’s using this function a blanket Remove-PSSession
command is run, like:
Get-PSSession | Remove-PSSession
We’ll lose this session and its commands, which could cripple the parent script

————————– EXAMPLE 1 ————————–

C:\PS>Import-SessionCommands -ModuleName ActiveDirectory -ComputerName DC01

This example imports all the commands from the ActiveDirectory module from the DC01 server
So, in this PS console instance we can use AD commands like Get-ADComputer without the need
to install AD features, tools, or PS modules on this computer!

————————– EXAMPLE 2 ————————–

C:\PS>Import-SessionCommands SQLPS,Storage V-2012R2-SQL1 -Verbose

This example imports all the commands from the PSSQL and Storage modules from the MySQLServer
server into the current PS instance

————————– EXAMPLE 3 ————————–

C:\PS>Import-SessionCommands WebAdministration,BestPractices,MMAgent CM01 -keep

This example imports all the commands from the WebAdministration, BestPractices, and MMAgent
modules from the CM01 server into the current PS instance, and exports them locally.

RELATED LINKS
https://superwidgets.wordpress.com/category/powershell/

Advertisements

One response

  1. Pingback: You don’t need to hunt for Powershell modules anymore | Sam's Corner

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s