Close the PSSession in New-AzsAdGraphServicePrincipal.

This is to avoid the ERCS VMs running out of memory when creating lots of service principals.
This commit is contained in:
James Stoker 2020-04-22 12:48:47 +01:00 коммит произвёл bganapa
Родитель 7519562cb6
Коммит c40bf3d0ab
1 изменённых файлов: 12 добавлений и 6 удалений

Просмотреть файл

@ -59,12 +59,18 @@ function New-AzsAdGraphServicePrincipal {
$computerName = $ERCSMachineName
$cloudAdminCredential = $AdminCredential
$domainAdminSession = New-PSSession -ComputerName $computerName -Credential $cloudAdminCredential -configurationname privilegedendpoint -Verbose
$GraphClientCertificate = New-SelfSignedCertificate -CertStoreLocation "cert:\CurrentUser\My" -Subject "CN=$ApplicationGroupName" -KeySpec KeyExchange
$graphRedirectUri = "https://localhost/".ToLowerInvariant()
$ApplicationName = $ApplicationGroupName
$application = Invoke-Command -Session $domainAdminSession -Verbose -ErrorAction Stop `
-ScriptBlock { New-GraphApplication -Name $using:ApplicationName -ClientRedirectUris $using:graphRedirectUri -ClientCertificates $using:GraphClientCertificate }
try
{
$GraphClientCertificate = New-SelfSignedCertificate -CertStoreLocation "cert:\CurrentUser\My" -Subject "CN=$ApplicationGroupName" -KeySpec KeyExchange
$graphRedirectUri = "https://localhost/".ToLowerInvariant()
$ApplicationName = $ApplicationGroupName
$application = Invoke-Command -Session $domainAdminSession -Verbose -ErrorAction Stop `
-ScriptBlock { New-GraphApplication -Name $using:ApplicationName -ClientRedirectUris $using:graphRedirectUri -ClientCertificates $using:GraphClientCertificate }
}
finally
{
$domainAdminSession | Remove-PSSession
}
return $application
}