You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Get-MsolServicePrincipalCredential指定属性无返回,需多属性用于SharePoint上传

PowerShell获取应用客户端密钥并包含DisplayName的解决方法

问题原因

Get-MsolServicePrincipalCredential返回的凭据对象本身不包含DisplayName属性,该属性属于服务主体($appentry)对象,因此直接通过Select-Object -Property DisplayName无法获取到对应值。

修改后的代码

Connect-MsolService
$applist = Get-MsolServicePrincipal -All | Where-Object { 
    $_.DisplayName -notlike "*Microsoft*" -and 
    $_.DisplayName -notlike "autohost*" -and 
    $_.ServicePrincipalNames -notlike "*localhost*" 
}

foreach ($appentry in $applist) {
    $principalName = $appentry.DisplayName
    # 合并服务主体的DisplayName与凭据属性
    Get-MsolServicePrincipalCredential -AppPrincipalId $appentry.AppPrincipalId -ReturnKeyValues $false | 
        Where-Object { $_.Type -eq "Password" } | 
        Select-Object @{Name='DisplayName';Expression={$principalName}}, Type, KeyId, StartDate, EndDate, Usage
}

代码说明

  • 通过计算属性@{Name='DisplayName';Expression={$principalName}},将服务主体的DisplayName注入到凭据结果中
  • 按需指定需要输出的其他属性(Type、KeyId等),格式与无属性指定时的输出结构一致,可直接复用上传至SharePoint列表

无属性指定时的返回示例(翻译后)

Type :
Password Value :
KeyId : 642ee910-9b17-4d17-93d4-0192f3c1f855
StartDate : 2018-05-25 08:22:37
EndDate : 2019-05-25 08:22:37
Usage : 验证

内容的提问来源于stack exchange,提问作者Gatewarden

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 11:09:17