生成并测试 Crescendo 模块

重要

截至2026年5月,Crescendo项目已不再积极维护。 该项目应从工程角度视为存档。

上一篇文章 中,我们为两个新的指令集创建了配置。 通过这种配置,Crescendo 可以生成一个新的 PowerShell 脚本模块。

生成你的新模块

现在你准备好创建你的模块了。 使用 Export-CrescendoModule cmdlet生成新的PowerShell脚本模块。

Export-CrescendoModule -ConfigurationFile AzCmAgent.json -ModuleName AzCmAgent.psm1

配置文件可以接受多个文件名的数组。 这样你可以为每个 cmlet 创建独立的 JSON 文件。 ModuleName 参数指定了被创建模块文件的完整路径和文件名。 在这个例子中,文件是在当前目录中创建的。

如下所示,生成 Export-CrescendoModule 两个文件——模块(PSM1)文件和模块清单(PSD1)文件。 这些是模块最终用户唯一需要安装的文件。

    Directory: D:\temp\azcmagent

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---            3/9/2022  1:59 PM           2167 AzCmAgent.json
-a---            3/9/2022  2:33 PM           4365 AzCmAgent.psd1
-a---            3/9/2022  2:33 PM           8017 AzCmAgent.psm1

测试模块中的cmdlets。

测试模块时,把它导入到PowerShell会话中,运行cmdlet。

Import-Module .\AzCmAgent.psd1
Get-Command -Module AzCmAgent
CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Get-AzCmAgentConfigProperty                        0.0.1      AzCmAgent
Function        Show-AzCmAgent                                     0.0.1      AzCmAgent

在上一篇文章中,我们定义了两个指令集。

重要

azcmagent该工具需要高度才能运行。 这意味着你必须在一个提升的PowerShell会话中运行你的模块。

Show-AzCmAgent
resourceName      :
resourceGroup     :
resourceNamespace :
subscriptionId    :
tenantId          :
vmId              :
correlationId     :
vmUuid            :
location          :
privateLinkScope  :
cloud             :
agentVersion      : 1.15.01879.114
logs              : C:\ProgramData\AzureConnectedMachineAgent\Log\himds.log
status            : Disconnected
lastHeartbeat     :
agentErrorCode    :
agentErrorDetails :
agentErrorTime    :
httpsProxy        :
proxyBypass       :
cloudProvider     : N/A
cloudMetadata     :
manufacturer      : LENOVO
model             : 30BFS07500
sql               : false
services          : {@{displayName=GC Service; serviceName=gcarcservice; status=running},
                    @{displayName=Extension Service; serviceName=extensionservice; status=running},
                    @{displayName=Agent Service; serviceName=himds; status=running}}

现在我们已经扩码了命令 azcmagent show ,它可以参与PowerShell流水线。

Show-AzCmAgent | Select-Object -ExpandProperty services
displayName       serviceName      status
-----------       -----------      ------
GC Service        gcarcservice     running
Extension Service extensionservice running
Agent Service     himds            running

接下来我们测试 Get-AzCmAgentConfigProperty cmdlet。

Get-AzCmAgentConfigProperty -Property proxy.url
Type Status  Messages                     Data
---- ------  --------                     ----
   0 success {proxy.url has not been set}

寻找Crescendo创建的指令键

随着你创建和部署更多基于Crescendo的模块,判断一个cmdlet是否由Crescendo生成可能会有帮助。 Crescendo提供了 Test-IsCrescendoCommand 指令小说(cmdlet)来帮助。

Test-IsCrescendoCommand -Command Get-AzCmAgentConfigProperty
   Module: AzCmAgent

Name                        IsCrescendoCommand RequiresElevation
----                        ------------------ -----------------
Get-AzCmAgentConfigProperty True               False

注释

如前所述,你的新模块必须在高层会话中运行。 在这个例子中,返回 Test-IsCrescendoCommand 了“ RequiresElevation为“错误”。 Crescendo 为所有平台提供了权限提升的方法。 不过,我们在配置示例中没有包含海拔。