Remote_PSRemoting.psm1

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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
Function Set-WINRMListener
{
<#
  .SYNOPSIS
    Set-WinRMListener will configure a remote computer's WinRM service to listen for WinRM requests
 
  .DESCRIPTION
    Set-WinRMListener uses the system.management.managementclass object to create 3 registry keys. These registry entries will configure the WinRM service when it restarts next. Use Restart-WinRM to restart the WinRM service after it has been successfully configured.
 
  .EXAMPLE
    Get-Content .\servers.txt | Set-WinRMListener
 
    Sets the WinRM service on the computers in the servers.txt file
 
  .EXAMPLE
    Set-WinRMListener -ComputerName (get-content .\servers.txt) -IPv4Range "10.10.1.0 255.255.255.0"
 
    Sets the WinRM service on the computers in the servers.txt file and restricts the IPv4 address to 10.10.1.0 /24
 
#>

[cmdletBinding()]
Param
  (
    # Enter a ComputerName or IP Address, accepts multiple ComputerNames
    [Parameter( 
    ValueFromPipeline=$True, 
    ValueFromPipelineByPropertyName=$True,
    Mandatory=$True,
    HelpMessage='Enter a ComputerName or IP Address, accepts multiple ComputerNames')] 
    [String[]]$ComputerName,
    
    # Enter the IPv4 address range for the WinRM listener
    [Parameter(
    HelpMessage='Enter the IPv4 address range for the WinRM listener')]
    [String]$IPv4Range = '*',
    
    # Enter the IPv4 address range for the WinRM listener
    [Parameter(
    HelpMessage='Enter the IPv4 address range for the WinRM listener')]
    [String]$IPv6Range = '*'
  )
Begin
  {
    Write-Debug 'Opening Begin block'
    $HKLM = 2147483650
    $Key = 'SOFTWARE\Policies\Microsoft\Windows\WinRM\Service'
    $DWORDName = 'AllowAutoConfig' 
    $DWORDvalue = '0x1'
    $String1Name = 'IPv4Filter'
    $String2Name = 'IPv6Filter'
    Write-Debug 'Finished begin block variables are built'
  }
Process
  {
    Foreach ($computer in $ComputerName)
      {
        Write-Verbose "Beginning function on $computer"
        Write-Debug "Opening process block of $Computer"
        Try
          {
            Write-Verbose 'Attempting to create remote registry handle'
            $Reg = New-Object -TypeName System.Management.ManagementClass -ArgumentList \\$computer\Root\default:StdRegProv
          }
        Catch 
          {
            Write-Warning $_.exception.message
            Write-Warning "The function will abort operations on $Computer"
            break
          }
        Try 
          {
            Write-Verbose 'Attempting to create Remote Key'
            if (($reg.CreateKey($HKLM, $key)).returnvalue -ne 0) {Throw 'Failed to create key'}
          }
        Catch 
          {
            Write-Warning $_.exception.message
            Write-Warning "The function will abort operations on $Computer"
            break
          }
        Try 
          {
            Write-Verbose 'Attemping to set DWORD value'
            if (($reg.SetDWORDValue($HKLM, $Key, $DWORDName, $DWORDvalue)).ReturnValue -ne 0) {Throw 'Failed to set DWORD'}
          }
        Catch 
          {
            Write-Warning $_.exception.message
            Write-Warning "The function will abort operations on $Computer"
            break
          }
        Try 
          {
            Write-Verbose 'Attempting to set first REG_SZ Value'
            if (($reg.SetStringValue($HKLM, $Key, $String1Name, $IPv4Range)).ReturnValue -ne 0) {Throw 'Failed to set REG_SZ'}
          }
        Catch 
          {
            Write-Warning $_.exception.message
            Write-Warning "The function will abort operations on $Computer"
            break
          }
        Try 
          {
            Write-Verbose 'Attempting to set second REG_SZ Value'
            if (($reg.SetStringValue($HKLM, $Key, $String2Name, $IPv6Range)).ReturnValue -ne 0) {Throw 'Failed to set REG_SZ'}
          }
        Catch 
          {
            Write-Warning $_.exception.message
            Write-Warning "The function will abort operations on $Computer"
            break
          }
        Write-Verbose "Successfully completed operation on $computer"
        Write-Debug "Finished process block for $Computer"
      }
  }
End {}
}

