简体   繁体   English

Sysprep Azure VM 与远程 PowerShell

[英]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.

相关问题 在管道中使用 PowerShell 任务 Sysprep 一个 Azure VM - Sysprep an Azure VM using PowerShell task in a pipeline Azure VM中的远程Powershell - remote powershell in azure VM 如何使用 Powershell 从我的本地计算机在 Azure Windows 虚拟机 (VM) 中运行“sysprep /generalize”? - How to run "sysprep /generalize” in Azure Windows Virtual Machine (VM) from my local machine using Powershell? 将Azure PowerShell Cmdlet加载到远程VM - Load Azure PowerShell Cmdlets onto Remote VM Azure Runbook - 在远程 VM 上运行 Powershell - Azure runbook - run Powershell on remote VM 通过Azure远程通过Powershell访问Azure虚拟机 - Accessing an azure vm through Powershell remote from Azure Automation 通过Azure Powershell中的命令远程运行SysPrep - Run SysPrep remotely through commands from Azure powershell 如何连接到Azure Windows VM并使用PowerShell运行远程脚本? - How Connect to a Azure Windows VM and run a remote script with PowerShell? 如何在远程Azure VM上执行Powershell命令/脚本? - How can one execute Powershell commands/ scripts on a remote Azure VM? 如何对Azure Data Factory V2中的现有Azure VM运行远程命令(PowerShell / Bash)? - How to run a remote command (powershell/bash) against an existing Azure VM in Azure Data Factory V2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM