簡體   English   中英

在 Azure DevOps 管道中的 PowerShell 中使用參數

[英]Use parameter in PowerShell in Azure DevOps pipeline

我們最近開始將 Azure DevOps Pipelines 用於我們的 Dynamics 365 CRM 實施,但它對我來說仍然是新的

我最近看到了 Joe Griffin 的這篇關於如何在 Azure DevOps 管道中使用 PowerShell 來確保 Access Team Templates 在部署解決方案時工作的博客文章 - 我想使用它。

但是,我不知道在哪里將參數添加到腳本中。 我可以內聯執行該操作還是需要將腳本添加到我的存儲庫中才能執行此操作? 如果是這樣 - 我該怎么做?

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!"

您可以使用“Azure Powershell”任務(如果腳本必須在 azure 上執行某些操作)並且可以指定 powershell 文件的路徑,並且可以添加參數值,如下面的屏幕截圖所示,

在此處輸入圖片說明

或者您可以使用“Powershell”任務並可以添加文件和參數的路徑,

在此處輸入圖片說明

您可以在 yaml 中添加 Powershell 任務並調用 Powershell 腳本來執行所需的任務。 可以在 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