首页 > 解决方案 > 当我的用户名中有 \ 时,Powershell 脚本不运行

问题描述

我有以下 PowerShell 脚本,但它认为 DS 是一个模块。我从 Azure DevOps 变量中获取用户名。用户名的实际值为DS\TestUser 有人可以帮忙吗

$serviceName = "Test"    
$username = $(UserName) 
$secpasswd = ConvertTo-SecureString $(Password) -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential $username, $secpasswd
$binaryPath = "D:\Test\Test.exe"
New-Service -name $serviceName -binaryPathName $binaryPath -displayName $serviceName -startupType Automatic -credential $mycreds

在此处输入图像描述

我已经尝试将变量转换为 ToString()、Out-String 并将 \ 替换为 \

标签: powershellazure-devops

解决方案


正如 Mathias R. Jessen 在评论中提到的,更改$(UserName)"$(UserName)"可以解决这个问题。


推荐阅读