Function Restart-WinRM
{
<#
  .SYNOPSIS
    Restarts the WinRM service
 
  .DESCRIPTION
    Uses the win32_service class to get and restart the WinRM service. This function was designed to be used after Set-WinRMListener to allow the new registry configuration to take hold.
 
  .EXAMPLE
    Restart-WinRM -computername TestVM
 
    Restarts the WinRM service on TestVM
 
  .EXAMPLE
    Get-Content .\servers.txt | Restart-WinRM
 
    Restarts the WinRM service on all the computers in the servers.txt file
 
#>

[CmdletBinding()]
Param 
  (
    # Enter a ComputerName or IP Address, accepts multiple ComputerNames
    [Parameter( 
    ValueFromPipeline=$True, 
    ValueFromPipelineByPropertyName=$True,
    Mandatory=$True,
    HelpMessage='Enter a ComputerName or IP Address, accepts multiple ComputerNames')] 
    [String[]]$ComputerName
  )
Begin {}
Process 
  {
    Foreach ($computer in $ComputerName)
      {
        Write-Verbose "Beginning function on $computer"
        Write-Debug "Opening process block of $Computer"
        Try
          {
            Write-Verbose 'Attempting to stop WinRM'
            (Get-WmiObject win32_service -Filter "Name='WinRM'" -ComputerName $computer).StopService() | Out-Null
            Start-Sleep -Seconds 10
            if ((Get-WmiObject win32_service -Filter "Name='WinRM'" -ComputerName $computer).state -notlike 'Stopped') {Throw 'Failed to Stop WinRM'}
          }
        Catch 
          {
            Write-Warning $_.exception.message
            Write-Warning "The function will abort operations on $Computer"
            break
          }
        Try 
          {
            Write-Verbose 'Attempting to start WinRM'
            (Get-WmiObject win32_service -Filter "Name='WinRM'" -ComputerName $computer).StartService() | Out-Null
            Start-Sleep -Seconds 10
            if ((Get-WmiObject win32_service -Filter "Name='WinRM'" -ComputerName $computer).state -notlike 'Running') {Throw 'Failed to Start WinRM'}
          }
        Catch
          {
            Write-Warning $_.exception.message
            Write-Warning "The function will abort operations on $Computer"
            break
          }
        Write-Verbose "Successfully completed operation on $computer"
        Write-Debug "Finished process block for $Computer"
      }    
  }
End {}
}

Function Set-WinRMStartup
{
<#
  .SYNOPSIS
    Changes the startup type of the WinRM service to automatic
 
  .DESCRIPTION
    Uses the Win32_service class to change the startup type on the WinRM service to Automatic
 
  .EXAMPLE
    Set-WinRMStartup -Computername TestVM
 
    Sets the WinRM service startup type to Automatic on test VM
 
#>

[CmdletBinding()]
Param 
  (
    # Enter a ComputerName or IP Address, accepts multiple ComputerNames
    [Parameter( 
    ValueFromPipeline=$True, 
    ValueFromPipelineByPropertyName=$True,
    Mandatory=$True,
    HelpMessage='Enter a ComputerName or IP Address, accepts multiple ComputerNames')] 
    [String[]]$ComputerName
  )
Begin {}
Process 
  {
    Foreach ($computer in $ComputerName)
      {
        Write-Verbose "Beginning function on $computer"
        Write-Debug "Opening process block of $Computer"
        Try
          {
            if (((Get-WmiObject win32_service -Filter "name='WinRM'" -ComputerName $computer).ChangeStartMode('Automatic')).ReturnValue -ne 0) {Throw "Failed to change WinRM Startup type on $Computer"}
          }
        Catch
          {
            Write-Warning $_.exception.message
            Write-Warning "Failed to change startmode on the WinRM service for $computer"
            break
          }
        Write-Verbose "Completed operations on $computer"
        Write-Debug "Finished process block for $Computer"
      }
  }
}

