-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnableRemoteScriptExecution.psm1
More file actions
110 lines (91 loc) · 3.21 KB
/
EnableRemoteScriptExecution.psm1
File metadata and controls
110 lines (91 loc) · 3.21 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
Function Set-WinRM
{
Param (
[Parameter(Mandatory=$false)]
[string[]]$ComputerName = $env:COMPUTERNAME,
[Parameter(Mandatory=$false)]
[switch]$https
)
If (-not $PSScriptRoot)
{
# Define this variable for PS 2.0
$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
}
$psexec = "$PSScriptRoot\psexec.exe"
If ($https)
{
$run = "$PSScriptRoot\winrm-https.bat"
}
Else
{
$run = "$PSScriptRoot\winrm.bat"
}
Foreach ($computer in $ComputerName)
{
Write-Host "Processing $computer"
If (Test-Connection -ComputerName $computer -Quiet -Count 1)
{
$comp = '\\' + $computer
Write-Host "-- Running WinRM quickconfig"
& $psexec $comp -h -accepteula -s -c $run
Write-Host "--- Completed Computer: $computer"
}
Else
{
Write-Host "-- The system is offline"
}
}
Write-Host "DONE"
}
Function Repair-WinRM
{
Param (
[Parameter(Mandatory=$false)]
[string[]]$ComputerName = $env:COMPUTERNAME
)
Foreach ($computer in $ComputerName)
{
Write-Host "Processing $computer"
If (Test-Connection -ComputerName $computer -Quiet -Count 1)
{
Write-Host '-- Register PSSession Configuration'
winrs /r:$computer powershell -noprofile -command {register-pssessionconfiguration microsoft.powershell -NoServiceRestart -Force}
winrs /r:$computer powershell -noprofile -command {register-pssessionconfiguration microsoft.powershell32 -NoServiceRestart -Force}
Write-Host '-- Restarting WinRM Service'
$service = Get-WmiObject -ComputerName $computer -Class win32_Service -Filter 'name="winrm"'
$service.StopService()
Start-Sleep -Seconds 2
$service.StartService()
Write-Host "-- Completed Computer: $computer"
}
Else
{
Write-Host '-- The system is offline'
}
}
Write-Host 'DONE'
}
Function Set-ExecutionPolicyRegKeys
{
Param (
[Parameter(Mandatory=$false)]
[string[]]$ComputerName = $env:COMPUTERNAME,
[Parameter(Mandatory=$false)]
[ValidateSet('Undefined','AllSigned','RemoteSigned','Unrestricted','Bypass')]
[string]$ExecutionPolicy = 'Bypass'
)
Foreach ($computer in $ComputerName)
{
Write-Host $computer
If (Test-Connection -ComputerName $computer -Quiet -Count 1)
{
cmd /c reg add \\$computer\hklm\software\microsoft\powershell\1\shellds\microsoft.powershell /v executionpolicy /t reg_sz /d $ExecutionPolicy /f
cmd /c reg add \\$computer\hklm\SOFTWARE\Policies\Microsoft\Windows\PowerShell /v executionpolicy /t reg_sz /d $ExecutionPolicy /f
cmd /c reg add \\$computer\hklm\SOFTWARE\Policies\Microsoft\Windows\PowerShell /v enablescripts /t reg_dword /d 1 /f
}
Else
{
Write-Host "-- Offline"
}
}
}