简体   繁体   English

在 Azure DevOps 管道中的 PowerShell 中使用参数

[英]Use parameter in PowerShell in Azure DevOps pipeline

We have recently started using Azure DevOps Pipelines for our Dynamics 365 CRM implementation, but it is still new to me我们最近开始将 Azure DevOps Pipelines 用于我们的 Dynamics 365 CRM 实施,但它对我来说仍然是新的

I recently came across this blog post by Joe Griffin on how you can use PowerShell in Azure DevOps pipelines to ensure, that Access Team Templates works when deploying a solution - and I would like to use that.我最近看到了 Joe Griffin 的这篇关于如何在 Azure DevOps 管道中使用 PowerShell 来确保 Access Team Templates 在部署解决方案时工作的博客文章 - 我想使用它。

However, I don't know where I add my parameters to script.但是,我不知道在哪里将参数添加到脚本中。 Can I do that inline or do I need to add the script to my repo to do that?我可以内联执行该操作还是需要将脚本添加到我的存储库中才能执行此操作? If so - how can I do that?如果是这样 - 我该怎么做?

param(
    #objectTypeCode: Unique Code that identifies the table in the environment for the Access Team Template. Always potentially different.
    [Parameter(Mandatory=$true)]
    [int]$objectTypeCode,
    #atName: Name of the Access Team Template
    [Parameter(Mandatory=$true)]
    [String]$atName,
    #accessRights: Number which represents the access rights defined for the template. Refer to this article for details on how to construct: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/web-api/accessrights?view=dynamics-ce-odata-9
    [Parameter(Mandatory=$true)]
    [int]$accessRights,
    #d365URL: URL of the environment to connect to
    [Parameter(Mandatory=$true)]
    [String]$d365URL,
    #clientID: AAD Client ID for the Application User linked to this environment
    [Parameter(Mandatory=$true)]
    [String]$clientID,
    #clientSecret: AAD Client Secret for the Application User linked to this environment
    [Parameter(Mandatory=$true)]
    [String]$clientSecret
)

#Install dependencies
Install-Module Microsoft.Xrm.Data.PowerShell -Scope CurrentUser -Force
#Connect to the D365 environment
$conn = Connect-CrmOnline -ServerUrl $d365URL -ClientSecret $clientSecret -OAuthClientId $clientID -OAuthRedirectUri "http://localhost"
#We first attempt to retrieve the rows if they already exist, and update it accordingly; if this errors, then the row does not exist, so we need to create it instead
Write-Host "Processing Access Team Template for $atName..."
try
{
    $atTemplate = Get-CrmRecord -conn $conn -EntityLogicalName teamtemplate -Id "44396647-CEDF-EB11-BACB-000D3A5810F2" -Fields teamtemplateid,teamtemplatename,objecttypecode,defaultaccessrightsmask,issystem
    $atTemplateId = $atTemplate.teamtemplateid
    Write-Host "Got existing Access Team Template row with ID $atTemplateId!"
    $atTemplate.teamtemplatename = $atName
    $atTemplate.objecttypecode = $objectTypeCode
    $atTemplate.defaultaccessrightsmask = $accessRights
    $atTemplate.issystem = 0
    Set-CrmRecord -conn $conn -CrmRecord $atTemplate
    Write-Host "Successfully updated Access Team Template row with ID $atTemplateId!"
}
catch [System.Management.Automation.RuntimeException]
{
    Write-Host "Access Template row with ID $atTemplateId does not exist, creating..."
    $atTemplateId = New-CrmRecord -conn $conn -EntityLogicalName teamtemplate `
    -Fields @{"teamtemplateid"=[guid]"{44396647-CEDF-EB11-BACB-000D3A5810F2}";"teamtemplatename"=$atName;"objecttypecode"=$objectTypeCode;"defaultaccessrightsmask"=$accessRights;"issystem"=0}   
    Write-Host "Successfully created new Access Template row with ID $atTemplateId"
}
Write-Host "Script execution finished!"

You can use "Azure Powershell" task (If the script has to do something on azure) and can specify path to your powershell file and can add parameter values as in this screenshot,您可以使用“Azure Powershell”任务(如果脚本必须在 azure 上执行某些操作)并且可以指定 powershell 文件的路径,并且可以添加参数值,如下面的屏幕截图所示,

在此处输入图片说明

or you can use "Powershell"task and can add path to the file and parameter,或者您可以使用“Powershell”任务并可以添加文件和参数的路径,

在此处输入图片说明

You can add a Powershell task in your yaml and invoke the Powershell script to perform required task.您可以在 yaml 中添加 Powershell 任务并调用 Powershell 脚本来执行所需的任务。 Script arguments can be set in and passed through DevOps library.可以在 DevOps 库中设置和传递脚本参数。

    - task: AzurePowerShell@5
      displayName: 'Powershell task'
      inputs:
        azureSubscription: '${{ parameters.ServiceConn }}'
        ScriptType: 'FilePath'
        ScriptPath: '[Path to script]/PowershellScriptName.ps1'
        ScriptArguments: '-objectTypeCode $(objectTypeCode) -atName $(atName) -accessRights $(accessRights) -d365URL $(d365URL) -clientID $(clientID) -clientSecret $(clientSecret)'
        azurePowerShellVersion: 'LatestVersion'
        pwsh: true

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

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