我想在沒有用戶互動的情況下連接 MgGraph。
通常我們使用普通命令,如Connect-MgGraph
我發現我們可以通過certificate作為引數傳遞來做到這一點。從 powershell 有很多腳本,但無法從 Graph api 獲取任何腳本。
如何從 Microsoft graph Api 創建證書?
TIA
uj5u.com熱心網友回復:
步驟1
打開管理員 PowerShell 提示符并運行以下命令以創建自分配證書
$pwd = "Your Password"
$thumb = (New-SelfSignedCertificate -DnsName "script.mydomain.com" -CertStoreLocation "cert:\LocalMachine\My" -KeyExportPolicy Exportable -Provider "Microsoft Enhanced RSA and AES Cryptographic Provider" -NotAfter (Get-Date).AddMonths(24)).Thumbprint
$pwd = ConvertTo-SecureString -String $pwd -Force -AsPlainText
Export-PfxCertificate -cert "cert:\localmachine\my\$thumb" -FilePath c:\user\cert.pfx -Password $pwd
在哪里
- “您的密碼”應該很復雜。
- NotAfter (Get-Date).AddMonths(24) 在這里我們設定證書的到期日期,在這種情況下是現在 24 個月。根據需要調整
- “c:\user\cert.pfx”,其中包含您希望保存證書副本的位置
第2步
在我們可以將證書上傳到 Azure 之前,我們需要將證書轉換為 Base64。您可以使用證書 MMC 管理單元,因為我們將證書標記為可匯出或運行以下 PowerShell,
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate("C:\user\cert.pfx", "Your Password")
$keyValue = [System.Convert]::ToBase64String($cert.GetRawCertData()) | Out-File c:\user\cert_base64.crt
其中“c:\user\cert_base64.crt”是您的新 base64 證書路徑
第 3 步
將證書上傳到 Azure 門戶

第4步
然后復制指紋,我們將在下面的命令中使用

第 5 步
連接到 Azure
$tenantID = "<Your Tenant ID>"
$applicationID = "<Your Application ID>"
$thumbprint = "<Your Certificate Thumbprint>"
Connect-MgGraph -ClientID $applicationID -TenantId $tenantID - CertificateThumbprint $thumbprint
獲取所有 Azure AD 用戶 /
Get-MgUser
希望這可以幫助
謝謝
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/524889.html
