Skip to content

Instantly share code, notes, and snippets.

@daveselias
Created July 16, 2014 11:30
PS script for importing remote commands, functions, modules, etc.
#* FileName: Import-RemoteCommands.ps1
#*=============================================
#* Script Name: Import-RemoteCommands
#* Created: 07/10/2014-published
#* Author: David S. Elias
#* Email: daveselias@gmail.com
#* Requirements: PowerShell v2.0+
#* Keywords: Function, module, command, cmdlet, import, remoting
#*=============================================
#*=============================================
#* REVISION HISTORY
#*=============================================
#* Date: 07/15/2014
#* Time: 1:50PM
#* Issue: Get-PSSnapin, Import-Module and Import-PSSession
#* always cause warnings or errors
#* Solution: Changed ErrorActions from Stop to
#* SilentlyContinue
#*=============================================
<#
.Synopsis
Imports all Commands, functions, modules and snapins not already found in the active console session
.DESCRIPTION
Imports all Commands, functions, modules and snapins not already found in the active console session
from the specified computer(s).
.EXAMPLE
Import-RemoteCommands.ps1 -ComputerName DC1
Attempting Remote session connection to DC1 using New-PSSession
Attempting Get-Pssnapin on DC1
Attempting Import-Module on DC1
Attempting Import-PSSession from DC1
###################################################
Congratulations, you have imported 907 new commands
###################################################
.EXAMPLE
Import-RemoteCommands.ps1 -ComputerName DC1, DC2
Attempting Remote session connection to DC1 using New-PSSession
Attempting Get-Pssnapin on DC1
Attempting Import-Module on DC1
Attempting Import-PSSession from DC1
Attempting Remote session connection to DC2 using New-PSSession
Attempting Get-Pssnapin on DC2
Attempting Import-Module on DC2
Attempting Import-PSSession from DC2
###################################################
Congratulations, you have imported 1207 new commands
###################################################
#>
Param
(
# Server Name(s) to import modules, functions and cmdlets from
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$false,
Position=0)]
[array]
$ComputerName
)
Begin{
[array]$ALL = $ComputerName
IF($ALL.count -lt 1){
Write-Error -Message "No Server Names Were Provided" -ErrorAction Stop
EXIT 1
}
$List=@()
ForEach($item in $ALL){
Try{
$Alive = Test-Connection -ComputerName $item -Count 1 -Quiet -ErrorAction Stop
IF($Alive -eq $true){
$List += $item
}
}Catch{
Write-Warning -Message "Unable to find $item"
}
}
$PSV = $PSVersionTable.PSVersion.major
IF($PSV -eq 2){
$LocalCommands = Get-Command
}ELSEIF($PSV -ge 3){
$LocalCommands = Get-Command -All
}
}
Process{
ForEach($Server in $List){
Try{
Write-Host "Attempting Remote session connection to $Server using New-PSSession" -ForegroundColor Green
$Active = New-PSSession -ComputerName $Server -ErrorAction Stop
Try{
Write-Host "Attempting Get-Pssnapin on $Server" -ForegroundColor Green
Invoke-Command -Session $Active { Get-PSSnapin -Registered | ForEach-Object { Add-PSSnapin -Name $_.name } } -ErrorAction SilentlyContinue
Try{
Write-Host "Attempting Import-Module on $Server" -ForegroundColor Green
Invoke-Command -Session $Active { Get-Module -ListAvailable | ForEach-Object { Import-Module -Name $_.Name } } -ErrorAction SilentlyContinue
Try{
Write-Host "Attempting Import-PSSession from $Server" -ForegroundColor Green
Import-PSSession -Session $Active -ErrorAction SilentlyContinue
}Catch{
Write-Warning -Message "Unable to execute Import-PSSession on $Server"
}
}Catch{
Write-Warning -Message "Unable to execute Import-Module on $Server"
}
}Catch{
Write-Warning -Message "Unable to execute Get-Pssnapin on $Server"
}
}Catch{
Write-Error -Message "Unable to connect to remote session on $Server and import requested functions, cmdlets, modules" -ErrorAction Stop
}
}
}
End{
########################### Compare commands to verify if any new commands were imported ###########################
IF($PSV -eq 2){
$AfterCommands = Get-Command
$NewCommands = Compare-Object -ReferenceObject $LocalCommands -DifferenceObject $AfterCommands
}ELSEIF($PSV -ge 3){
$AfterCommands = Get-Command -All
$NewCommands = Compare-Object -ReferenceObject $LocalCommands.Name -DifferenceObject $AfterCommands.Name
}
IF($NewCommands.count -lt 1){
Write-Error -Message "No New functions, cmdlets or modules were imported"
}ELSEIF($NewCommands.count -gt 1){
Clear-Host
Write-Host "###################################################" -ForegroundColor Cyan
Write-Host " " -ForegroundColor Cyan
Write-Host "Congratulations, you have imported $($NewCommands.count) new commands" -ForegroundColor Green
Write-Host " " -ForegroundColor Cyan
Write-Host "###################################################" -ForegroundColor Cyan
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment