[英]Sysprep Azure VM with Remote PowerShell
I am trying to SysPrep a VM in Azure via Azure DevOps YAML pipelines.我正在尝试通过 Azure DevOps YAML 管道在 Azure 中 SysPrep 虚拟机。 In the same pipeline (previous stage), I am deploying the VM via Azure Resource Manager (ARM) templates and copying scripts and files to the machine.
在同一管道(上一阶段)中,我通过 Azure 资源管理器 (ARM) 模板部署 VM,并将脚本和文件复制到机器上。 I want to automate the whole process so RDP'ing into the machine is not an option.
我想自动化整个过程,因此不能选择 RDP 进入机器。 When I run SysPrep on the VM, I am getting the following error message:
当我在 VM 上运行 SysPrep 时,我收到以下错误消息:
##[error]The remote session query failed for <insertMyFQDN>.eastus.cloudapp.azure.com with the following error message: Access is denied.
From my repo I have a couple of files, including a PowerShell script that SysPreps the machine (sysPrepvm.ps1) - taken from Run SysPrep remotely through commands from Azure powershell .从我的存储库中,我有几个文件,包括 SysPreps 机器的 PowerShell 脚本 (sysPrepvm.ps1) - 通过来自 Azure Z2F2D399F0EA3844859BZ FE5543 的命令远程运行 SysPrep 获取。 The script works when I am logged in on the machine and run in manually.
当我在机器上登录并手动运行时,该脚本有效。
sysPrepvm.ps1 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
I am using the built-in task in Azure DevOps, "Powershell on Target Machines", with which I am able to invoke other commands or scripts, so I am confident that the task works.我正在使用 Azure DevOps 中的内置任务,“目标机器上的 Powershell”,我可以使用它调用其他命令或脚本,因此我相信该任务可以正常工作。
My Stage in the YAML Pipeline looks like this:我在 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'
Is there a limitation of running this remotely?远程运行这个有限制吗? I haven't been able to find a work-around yet, so any answer is greatly appreciated.
我还没有找到解决方法,因此非常感谢任何答案。
Edit编辑
I have also tried running the script with -Verb RunAs, as an Inline script instead of File, as well as tried the accepted answer on the previous post:我还尝试使用 -Verb RunAs 作为内联脚本而不是 File 运行脚本,并尝试了上一篇文章中接受的答案:
Sysprep an Azure VM using PowerShell task in a pipeline 在管道中使用 PowerShell 任务对 Azure VM 进行 Sysprep
I managed to find a way using Custom Script Extension instead.我设法找到了一种使用自定义脚本扩展的方法。 I didn't think of doing this at first, since the ARM template I am using already has a Custom Script Extension on the machine to enable WinRM, and cannot have multiple Custom Script Extensions installed at the same time.
一开始我没有想到要这样做,因为我使用的 ARM 模板在机器上已经有一个自定义脚本扩展来启用 WinRM,并且不能同时安装多个自定义脚本扩展。 Also, I didn't want to execute SysPrep as soon as the machine booted because it had to run other scripts and settings first.
此外,我不想在机器启动后立即执行 SysPrep,因为它必须先运行其他脚本和设置。 In my YAML pipeline, I managed to remove the existing one to redeploy a new Custom Script Extension with the script included.
在我的 YAML 管道中,我设法删除了现有的管道,以重新部署包含脚本的新自定义脚本扩展。
I posted the entire solution on GitHub - https://github.com/Crytlig/azsysprep - for anyone in interested.我在 GitHub - https://github.com/Crytlig/azsysprep上发布了整个解决方案 - 对于任何感兴趣的人。 It obviously needs a bit of cleaning but it works as is.
它显然需要一些清洁,但它可以按原样工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.