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

修复64位PowerShell下获取O365文件隐私标签的脚本问题

修复UiPath中调用64位PowerShell获取O365隐私标签的脚本问题

问题脚本的核心错误

  1. 路径指向错误:SysWOW64下的powershell.exe是32位版本,不符合调用64位PowerShell的需求
  2. 未捕获执行结果:直接执行命令后输出仅打印到控制台,未赋值给变量,导致后续代码无法获取有效数据

修复方案一:返回结构化JSON数据(推荐用于多属性获取)

将完整逻辑封装到64位PowerShell中执行,通过JSON格式传递结构化数据,避免跨进程对象信息丢失:

# 64位PowerShell的正确路径
$powershell64 = "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe"

# 定义要在64位PowerShell中执行的脚本块
$scriptBlock = {
    $Status = Get-AipFileStatus -Path 'C:\Users\User\Downloads\test.xlsx'
    # 生成自定义对象并转为JSON输出
    [PSCustomObject]@{
        IsLabeled = $Status.IsLabeled
        LabelId   = $Status.MainLabelId
        Label     = $Status.MainLabelName
    } | ConvertTo-Json -Compress
}

# 执行64位PowerShell并捕获JSON输出
$outputJson = & $powershell64 -NoProfile -Command $scriptBlock

# 将JSON转回PowerShell对象
$ReportLine = $outputJson | ConvertFrom-Json

# 直接获取需要的LabelId
$ReportLine.LabelId

修复方案二:直接返回LabelId值(仅需单个属性时使用)

如果只需要MainLabelId,可以简化为直接输出该属性值:

$powershell64 = "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe"
# 直接在64位PowerShell中执行命令并返回LabelId
$labelId = & $powershell64 -NoProfile -Command "(Get-AipFileStatus -Path 'C:\Users\User\Downloads\test.xlsx').MainLabelId"

# 使用获取到的LabelId
$labelId

关键注意事项

  • 64位路径正确性:必须使用System32目录下的powershell.exe,SysWOW64对应32位版本,无法满足需求
  • 跨进程数据传递:PowerShell跨进程直接传递对象会丢失类型信息,JSON是最可靠的结构化传递方式
  • -NoProfile参数:添加该参数可避免加载用户配置文件,提升执行速度并减少环境干扰

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 07:47:41