我正在尝试通过 Azure DevOps YAML 管道在 Azure 中对 VM 进行 SysPrep。在同一管道(上一阶段)中,我通过 Azure 资源管理器 (ARM) 模板部署 VM,并将脚本和文件复制到机器上。我想自动化整个过程,因此不能选择 RDP 进入机器。当我在 VM 上运行 SysPrep 时,我收到以下错误消息:
##[error]The remote session query failed for <insertMyFQDN>.eastus.cloudapp.azure.com with the following error message: Access is denied.
从我的存储库中,我有几个文件,包括 SysPrep 机器 (sysPrepvm.ps1) 的 PowerShell 脚本 -通过来自 Azure powershell 的命令远程运行SysPrep 获取。当我在机器上登录并手动运行时,该脚本有效。
sysPrepvm.ps1
$sysPrepPath = 'C:\Windows\System32\Sysprep\Sysprep.exe'
$arguments = '/generalize /oobe /shutdown /quiet'
Invoke-Command -ScriptBlock {param($sysPrepPath,$arguments) Start-Process -FilePath $sysPrepPath -ArgumentList $arguments} -ArgumentList $sysPrepPath,$arguments
我正在使用 Azure DevOps 中的内置任务“Powershell on Target Machines”,通过它我可以调用其他命令或脚本,因此我确信该任务可以正常工作。
我在 YAML 管道中的阶段如下所示:
- stage:
displayName: SysPrep
variables:
azFQDN: $[stageDependencies.Connect.connect.outputs['SetEnvVars.azFQDN']]
jobs:
- job: SysPrepVhD
steps:
- task: PowerShellOnTargetMachines@3
inputs:
Machines: '$(azFQDN)' # FQDN on the machine
UserName: '$(adminUser)'
UserPassword: '$(adminPw)'
ScriptType: 'FilePath'
ScriptPath: 'C:\Windows\System32\Sysprep\fishtank\sysPrepvm.ps1'
远程运行这个有限制吗?我还没有找到解决方法,因此非常感谢任何答案。
编辑
我还尝试使用 -Verb RunAs 作为内联脚本而不是 File 运行脚本,并尝试了上一篇文章中接受的答案: