Posts tagged “Export-SessionCommand

Export-SessionCommand function to export PS script commands to remote PS session


Have you been in the situation where you’re in a PS session trying to run commands against one or more remote computers using PowerShell Remoting sessions? You may have PS modules and cmdlets in the current PS session that are not available in the remote session(s). This function will allow you to export local PS session commands to remote PS sessions. The native Export-PSSession cmdlet seems to do the opposite – bringing remote session cmdlets to local PS session.

Export-SessionCommand PowerShell function is part of the SB-Tools PowerShell module. This function takes one or more Powershell script functions/commands from current session and exports them to a remote PS session. This function will ignore and not export binary functions.  Exported functions will persist on the remote computer for the user profile used with the PS remote session.

The function takes the following parameters:

Command

This is a required parameter. This is one or more script commands available in the current PS session. For example Update-SmbMultichannelConnection cmdlet/function of the SmbShare PS module.
To see available script commands, you can use:

Get-Command | ? { $_.CommandType -eq 'function' }

ModuleName

This is an optional parameter that defaults to ‘SBjr’ if not provided. This is the name of the module that this function will create on the remote computer under the user profile of the remote PS session. This will over-write prior existing module with the same name.

Session

System.Management.Automation.Runspaces.PSSession object usually obtained by using New-PSSession cmdlet

Example

$Session = New-PSSession -ComputerName 'Mycomp.MyDomain.com' -Credential (Get-SBCredential -UserName 'domain\user')

Export-SessionCommand get-saervice,get-sbdisk,bla,get-bitlockerstatus,get-service -Session $Session -Verbose


To use the SB-Tools PowerShell module which is available in the PowerShell Gallery, you need PowerShell 5. To view your PowerShell version, in an elevated PowerShell ISE window type

$PSVersionTable

To download and install the latest version of SB-Tools from the PowerShell Gallery, type

Install-Module SB-Tools,POSH-SSH -Force

SB-Tools contains functions that depend on POSH-SSH module, and they’re typically installed together.

To load the SB-Tools and POSH-SSH modules type:

Import-Module SB-Tools,POSH-SSH -DisableNameChecking

To view a list of cmdlets/functions in SB-Tools, type

Get-Command -Module SB-Tools

To view the built-in help of one of the SB-Tools functions/cmdlets, type

help <function/cmdlet name> -show

such as

help Convert-IpAddressToMaskLength -show

Advertisements