繁体   English   中英

Sysprep Azure VM 与远程 PowerShell

[英]Sysprep Azure VM with Remote PowerShell

我正在尝试通过 Azure DevOps YAML 管道在 Azure 中 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.

从我的存储库中,我有几个文件,包括 SysPreps 机器的 PowerShell 脚本 (sysPrepvm.ps1) - 通过来自 Azure Z2F2D399F0EA3844859BZ FE5543 的命令远程运行 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”,我可以使用它调用其他命令或脚本,因此我相信该任务可以正常工作。

我在 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 运行脚本,并尝试了上一篇文章中接受的答案:

在管道中使用 PowerShell 任务对 Azure VM 进行 Sysprep

我设法找到了一种使用自定义脚本扩展的方法。 一开始我没有想到要这样做,因为我使用的 ARM 模板在机器上已经有一个自定义脚本扩展来启用 WinRM,并且不能同时安装多个自定义脚本扩展。 此外,我不想在机器启动后立即执行 SysPrep,因为它必须先运行其他脚本和设置。 在我的 YAML 管道中,我设法删除了现有的管道,以重新部署包含脚本的新自定义脚本扩展。

我在 GitHub - https://github.com/Crytlig/azsysprep上发布了整个解决方案 - 对于任何感兴趣的人。 它显然需要一些清洁,但它可以按原样工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM