Dave: Hey Buddy,
you should look at this cool script that I found.
Buddy: Really, what
does it do?
Dave: It’s so cool. It will connect to any SQL
server and report back information about each database that is hosted on it.
Buddy: That’s cool.
Did you tell the SQL admin about it?
Dave: Yup, he said it was easier than the
expensive product we just spent 50k on. Plus he didn’t need to do anything
extra.
Buddy: I just tried
it but it keeps giving me errors.
Dave: Well do you
have SQL installed on your workstation? Greg and I do.
Buddy: No, where
can I install it from? Do I need a license? How much space does it need?
Dave: Here try running Import-RemoteCommands.ps1 –Computername
someSQLservername instead then try it
again.
Buddy: Awesome it
works now, what did that do?
Dave: The Import-RemoteCommands.ps1 script
temporarily imports all commands, functions, modules, etc. from a remote
computer to your local session so you can run almost any PowerShell script
without having to install things locally to a system.
Buddy: But what if I want to run something like that
on a server? Will I need to submit a change so the boss doesn’t have an aneurism?
Dave: You shouldn’t because as soon as you close
the PowerShell window all those commands go away! You can even use it for a
list of computers, so if you want SQL plugins, the AD module and the SCOM
module, just list all those server names separated by a coma.
Buddy: I just
opened a new console window but Import-RemoteCommands is giving me an error
now.
Dave: For it to work you need two things, 1. PowerShell
remoting needs to be enabled on the computers you list. 2. You need local admin
or PowerShell remoting access on those computers.
OK, so this didn't happen quite the way I portrayed it but it's pretty close.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#* 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 | |
} | |
} |
No comments:
Post a Comment