forked from mikefrobbins/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew-MrCimSession.ps1
84 lines (75 loc) · 2.95 KB
/
New-MrCimSession.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#Requires -Version 3.0
function New-MrCimSession {
<#
.SYNOPSIS
Creates CimSessions to remote computer(s), automatically determining if the WSMAN
or Dcom protocol should be used.
.DESCRIPTION
New-MrCimSession is a function that is designed to create CimSessions to one or more
computers, automatically determining if the default WSMAN protocol or the backwards
compatible Dcom protocol should be used. PowerShell version 3 is required on the
computer that this function is being run on, but PowerShell does not need to be
installed at all on the remote computer.
.PARAMETER ComputerName
The name of the remote computer(s). This parameter accepts pipeline input. The local
computer is the default.
.PARAMETER Credential
Specifies a user account that has permission to perform this action. The default is
the current user.
.EXAMPLE
New-MrCimSession -ComputerName Server01, Server02
.EXAMPLE
New-MrCimSession -ComputerName Server01, Server02 -Credential (Get-Credential)
.EXAMPLE
Get-Content -Path C:\Servers.txt | New-MrCimSession
.INPUTS
String
.OUTPUTS
Microsoft.Management.Infrastructure.CimSession
.NOTES
Author: Mike F Robbins
Website: http://mikefrobbins.com
Twitter: @mikefrobbins
#>
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline)]
[ValidateNotNullorEmpty()]
[string[]]$ComputerName = $env:COMPUTERNAME,
[System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty
)
BEGIN {
$Opt = New-CimSessionOption -Protocol Dcom
$SessionParams = @{
ErrorAction = 'Stop'
}
If ($PSBoundParameters['Credential']) {
$SessionParams.Credential = $Credential
}
}
PROCESS {
foreach ($Computer in $ComputerName) {
$SessionParams.ComputerName = $Computer
if ((Test-WSMan -ComputerName $Computer -ErrorAction SilentlyContinue).productversion -match 'Stack: ([3-9]|[1-9][0-9]+)\.[0-9]+') {
try {
Write-Verbose -Message "Attempting to connect to $Computer using the WSMAN protocol."
New-CimSession @SessionParams
}
catch {
Write-Warning -Message "Unable to connect to $Computer using the WSMAN protocol. Verify your credentials and try again."
}
}
else {
$SessionParams.SessionOption = $Opt
try {
Write-Verbose -Message "Attempting to connect to $Computer using the DCOM protocol."
New-CimSession @SessionParams
}
catch {
Write-Warning -Message "Unable to connect to $Computer using the WSMAN or DCOM protocol. Verify $Computer is online and try again."
}
$SessionParams.Remove('SessionOption')
}
}
}
}