-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrcgmsa.ps1
More file actions
202 lines (174 loc) · 6.96 KB
/
rcgmsa.ps1
File metadata and controls
202 lines (174 loc) · 6.96 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
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
param (
[Parameter(Mandatory = $true, ParameterSetName="gmsa", Position = 0)]
[ValidateLength(2, 255)]
[string]$Command,
[Parameter(Mandatory = $true, ParameterSetName="gmsa", Position = 1)]
[ValidateLength(2, 255)]
[ValidateScript({
foreach ($computer in $_) {
if (-not ([System.Uri]::CheckHostName($computer) -in @('Dns', 'IPv4', 'IPv6'))) {
throw "Creativity meets catastrophe, invalid computer name: $computer"
}
}
$true
})]
[string[]]$Computers,
[Parameter(Mandatory = $true, ParameterSetName="gmsa", Position = 2)]
[ValidateLength(2, 16)]
[string]$User,
[Parameter(Mandatory = $false, ParameterSetName="gmsa", Position = 3)]
[ValidateSet(0, 1, 2, 3, 4, 5, 6)]
[int]$Authentication = 0,
[Parameter(Mandatory = $false, ParameterSetName="gmsa", Position = 4)]
[ValidateLength(2, 64)]
[string]$Domain = "",
[Parameter(Mandatory = $false, ParameterSetName="gmsa", Position = 5)]
[ValidateLength(22, 22)]
[string]$Keeper,
[Parameter(Mandatory = $false, ParameterSetName="gmsa", Position = 6)]
[ValidateLength(2, 255)]
[ValidateScript({
foreach ($computer in $_) {
if (-not ([System.Uri]::CheckHostName($computer) -in @('Dns', 'IPv4', 'IPv6'))) {
throw "For FQDN's sake, invalid computer name: $computer"
}
}
$true
})]
[string[]]$Orbs = "",
[Parameter(Mandatory = $false, ParameterSetName="gmsa", Position = 7)]
[ValidateLength(3, 24)]
[string]$Vault = "devops",
[Parameter(Mandatory = $false, ParameterSetName="gmsa", Position = 8)]
[switch]$Raw,
[Parameter(Mandatory = $true, ParameterSetName="info", Position = 0)]
[switch]$v
)
if ($v) {
$version = "1.0.0"
Write-Host "Version: $version"
exit
}
$account = (New-Object System.Management.Automation.PSCredential("$Domain\$User"))
$command = $Command -replace 'javaopts\=', 'javaopts '
if ($Raw) {
$sb = $command
} else {
$sb = [scriptblock]::Create($command)
}
$scriptPath = $MyInvocation.MyCommand.Path
$serverName = $env:COMPUTERNAME
$sessionOptions = New-PssessionOption -NoCompression
$sysTempDir = [System.IO.Path]::GetTempPath()
$tempDir = Join-Path -Path $sysTempDir -ChildPath ([Guid]::NewGuid().ToString())
New-Item -Path $tempDir -ItemType Directory -Force | Out-Null
Write-Host "***********************************************************************"
Write-Host "* Running PowerShell Script: $scriptPath on $serverName"
Write-Host "* Command: $command"
Write-Host "* Run on: $Computers"
Write-Host "* gMSA Account: $User"
Write-Host "* Domain: $Domain"
Write-Host "* Keeper Credential ID: $Keeper"
Write-Host "***********************************************************************"
if ($Keeper) {
$requiredModules = @(
'Microsoft.PowerShell.SecretStore',
'Microsoft.PowerShell.SecretManagement',
'SecretManagement.Keeper'
)
$requiredModules.ForEach{
$moduleName = $_
try {
Write-Host "Importing [$moduleName]"
Import-Module $moduleName -ErrorAction Stop
} catch [System.Management.Automation.RuntimeException] {
Write-Host "Installing [$moduleName]"
Install-Module -Name $moduleName -Scope CurrentUser -Force
Import-Module $moduleName
}
}
try {
$vault_credential = [System.Environment]::GetEnvironmentVariable("VAULT", [System.EnvironmentVariableTarget]::User)
$password = ConvertTo-SecureString -String $vault_credential -AsPlainText -Force
Unlock-SecretStore -Password $password
$credential = Get-Secret -Vault $Vault $Keeper -AsPlainText -ErrorAction Stop
if ('Files' -in $credential.Keys) {
foreach ($filename in ($credential.Files)) {
$data = Get-Secret -Vault $Vault $Keeper $credential.Files[$filename]
$filepath = Join-Path -Path $tempDir -ChildPath $filename
Set-Content -Path $filepath -Value $data -AsByteStream
}
$keeperFiles = Get-ChildItem -Path $tempDir
}
} catch [Microsoft.PowerShell.SecretManagement.PasswordRequiredException] {
Write-Host "Unlocking Vault $Vault for one hour."
Unlock-SecretStore -Password $vault_credential
$credential = Get-Secret -Vault $Vault $Keeper -AsPlainText -ErrorAction Stop
} catch [System.Exception] {
Write-Error "Failed to retrieve Keeper secret.`nError: $($_.Exception.Message)"
$host.SetShouldExit(1)
}
}
$parameters = @{
Authentication = $Authentication
ComputerName = $Computers
Credential = $account
ScriptBlock = {
$remoteSysTempDir = [System.IO.Path]::GetTempPath()
$remoteTempDir = Join-Path -Path $remoteSysTempDir -ChildPath ([Guid]::NewGuid().ToString())
New-Item -Path $remoteTempDir -ItemType Directory -Force | Out-Null
if ($credential) {
foreach ($field in ($credential.Keys)) {
if ($field -ne 'Files') {
$name = "KEEPER_$($field.ToUpper())"
[Environment]::SetEnvironmentVariable(
$name,
$credential[$field],
[System.EnvironmentVariableTarget]::User
)
} else {
foreach ($filename in $keeperFiles) {
Copy-Item -Path $filename.FullName -Destination $remoteTempDir
}
}
}
}
if ($using:Orbs) {
Invoke-Command -ComputerName $using:Orbs -Credential $using:account -ScriptBlock { & $using:sb } -SessionOption $using:sessionOptions
} else {
Invoke-Command -ScriptBlock { & $using:sb }
}
Remove-Item -Path $remoteTempDir -Recurse -Force
}
}
try {
$remoteResults = Invoke-Command @parameters -SessionOption $sessionOptions -ErrorAction Stop
Write-Host "$remoteResults"
} catch [System.Exception] {
$currentError = $_
$errorId = $currentError.FullyQualifiedErrorId
$errorType = $currentError.GetType().FullName
$spnRelatedFQIDs = @(
"-2144108387,PSSessionStateBroken"
)
$spnRelatedTypes= @(
"System.Management.Automation.ErrorRecord"
)
Write-Warning "Caught an Exception."
Write-Host " - FQID: $errorId"
Write-Host " - Type: $errorType"
if ( ($spnRelatedFQIDs -contains $errorId) -or
($spnRelatedTypes -contains $errorType) )
{
Write-Warning "First attempt failed with a possible SPN issue. Retrying with -IncludePortInSPN."
$sessionOptions = New-PssessionOption -IncludePortInSPN -NoCompression
$remoteResults = Invoke-Command @parameters -SessionOption $sessionOptions
Write-Host "$remoteResults"
} else {
throw $currentError
$host.SetShouldExit(1)
}
} finally {
Remove-Item -Path $tempDir -Recurse -Force
}
$host.SetShouldExit(0)