Skip to content

Instantly share code, notes, and snippets.

@mikepfeiffer
Last active November 13, 2022 21:45
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save mikepfeiffer/b2cc488985e675de7e8c8d38a0943539 to your computer and use it in GitHub Desktop.
Save mikepfeiffer/b2cc488985e675de7e8c8d38a0943539 to your computer and use it in GitHub Desktop.
PS Function that creates an Azure Service Principal
function New-Sp {
param($Name, $Password)
$spParams = @{
StartDate = Get-Date
EndDate = Get-Date -Year 2030
Password = $Password
}
$cred= New-Object Microsoft.Azure.Commands.ActiveDirectory.PSADPasswordCredential -Property $spParams
$sp = New-AzAdServicePrincipal -DisplayName $Name -PasswordCredential $cred
Write-Output $sp
}
@Kivi-Anandarajah
Copy link

Hi Mike,
I am following your DevOps training course from CloudSkills. This script is not working on cloud shell to create Azure Service Principal. Getting below error message.

New-Object:
Line |
10 | $cred= New-Object Microsoft.Azure.Commands.ActiveDirectory.PSADPass …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Cannot find type [Microsoft.Azure.Commands.ActiveDirectory.PSADPasswordCredential]: verify that the assembly containing this type is loaded.
New-AzADServicePrincipal:
Line |
11 | … New-AzAdServicePrincipal -DisplayName $Name -PasswordCredential $cred
| ~~~~~
| Cannot bind argument to parameter 'PasswordCredential' because it is null.

Kindly let me know if there is workaround for this?

Thanks,
Kivi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment