[英]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
部署后资源,它执行以下步骤:
Start-Process
(OK)使用Start-Process
(OK) 安装 MSI 打包的应用程序(带有新的自定义 PowerShell 模块)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.