简体   繁体   English

部署新的 Azure VM 时,如何从自定义 PS 模块运行 PowerShell cmdlet?

[英]How to run PowerShell cmdlets from custom PS modules when deploying a new Azure VM?

Is it possible to install a custom PowerShell module in Azure VM post-deployment task and run PS cmdlets from that module in this task?是否可以在 Azure VM 部署后任务中安装自定义 PowerShell 模块并在此任务中从该模块运行 PS cmdlet?

I have a Bicep template that deploys a new Windows Azure VM.我有一个部署新 Windows Azure VM 的 Bicep 模板。 The template has a Microsoft.Compute/virtualMachines/extensions@2020-12-01 post-deployment resource that takes these steps:该模板有一个Microsoft.Compute/virtualMachines/extensions@2020-12-01部署后资源,它执行以下步骤:

  1. Installs an MSI-packaged application (with a new custom PowerShell module) using Start-Process (OK)使用Start-Process (OK) 安装 MSI 打包的应用程序(带有新的自定义 PowerShell 模块)
  2. Creates several folders via PowerShell (OK)通过 PowerShell 创建多个文件夹(OK)
  3. Runs several cmdlets installed at step 1 ( NOT OK )运行在步骤 1 中安装的多个 cmdlet(不正常

Running a cmdlet from the custom module in the post-deployment script shows the following error in the "Extensions + applications" log:从部署后脚本中的自定义模块运行 cmdlet 会在“Extensions + applications”日志中显示以下错误:

... is not recognized as the name of a cmdlet...

When I change the post-deployment step to Import-Module MODULENAME , I see another message:当我将部署后步骤更改为Import-Module MODULENAME时,我看到另一条消息:

Import-Module: The specified module 'MODULENAME' was not loaded because no valid module file was found in any module \r\ndirectory.

When I run Get-Module in the post-deployment task, I see only these two modules listsd:当我在部署后任务中运行Get-Module时,我只看到这两个模块列表:

ModuleType Version    Name                                ExportedCommands                                             
---------- -------    ----                                ----------------                                             
Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Con...
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object...}

So the module is not getting loaded.所以模块没有被加载。

But when I remote to the deployed VM, I run the cmdlets from the custom PowerShell module without any errors.但是,当我远程访问已部署的 VM 时,我从自定义 PowerShell 模块运行 cmdlet,没有任何错误。

I think that my scenario is supported, but I don't quite understand how to troubleshoot this further.我认为我的场景是受支持的,但我不太明白如何进一步解决这个问题。

I've added -Wait to Start-Process and this has resolved the problem.我添加了-Wait to Start-Process ,这解决了问题。

It appears that the problem was with my customScript.ps1 script.看来问题出在我的customScript.ps1脚本上。 The very first line of the script installs an MSI-packaged application that adds the required PowerShell module:脚本的第一行安装了一个 MSI 打包的应用程序,它添加了所需的 PowerShell 模块:

Start-Process -FilePath msiexec.exe -ArgumentList @('/i', 'msi-installer-name.msi', '/qb')

PowerShell starts the installation using Start-Process and continues. PowerShell 使用Start-Process开始安装并继续。 So when I'm running these commands, the installation hasn't yet finished:所以当我运行这些命令时,安装还没有完成:

$env:PSModulePath | Out-File C:\Temp\psmodulepath.txt
Get-Module -ListAvailable | Out-File C:\Temp\getmodulelistavailable.txt

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

相关问题 Azure 错误:从 Azure Marketplace 部署 Visual Studio Community VM 时出现 SubscriptionNotAuthorizedForImage - Azure Error: SubscriptionNotAuthorizedForImage when deploying a Visual Studio Community VM from Azure Marketplace Cloud Run,当新的部署服务停止时 - Cloud Run, when new deploying service is stopping 从 PowerShell 创建 Azure VM 时是否可以安装应用程序? - Is it possible to install application while creating Azure VM from PowerShell? Azure Function 从 Azure devops 管道部署时无法访问运行时 - Azure Function Runtime Unreachable when deploying from Azure devops pipeline 如何在 Azure DevOps 上运行的 PowerShell 上登录 azure - How can I login azure on PowerShell that run on Azure DevOps 如何使用 VM 上的文件使用 Azure 数据工厂运行 python 脚本? - How to run python script with Azure Data Factory using files on a VM? 如何以非交互方式从 Powershell 脚本运行 Azure Log Analytics 查询? - How to run an Azure Log Analytics query from a Powershell script non interactively? 从 Azure 中的现有 VM 构建类似的 VM - Build a similar VM from an existing VM in Azure 在 Azure 上使用 Terraform 部署具有托管标识的 VM 失败 - Deploying a VM with managed identity using Terraform on Azure fails 如何使用 AzureAD Auth 从一个 Azure VM RDP 到另一个 VM? - How to RDP from one Azure VM to another with AzureAD Auth?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM