Skip to content

Commit

Permalink
Create Create-RunAsAccount-Updated.ps1
Browse files Browse the repository at this point in the history
  • Loading branch information
krmanupa authored Oct 3, 2022
1 parent 677c9fd commit bb51e59
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions Utility/AzRunAs/Create-RunAsAccount-Updated.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
Param (
[Parameter(Mandatory = $true)]
[String] $ResourceGroup,
[Parameter(Mandatory = $true)]
[String] $AutomationAccountName,
[Parameter(Mandatory = $true)]
[String] $ApplicationDisplayName,
[Parameter(Mandatory = $true)]
[String] $SubscriptionId,
[Parameter(Mandatory = $true)]
[String] $SelfSignedCertPlainPassword,
[Parameter(Mandatory = $false)]
[string] $EnterpriseCertPathForRunAsAccount,
[Parameter(Mandatory = $false)]
[String] $EnterpriseCertPlainPasswordForRunAsAccount,
[Parameter(Mandatory = $false)]
[ValidateSet("AzureCloud", "AzureUSGovernment")]
[string]$EnvironmentName = "AzureCloud",
[Parameter(Mandatory = $false)]
[int] $SelfSignedCertNoOfMonthsUntilExpired = 12
)
function CreateSelfSignedCertificate([string] $certificateName, [string] $selfSignedCertPlainPassword,
[string] $certPath, [string] $certPathCer, [string] $selfSignedCertNoOfMonthsUntilExpired ) {
$Cert = New-SelfSignedCertificate -DnsName $certificateName -CertStoreLocation cert:\LocalMachine\My -KeyExportPolicy Exportable -Provider "Microsoft Enhanced RSA and AES Cryptographic Provider" -NotAfter (Get-Date).AddMonths($selfSignedCertNoOfMonthsUntilExpired) -HashAlgorithm SHA256
$CertPassword = ConvertTo-SecureString $selfSignedCertPlainPassword -AsPlainText -Force
Export-PfxCertificate -Cert ("Cert:\localmachine\my\" + $Cert.Thumbprint) -FilePath $certPath -Password $CertPassword -Force | Write-Verbose
Export-Certificate -Cert ("Cert:\localmachine\my\" + $Cert.Thumbprint) -FilePath $certPathCer -Type CERT | Write-Verbose
}
function CreateServicePrincipal([System.Security.Cryptography.X509Certificates.X509Certificate2] $PfxCert, [string] $applicationDisplayName) {
$keyValue = [System.Convert]::ToBase64String($PfxCert.GetRawCertData())
$keyId = (New-Guid).Guid
# Create an Azure AD application, AD App Credential, AD ServicePrincipal
# Requires Application Developer Role, but works with Application administrator or GLOBAL ADMIN
# -HomePage ("http://" + $applicationDisplayName) -IdentifierUris ("http://" + $keyId)
$Application = New-AzADApplication -DisplayName $ApplicationDisplayName
# Requires Application administrator or GLOBAL ADMIN
$ApplicationCredential = New-AzADAppCredential -ApplicationId $Application.AppId -CertValue $keyValue -StartDate $PfxCert.NotBefore -EndDate $PfxCert.NotAfter
# Requires Application administrator or GLOBAL ADMIN
$ServicePrincipal = New-AzADServicePrincipal -ApplicationId $Application.AppId
$GetServicePrincipal = Get-AzADServicePrincipal -ObjectId $ServicePrincipal.Id
# Sleep here for a few seconds to allow the service principal application to become active (ordinarily takes a few seconds)
Sleep -s 15
# Requires User Access Administrator or Owner.
$NewRole = New-AzRoleAssignment -RoleDefinitionName Contributor -ServicePrincipalName $Application.AppId -ErrorAction SilentlyContinue
$Retries = 0;
While ($NewRole -eq $null -and $Retries -le 6) {
Sleep -s 10
New-AzRoleAssignment -RoleDefinitionName Contributor -ServicePrincipalName $Application.AppId | Write-Verbose -ErrorAction SilentlyContinue
$NewRole = Get-AzRoleAssignment -ServicePrincipalName $Application.AppId -ErrorAction SilentlyContinue
$Retries++;
}
return $Application.AppId.ToString();
}
function CreateAutomationCertificateAsset ([string] $resourceGroup, [string] $AutomationAccountName, [string] $certifcateAssetName, [string] $certPath, [string] $certPlainPassword, [Boolean] $Exportable) {
$CertPassword = ConvertTo-SecureString $certPlainPassword -AsPlainText -Force
Remove-AzAutomationCertificate -ResourceGroupName $resourceGroup -AutomationAccountName $AutomationAccountName -Name $certifcateAssetName -ErrorAction SilentlyContinue
New-AzAutomationCertificate -ResourceGroupName $resourceGroup -AutomationAccountName $AutomationAccountName -Path $certPath -Name $certifcateAssetName -Password $CertPassword -Exportable:$Exportable | write-verbose
}
function CreateAutomationConnectionAsset ([string] $resourceGroup, [string] $AutomationAccountName, [string] $connectionAssetName, [string] $connectionTypeName, [System.Collections.Hashtable] $connectionFieldValues ) {
Remove-AzAutomationConnection -ResourceGroupName $resourceGroup -AutomationAccountName $AutomationAccountName -Name $connectionAssetName -Force -ErrorAction SilentlyContinue
New-AzAutomationConnection -ResourceGroupName $ResourceGroup -AutomationAccountName $AutomationAccountName -Name $connectionAssetName -ConnectionTypeName $connectionTypeName -ConnectionFieldValues $connectionFieldValues
}

#To install the latest version of Azure PowerShell, see https://docs.microsoft.com/powershell/azure/install-az-ps.
#To learn about about using Az modules in your Automation account see https://docs.microsoft.com/azure/automation/shared-resources/modules.

Import-Module Az.Automation
Enable-AzureRmAlias
Connect-AzAccount -Environment $EnvironmentName
$Subscription = Get-AzSubscription -SubscriptionId $SubscriptionId | Set-AzContext

# Create a Run As account by using a service principal
$CertifcateAssetName = "AzureRunAsCertificate"
$ConnectionAssetName = "AzureRunAsConnection"
$ConnectionTypeName = "AzureServicePrincipal"
if ($EnterpriseCertPathForRunAsAccount -and $EnterpriseCertPlainPasswordForRunAsAccount) {
$PfxCertPathForRunAsAccount = $EnterpriseCertPathForRunAsAccount
$PfxCertPlainPasswordForRunAsAccount = $EnterpriseCertPlainPasswordForRunAsAccount
}
else {
$CertificateName = $AutomationAccountName + $CertifcateAssetName
$PfxCertPathForRunAsAccount = Join-Path $env:TEMP ($CertificateName + ".pfx")
$PfxCertPlainPasswordForRunAsAccount = $SelfSignedCertPlainPassword
$CerCertPathForRunAsAccount = Join-Path $env:TEMP ($CertificateName + ".cer")
CreateSelfSignedCertificate $CertificateName $PfxCertPlainPasswordForRunAsAccount $PfxCertPathForRunAsAccount $CerCertPathForRunAsAccount $SelfSignedCertNoOfMonthsUntilExpired
}

# Create a service principal
$PfxCert = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate2 -ArgumentList @($PfxCertPathForRunAsAccount, $PfxCertPlainPasswordForRunAsAccount)
$ApplicationId = CreateServicePrincipal $PfxCert $ApplicationDisplayName

# Create the Automation certificate asset
CreateAutomationCertificateAsset $ResourceGroup $AutomationAccountName $CertifcateAssetName $PfxCertPathForRunAsAccount $PfxCertPlainPasswordForRunAsAccount $true

# Populate the ConnectionFieldValues
$SubscriptionInfo = Get-AzSubscription -SubscriptionId $SubscriptionId
$TenantID = $SubscriptionInfo | Select TenantId -First 1
$Thumbprint = $PfxCert.Thumbprint
$ConnectionFieldValues = @{"ApplicationId" = $ApplicationId; "TenantId" = $TenantID.TenantId; "CertificateThumbprint" = $Thumbprint; "SubscriptionId" = $SubscriptionId}

# Create an Automation connection asset named AzureRunAsConnection in the Automation account. This connection uses the service principal.
CreateAutomationConnectionAsset $ResourceGroup $AutomationAccountName $ConnectionAssetName $ConnectionTypeName $ConnectionFieldValues

0 comments on commit bb51e59

Please sign in to comment.