New-AzADAppCredential 生成客户端密码

New-AzADAppCredential Generate Client Secret

有没有办法使用 New-AzADAppCredential cmdlet 生成密码客户端机密?我不想向 cmdlet 提供密码,而更愿意像 Azure 门户一样使用生成密码。

恐怕你不能,当使用New-AzADAppCredential创建客户端密码时,需要-Password

解决方法是使用 New-AzureADApplicationPasswordCredential command in AzureAD 模块。

New-AzureADApplicationPasswordCredential -ObjectId "<object-id>"

不是原生的,但您可以使用在门户中生成它们的相同工具创建非常相似的客户端密码。

它不像将解决方案嵌入 cmdlet 那样优雅,但效果很好。

$bytes = New-Object Byte[] 32
$rand = ([System.Security.Cryptography.RandomNumberGenerator]::Create()).GetBytes($bytes)
$ClientSecret = [System.Convert]::ToBase64String($bytes) | ConvertTo-SecureString -AsPlainText -Force

$endDate = [System.DateTime]::Now.AddYears(1)
New-AzADAppCredential -ObjectId "<object-id>" -Password $ClientSecret -startDate $(get-date) -EndDate $endDate

现在这是可能的。

$Sub = "Your Sub Here"
Set-AzContext -SubscriptionName $Sub
$app = New-AzADApplication -DisplayName 'MyTestApp'
$secretStartDate = Get-Date
$secretEndDate = $secretStartDate.AddYears(1)
$webApiSecret = New-AzADAppCredential -StartDate $secretStartDate -EndDate $secretEndDate -ApplicationId $app.AppId 
Write-Output $webApiSecret

输出: