摆脱Azure PowerShell脚本中过时参数的任何方法(AzureRM更改为Az) [英] Any way to get rid of an obsolete parameter in Azure PowerShell script (AzureRM to Az changing)

查看:70
本文介绍了摆脱Azure PowerShell脚本中过时参数的任何方法(AzureRM更改为Az)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可能已经知道,MSFT摆脱了AzureRM cmdlet,转而支持Az.由于提议的本机别名"Enable-AzureRmAlias"已存在,因此存在许多问题.似乎停止更新了.

我在一个存储库中有一个基于AzureRM的脚本,该脚本由Azure DevOps发布管道步骤功能(基于Azure的PowerShell)触发,

具有以下代码:

  $ var =(Get-AzureKeyVaultSecret -VaultName $ vaultName-Name $ Key).SecretValueText 

"Enable-AzureRmAlias"表示命令也已激活...,它会转换如下代码:

  $ var =(Get-AzKeyVaultSecret -VaultName $ vaultName-Name $ Key).SecretValueText 

问题是,.SecretValueText"不久前已被弃用.取而代之的是,已将新参数添加到Get-AzKeyVaultSecret cmdlet-"-AsPlainText"

所以...理论上,最终构造必须是这样的:

 <代码> $ var = Get-AzKeyVaultSecret -VaultName $ vaultName-Name $ Key -AsPlainText 

挑战!由于必须具有向后兼容性,因此无法将存储库中的原始脚本升级到Az.解决此问题的唯一方法-是在Azure PowerShell内联脚本中创建某种别名(这会在存储库中触发主脚本)我坚持使用此".SecretValueText"

我最初的想法是将以下内容放入内联脚本中:

 功能Get-AzKeyVaultSecretNew {参数($ vaultName,$键)$ var = Get-AzKeyVaultSecret -VaultName $ vaultName -Name $ Key -AsPlainText返回$ var}设置别名-名称Get-AzKeyVaultSecret-值Get-AzKeyVaultSecretNew 

关于如何实现此目标的任何想法?

解决方案

您可以尝试使用以下变通方法替换下面的代码:

(Get-AzureKeyVaultSecret -VaultName $ vaultName-Name $ Key).SecretValueText

使用

2, -AsPlainText 参数仅在最新的az 5.3.0版本中可用.由于云代理中安装的版本是4.7.0.您需要先安装az 5.3.0版本,然后才能执行脚本.见下文.使用powershell任务安装az 5.3.0版本.

  New-Item -Path"C:\ Modules"-名称"az_5.3.0&";-ItemType目录"保存模块-名称AZ-必需版本5.3.0-路径"C:\ Modules \ az_5.3.0&"; 

3,然后您可以直接在azure powershell任务中调用脚本.

As you may know, MSFT is getting rid of AzureRM cmdlets in favor of Az. There are a lot of issues regarding this since the proposed native aliases "Enable-AzureRmAlias" seems to stop being updated.

I have a script based on AzureRM in one repo, that triggers by Azure DevOps release pipeline step function (Azure PowerShell based),

that has the following piece of code:

$var = (Get-AzureKeyVaultSecret -VaultName $vaultName-Name $Key).SecretValueText

"Enable-AzureRmAlias" command activated as well..., that converts the code like this:

$var = (Get-AzKeyVaultSecret -VaultName $vaultName-Name $Key).SecretValueText

The problem is, that ".SecretValueText" was deprecated a while ago. Instead of it, a new parameter has been added to the Get-AzKeyVaultSecret cmdlet - "-AsPlainText"

so... theoretically the final construction has to be like this:

$var = Get-AzKeyVaultSecret -VaultName $vaultName-Name $Key -AsPlainText

Challenges! I can't upgrade the original script in the repo to Az due to the necessity of back-compatibility. The only way to solve it - is to create some kind of alias in Azure PowerShell inline script (that triggers the main script in the repo) I stuck with this ".SecretValueText"

My original idea to put the following into the inline script doesn't seem to be working:

function Get-AzKeyVaultSecretNew {
Param(
  $vaultName,
  $Key
     )
   $var = Get-AzKeyVaultSecret -VaultName $vaultName -Name $Key -AsPlainText
   return $var
} 

Set-Alias -Name Get-AzKeyVaultSecret -Value Get-AzKeyVaultSecretNew

Any ideas on how to accomplish this?

解决方案

You can try using below workaround to replace below piece of code:

(Get-AzureKeyVaultSecret -VaultName $vaultName-Name $Key).SecretValueText

with Get-AzKeyVaultSecret -VaultName $vaultName -Name $Key -AsPlainText via using RegEx Find & Replace task. Check below steps:

1, Add task RegEx Find & Replace to replace the orginal code with the converted code. See below:

    FindRegex: '\(Get-AzureKeyVaultSecret -VaultName \$vaultName -Name \$Key\)\.SecretValueText'
    ReplaceRegex: 'Get-AzKeyVaultSecret -VaultName $vaultName -Name $Key -AsPlainText'
    

2, -AsPlainText parameter is only available in the latest az 5.3.0 version. Since the version installed in cloud agent is 4.7.0. You need to install the az 5.3.0 version before executing your script. See below. Use a powershell task to install az 5.3.0 version.

New-Item -Path "C:\Modules" -Name "az_5.3.0" -ItemType "directory"

Save-Module -Name AZ -RequiredVersion 5.3.0 -Path "C:\Modules\az_5.3.0"

3, Then you can invoke your script in the azure powershell task directly.

这篇关于摆脱Azure PowerShell脚本中过时参数的任何方法(AzureRM更改为Az)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