Function Set-WinRMFirewallRule
{
<#
  .SYNOPSIS
    Set-WinRMFirewallRule will configure a remote computer's firewall
 
  .DESCRIPTION
    Set-WinRMFirewallRule uses the system.management.managementclass object to create 2 registry keys. These registry entries will configure the Windows Firewall service when it nest restarts. Use Restart-WindowsFirewall to restart the Windows Firewall service after it has been successfully configured. Only use this function if you're using the windows firewall.
 
  .EXAMPLE
    Get-Content .\servers.txt | Set-WinRMFirewallRule
 
    Sets the rules for the windows firewall on the computers in the servers.txt file
 
  .EXAMPLE
    Set-WinRMFirewallRule -ComputerName (get-content .\servers.txt)
 
    Sets the rules for the windows firewall on the computers in the servers.txt file
 
#>

[CmdletBinding()]
Param 
  (
    # Enter a ComputerName or IP Address, accepts multiple ComputerNames
    [Parameter( 
    ValueFromPipeline=$True, 
    ValueFromPipelineByPropertyName=$True,
    Mandatory=$True,
    HelpMessage='Enter a ComputerName or IP Address, accepts multiple ComputerNames')] 
    [String[]]$ComputerName
  )
Begin 
  {
    Write-Debug 'Opening Begin block'
    $HKLM = 2147483650
    $Key = 'SOFTWARE\Policies\Microsoft\WindowsFirewall\FirewallRules'
    $Rule1Value = 'v2.20|Action=Allow|Active=TRUE|Dir=In|Protocol=6|Profile=Public|LPort=5985|RA4=LocalSubnet|RA6=LocalSubnet|App=System|Name=@FirewallAPI.dll,-30253|Desc=@FirewallAPI.dll,-30256|EmbedCtxt=@FirewallAPI.dll,-30267|'
    $Rule1Name = 'WINRM-HTTP-In-TCP-PUBLIC'
    $Rule2Value = 'v2.20|Action=Allow|Active=TRUE|Dir=In|Protocol=6|Profile=Domain|Profile=Private|LPort=5985|App=System|Name=@FirewallAPI.dll,-30253|Desc=@FirewallAPI.dll,-30256|EmbedCtxt=@FirewallAPI.dll,-30267|'
    $Rule2Name = 'WINRM-HTTP-In-TCP'
    Write-Debug 'Completed Begin block, Finshed creating variables'
  }
Process 
  {
    Foreach ($computer in $ComputerName)
      {
        Write-Verbose "Beginning function on $computer"
        Write-Debug "Opening process block of $Computer"
        Try
          {
            Write-Verbose 'Attempting to create remote registry handle'
            $Reg = New-Object -TypeName System.Management.ManagementClass -ArgumentList \\$computer\Root\default:StdRegProv
          }
        Catch 
          {
            Write-Warning $_.exception.message
            Write-Warning "The function will abort operations on $Computer"
            break
          }
        Try 
          {
            Write-Verbose 'Attempting to create Remote Key'
            if (($reg.CreateKey($HKLM, $key)).returnvalue -ne 0) {Throw 'Failed to create key'}
          }
        Catch 
          {
            Write-Warning $_.exception.message
            Write-Warning "The function will abort operations on $Computer"
            break
          }
        Try 
          {
            Write-Verbose 'Attempting to set first REG_SZ Value'
            if (($reg.SetStringValue($HKLM, $Key, $Rule1Name, $Rule1Value)).ReturnValue -ne 0) {Throw 'Failed to set REG_SZ'}
          }
        Catch 
          {
            Write-Warning $_.exception.message
            Write-Warning "The function will abort operations on $Computer"
            break
          }
        Try 
          {
            Write-Verbose 'Attempting to set second REG_SZ Value'
            if (($reg.SetStringValue($HKLM, $Key, $Rule2Name, $Rule2Value)).ReturnValue -ne 0) {Throw 'Failed to set REG_SZ'}
          }
        Catch 
          {
            Write-Warning $_.exception.message
            Write-Warning "The function will abort operations on $Computer"
            break
          }
        Write-Verbose "Successfully completed operation on $computer"
        Write-Debug "Finished process block for $Computer"
      }    
  }
}

Function Restart-WindowsFirewall
{
<#
  .SYNOPSIS
    Restarts the windows firewall service
 
  .DESCRIPTION
    Uses the Win32_Service class to restart the windows firewall service.
 
  .EXAMPLE
    Restart-WindowsFirewall -computername TestVM
 
    Restarts the Windows Firewall service on TestVM
 
  .EXAMPLE
    Get-Content .\servers.txt | Restart-WindowsFirewall
 
    Restarts the Windows Firewall service on all the computers in the servers.txt file
 
#>

[CmdletBinding()]
Param 
  (
    # Enter a ComputerName or IP Address, accepts multiple ComputerNames
    [Parameter( 
    ValueFromPipeline=$True, 
    ValueFromPipelineByPropertyName=$True,
    Mandatory=$True,
    HelpMessage='Enter a ComputerName or IP Address, accepts multiple ComputerNames')] 
    [String[]]$ComputerName
  )
Begin {}
Process 
  {
    Foreach ($computer in $ComputerName)
      {
        Write-Verbose "Beginning function on $computer"
        Write-Debug "Opening process block of $Computer"
        Try
          {
            Write-Verbose 'Attempting to stop MpsSvc'
            (Get-WmiObject win32_service -Filter "Name='MpsSvc'" -ComputerName $computer).StopService() | Out-Null
            Start-Sleep -Seconds 10
            if ((Get-WmiObject win32_service -Filter "Name='MpsSvc'" -ComputerName $computer).state -notlike 'Stopped') {Throw 'Failed to Stop MpsSvc'}
          }
        Catch 
          {
            Write-Warning $_.exception.message
            Write-Warning "The function will abort operations on $Computer"
            break
          }
        Try 
          {
            Write-Verbose 'Attempting to start MpsSvc'
            (Get-WmiObject win32_service -Filter "Name='MpsSvc'" -ComputerName $computer).StartService() | Out-Null
            Start-Sleep -Seconds 10
            if ((Get-WmiObject win32_service -Filter "Name='MpsSvc'" -ComputerName $computer).state -notlike 'Running') {Throw 'Failed to Start MpsSvc'}
          }
        Catch
          {
            Write-Warning $_.exception.message
            Write-Warning "The function will abort operations on $Computer"
            break
          }
        Write-Verbose "Successfully completed operation on $computer"
        Write-Debug "Finished process block for $Computer"
      }    
  }
End {}
}