-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathNew-gMSA.ps1
48 lines (36 loc) · 1.35 KB
/
New-gMSA.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
function New-gMSA
{
param (
[Parameter(Mandatory=$true)]
[String[]]$HostNames,
[Parameter(Mandatory=$false)]
[String]$SecGroupPath = 'OU=gMSA for Windows Containers,DC=mydomain,DC=com',
[Parameter(Mandatory=$false)]
[String[]]$PrincipalsAllowedToRetrieveManagedPassword = @( 'DockerGMSAGroup' )
)
Import-Module (Join-Path $PSScriptRoot CredentialSpec.psm1)
foreach ($hostname in $HostNames)
{
$account = $null
$dnsroot = (Get-ADDomain).DNSRoot
$dnsHostName = $hostName + '.' + $dnsroot
$account = Get-ADServiceAccount -Filter { cn -eq $hostName }
if ($account -eq $null)
{
Write-Verbose "Creating ADServiceAccount..."
$account = New-ADServiceAccount -name $hostName `
-DnsHostName $dnsHostName `
-Path $SecGroupPath `
-PrincipalsAllowedToRetrieveManagedPassword $PrincipalsAllowedToRetrieveManagedPassword `
-PassThru
foreach ($group in $PrincipalsAllowedToRetrieveManagedPassword)
{
Add-ADGroupMember $group $account
}
} else
{
Write-Verbose "ADServiceAccount already exists."
}
New-CredentialSpec -Name $hostName -AccountName $hostName
}
}