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

Desktop Central运行脚本时如何获取当前登录用户路径?

问题:服务账户运行PowerShell脚本时无法获取当前登录用户的Downloads路径

使用Desktop Central在多台设备执行PowerShell脚本,需求是从当前登录用户的Downloads文件夹(Teams默认下载目录)解压SWANDPDM_SP5.1.zip包并执行安装程序。手动以用户身份运行时$home能正确指向用户目录,但通过Desktop Central服务账户运行时,$home指向C:\Windows\system32\config\systemprofile\,$env:HOMEPATH也无效,导致解压路径错误,需找到在服务账户下获取当前登录用户路径的方法。

原脚本

# 解压文件
Expand-archive -path $home\Downloads\SWANDPDM_SP5.1.zip -DestinationPath C:\temp\swpdminstaller\extracted\ -Force

# 定义要修改的注册表项(关闭UAC提示)
$RegistryPath = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System'
$Name         = 'ConsentPromptBehaviorAdmin'
$Value        = '0'

# 修改注册表
New-ItemProperty -Path $RegistryPath -Name $Name -Value $Value -PropertyType DWORD -Force

# 运行安装程序
Invoke-Item C:\temp\swpdminstaller\extracted\SOLIDWORKS_AND_PDM_2021_SP5.1\startswinstall.exe

# 定义要恢复的注册表项(还原UAC默认设置)
$RegistryPath = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System'
$Name         = 'ConsentPromptBehaviorAdmin'
$Value        = '5'

# 恢复注册表
New-ItemProperty -Path $RegistryPath -Name $Name -Value $Value -PropertyType DWORD -Force

错误信息

Expand-archive : The path 'C:\Windows\system32\config\systemprofile\Downloads\SWANDPDM_SP5.1.zip' either does not
exist or is not a valid file system path.
At C:\Program Files (x86)\DesktopCentral_Agent\Computer\startup\76507\SWandPDMdecomInstall.ps1:2 char:1
+ Expand-archive -path $home\Downloads\SWANDPDM_SP5.1.zip -DestinationP ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (C:\Windows\syst...NDPDM_SP5.1.zip:String) [Expand-Archive], InvalidOpe
rationException
+ FullyQualifiedErrorId : ArchiveCmdletPathNotFound,Expand-Archive

解决方案

方法1:通过WMI获取当前交互式登录用户的配置路径

服务账户运行脚本时,通过查询系统WMI对象定位当前实际登录的用户,再获取其Downloads目录:

# 获取当前交互式登录的用户名(格式:域\用户名)
$currentUser = Get-CimInstance -ClassName Win32_ComputerSystem | Select-Object -ExpandProperty UserName
# 提取用户名部分
$userName = $currentUser.Split('\')[1]
# 获取对应用户的配置文件路径
$userProfile = Get-CimInstance -ClassName Win32_UserProfile | Where-Object { $_.LocalPath -match "\\$userName$" } | Select-Object -ExpandProperty LocalPath
# 拼接Downloads文件夹路径
$downloadsPath = Join-Path -Path $userProfile -ChildPath "Downloads"
$zipFilePath = Join-Path -Path $downloadsPath -ChildPath "SWANDPDM_SP5.1.zip"

# 执行解压
Expand-Archive -Path $zipFilePath -DestinationPath C:\temp\swpdminstaller\extracted\ -Force

# 后续注册表修改和安装代码保持不变
$RegistryPath = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System'
$Name         = 'ConsentPromptBehaviorAdmin'
$Value        = '0'
New-ItemProperty -Path $RegistryPath -Name $Name -Value $Value -PropertyType DWORD -Force

Invoke-Item C:\temp\swpdminstaller\extracted\SOLIDWORKS_AND_PDM_2021_SP5.1\startswinstall.exe

$Value        = '5'
New-ItemProperty -Path $RegistryPath -Name $Name -Value $Value -PropertyType DWORD -Force

方法2:通过注册表读取用户配置路径

通过用户SID定位注册表中的配置路径,同样能获取当前登录用户的Downloads目录:

# 获取当前登录用户的SID
$ntAccount = New-Object System.Security.Principal.NTAccount((Get-CimInstance Win32_ComputerSystem).UserName)
$currentUserSID = $ntAccount.Translate([System.Security.Principal.SecurityIdentifier]).Value

# 从注册表读取用户配置文件路径
$userProfilePath = Get-ItemPropertyValue -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\$currentUserSID" -Name ProfileImagePath
$downloadsPath = Join-Path -Path $userProfilePath -ChildPath "Downloads"
$zipFilePath = Join-Path -Path $downloadsPath -ChildPath "SWANDPDM_SP5.1.zip"

# 执行解压
Expand-Archive -Path $zipFilePath -DestinationPath C:\temp\swpdminstaller\extracted\ -Force

# 后续代码保持不变
$RegistryPath = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System'
$Name         = 'ConsentPromptBehaviorAdmin'
$Value        = '0'
New-ItemProperty -Path $RegistryPath -Name $Name -Value $Value -PropertyType DWORD -Force

Invoke-Item C:\temp\swpdminstaller\extracted\SOLIDWORKS_AND_PDM_2021_SP5.1\startswinstall.exe

$Value        = '5'
New-ItemProperty -Path $RegistryPath -Name $Name -Value $Value -PropertyType DWORD -Force

注意事项

  • 确保Desktop Central的服务账户拥有读取用户配置目录和对应注册表项的权限
  • 若设备存在多用户同时登录的情况,可结合Win32_LogonSession筛选登录类型为**交互式(2)**的用户,避免获取错误路径
  • 修改UAC注册表项需要管理员权限,服务账户需具备相应权限

内容的提问来源于stack exchange,提问作者C.R. Pynch

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 16:25:21