forked from timmcmic/DLConversionV2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNew-PowershellSession.ps1
More file actions
137 lines (104 loc) · 4.72 KB
/
New-PowershellSession.ps1
File metadata and controls
137 lines (104 loc) · 4.72 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<#
.SYNOPSIS
This function creates a powershell session to an on premises server to invoke winRM commands.
.DESCRIPTION
This function creates a powershell session to an on premises server to invoke winRM commands.
.PARAMETER Credential
This is the credential that will be utilized to establish the connection
.PARAMETER Server
This is the server that the connection will be made to.
.PARAMETER PowershellSessionName
This is the name of the powershell session that will be created.
.OUTPUTS
Powershell session to use for aad connect commands.
.EXAMPLE
new-PowershellSession -Server SERVER -Credential Credential -PowershellSessionName Name
#>
Function New-PowershellSession
{
[cmdletbinding()]
Param
(
[Parameter(ParameterSetName="NotOnline",Mandatory = $true)]
[Parameter(ParameterSetName="Online",Mandatory = $true)]
[pscredential]$Credentials,
[Parameter(ParameterSetName="NotOnline",Mandatory = $true)]
[string]$Server,
[Parameter(ParameterSetName="NotOnline",Mandatory = $true)]
[Parameter(ParameterSetName="Online",Mandatory = $true)]
[string]$PowershellSessionName,
[Parameter(ParameterSetName="Online",Mandatory = $true)]
[string]$connectionURI,
[Parameter(ParameterSetName="Online",Mandatory = $true)]
[string]$authenticationType,
[Parameter(ParameterSetName="Online",Mandatory = $true)]
[string]$configurationName,
[Parameter(ParameterSetName="Online")]
[boolean]$allowRedirection=$FALSE,
[Parameter(ParameterSetName="Online")]
[boolean]$requiresImport=$FALSE
)
#Declare function variables.
$sessionToImport=$NULL
#Start function processing.
Out-LogFile -string "********************************************************************************"
Out-LogFile -string "BEGIN NEW-POWERSHELLSESSION"
Out-LogFile -string "********************************************************************************"
#Log the parameters and variables for the function.
if ($server -ne "")
{
Out-LogFile -string ("Server = "+$Server)
}
Out-LogFile -string ("Credential = "+$Credentials.userName.tostring())
Out-LogFile -string ("PowershellSessionName = "+$PowershellSessionName)
if ($connectionURI -ne "")
{
Out-LogFile -string ("ConnectionURI = "+$connectionURI)
}
if ($authenticationType -ne "")
{
Out-LogFile -string ("AuthenticationType = "+$authenticationType)
}
if ($configurationName -ne "")
{
Out-LogFile -string ("ConfigurationName = "+$configurationName)
}
if ($allowRedirection -ne $FALSE)
{
Out-LogFile -string ("AllowRedirection = "+$allowRedirection)
}
if ($requiresImport -ne $FALSE)
{
Out-LogFile -string ("RequireImport = "+$requiresImport)
}
try
{
if ($requiresImport -eq $FALSE)
{
#The session was flagged by the caller as requiring import.
#This would usually be reserved for things like Exchange On Premises / Exchange Online
Out-LogFile -string "Creating the powershell to server."
New-PSSession -computername $Server -credential $Credentials -name $PowershellSessionName -errorAction STOP
}
elseif ($requiresImport -eq $TRUE)
{
#No import is required - this is a local powershell session
Out-LogFile -string "Creating the powershell to server that requires import."
$sessiontoimport=New-PSSession -ConfigurationName $configurationName -ConnectionUri $connectionURI -Credential $credentials -AllowRedirection:$allowRedirection -Authentication $authenticationType -name $PowershellSessionName
}
}
catch
{
Out-LogFile -string $_ -isError:$TRUE
}
Out-LogFile -string "The powershell session was created successfully."
Out-LogFile -string "END NEW-POWERSHELLSESSION"
Out-LogFile -string "********************************************************************************"
#This function is designed to open local and remote powershell sessions.
#If the session requires import - for example exchange - return the session for later work.
#If not no return is required.
if ($requiresImport -eq $TRUE)
{
return $sessionToImport
}
}