简体   繁体   English

在运行 Azure DevOps 管道时,如果我们不给管道的 powershell 脚本中使用的变量任何值

[英]While running Azure DevOps pipeline, If we dont give any value to the variable which is used in powershell script of pipeline

I have a powershell script in which variable "ResourceGroup" variable is used.我有一个 powershell 脚本,其中使用了变量“ResourceGroup”变量。 We need to give value to Resource group while running the pipeline.If we dont give any value to that variable we need to get all the resource groups from the whole subscription of azure.Please suggest.我们需要在运行管道时为资源组赋值。如果我们不为该变量赋值,我们需要从 azure 的整个订阅中获取所有资源组。请建议。

This the code we are using这是我们正在使用的代码

$excel=@()
    $list = Get-AznetworkInterface |where-Object {$_.ResourceGroupName -Clike '*$(givenVarible)'} |Select-Object
    foreach ($i in $list) {
   $x = " " | Select-Object SID1_name,SID1_VIP,SID2_name,SID2_VIP,SID3_name,SID3_VIP
   $case =1
   While ($case -1t $i.IpConfigurations.Count)
   {
   switch ($case){
   1 {
      $x.SID1_name = $i.IPconfigurations[$case];
     $x.SID1_VIP = $i.IPconfigurations[$case].PrivateIpaddress;
     break
     }
    2 {
     $x.SID2_name = $i.IPconfigurations[$case];
     $x.SID2_VIP = $i.IPconfigurations[$case].PrivateIpaddress;
     break
     }
    3 {
    $x.SID1_name = $i.IPconfigurations[$case];
     $x.SID1_VIP = $i.IPconfigurations[$case].PrivateIpaddress;
     break
     }
     $case =$case+1
     $excel +=$x
     $excel | Format-Table SID1_name,SID1_VIP,SID2_name,SID2_VIP,SID3_name,SID3_VIP
     $excel |Export-Csv  -NTI - Path "$(Build.ArtifactoryStagingDirectory)/report.csv"

It looks like you just need to add a Param block so you can provide this parameter.看起来您只需要添加一个Param块,就可以提供此参数。 However, if you want all Azure Network Interfaces, then you just need to use Get-AzNetworkInterface without parameters.但是,如果您想要所有 Azure 网络接口,那么您只需要使用不带参数Get-AzNetworkInterface

Param (
    # Mandatory=$false ensures that the user does not have to specify this information.
    [parameter(Mandatory=$false)][string]$ResourceGroup
)

# Checks if the user has used the parameter
if ($PSBoundParamters.ContainsKey('ResourceGroup')) {
    $list = Get-AznetworkInterface |where-Object {$_.ResourceGroupName -Clike $ResourceGroup} |Select-Object
}
else {
    $List = Get-AzNetworkInterface
}
# Rest of script

You would then run it like this:然后你会像这样运行它:

PS C:\My_Dir> .\MyScript.ps1 -ResourceGroup MyResourceGroupName

Or without parameters to obtain all the Network Interfaces.或者不带参数来获取所有的网络接口。

PS C:\My_Dir> .\MyScript.ps1

Things to be aware of:需要注意的事项:

  • If you do not know, clike is a case sensitive match.如果你不知道, clike是区分大小写的匹配。 I don't think naming of Resource Groups in Azure is case sensitive, so it is probably better just to use -like .我认为 Azure 中资源组的命名不区分大小写,因此最好使用-like
  • Select-Object selects chosen properties from an object, so it is not necessary in your script. Select-Object从 object 中选择选定的属性,因此在您的脚本中没有必要。 If you wanted just the name or other specific values from the Network Interface, you would use ... | Select-Object -Property Name,IP,etc如果您只需要网络接口的名称或其他特定值,您可以使用... | Select-Object -Property Name,IP,etc ... | Select-Object -Property Name,IP,etc . ... | Select-Object -Property Name,IP,etc
  • ResourceGroupName is a parameter of Get-AzNetworkInterface , so you can just specify the name in the parameter. ResourceGroupName 是Get-AzNetworkInterface的一个参数,因此您只需在参数中指定名称即可。 Using Where-Object is superfluous.使用Where-Object是多余的。
$list = Get-AzNetworkInterface -ResourceGroupName $ResourceGroup
  • You should check for null in the event that the user uses a Resource Group that does not contain any Network Interface resources.如果用户使用不包含任何网络接口资源的资源组,您应该检查 null。

By the way, you are missing a closing set of curly braces for your while loop and it is not necessary to use break in your switch statement because it has no repeated conditions.顺便说一句,您的while循环缺少一组闭合花括号,并且没有必要在switch语句中使用break ,因为它没有重复的条件。

EDIT编辑

You could in reality replace your while loop and switch with a smaller section of code, within a for loop.实际上,您可以在 for 循环中用一小段代码替换您的while循环和switch I tested with some made up objects.我用一些虚构的对象进行了测试。

for ($Count = 0; $count -lt $i.IpConfigurations.Count; $count++) {
    $x."SID$($count + 1)_name" = $i.IPConfigurations[$Count].Name
    $x."SID$($count + 1)_VIP" = $i.IPConfigurations[$Count].PrivateIpAddress
}

Returns:回报:

SID1_name : @{IP=192.168.1.1; AdapterName=Test; PrivateIpAddress=192.168.1.1}
SID1_VIP  : 192.168.1.1
SID2_name : @{IP=192.168.1.2; AdapterName=Test2; PrivateIpAddress=192.168.1.2}
SID2_VIP  : 192.168.1.2
SID3_name : @{IP=192.168.1.3; AdapterName=Test3; PrivateIpAddress=192.168.1.3}
SID3_VIP  : 192.168.1.3

How Does It Work?它是如何工作的?

The parameter names take a variable so you do not need to repeat them, also, if there are more than three objects or IP configurations, you do not need to specify them specifically in your script.参数名称采用变量,因此您无需重复它们,此外,如果存在三个以上的对象或 IP 配置,则无需在脚本中专门指定它们。 You could even use Add-Member to dynamically create them as they are needed.您甚至可以根据需要使用Add-Member动态创建它们。 Just need to remember that the first object in an array is value 0 and you are starting the numbering convention of your parameters at 1, so the plus 1 is necessary in the parameter name.只需要记住数组中的第一个 object 是值0 ,并且您的参数编号约定从 1 开始,因此参数名称中需要加 1。

Here is an example of dynamically adding your members:以下是动态添加成员的示例:

$x = New-Object psobject
for ($Count = 0; $count -le $i.IpConfigurations.Count; $count++) {
    $x | Add-Member -MemberType NoteProperty -Name "SID$($Count + 1)_Name" -Value $i.IPConfigurations[$Count].Name
    $x | Add-Member -MemberType NoteProperty -Name "SID$($Count + 1)_VIP" -Value $i.IPConfigurations[$Count].PrivateIpAddress
}

Suggested script.建议的脚本。

Param (
    # Mandatory=$false ensures that the user does not have to specify this information.
    [parameter(Mandatory=$false)][string]$ResourceGroup
)
$Excel = @()

# Checks if the user has used the parameter
if ($PSBoundParameters.ContainsKey('ResourceGroup')) {
    $List = Get-AzNetworkInterface -ResourceGroupName $ResourceGroup # | Select-Object -Property Name,IPConfigurations # Uncomment to select these properties
}
else {
    $List = Get-AzNetworkInterface # | Select-Object -Property Name,IPConfigurations # Uncomment to select these properties
}

# If you don't have any results, no need to continue.
if ($null -ne $List) {
    foreach ($i in $List) {
        $x = New-Object psobject
        for ($Count = 0; $count -le $i.IpConfigurations.Count; $count++) {
            # Add member properties as they are required
            $x | Add-Member -MemberType NoteProperty -Name "SID$($Count + 1)_Name" -Value $i.IPConfigurations[$Count].Name
            $x | Add-Member -MemberType NoteProperty -Name "SID$($Count + 1)_VIP" -Value $i.IPConfigurations[$Count].PrivateIpAddress
        }
        # Update your array
        $Excel += $x
    }
    # Export directly to CSV, no need to use Format-Table here, unless you want to see the output in the console too.
    $Excel | Export-Csv -Path C:\Test\SampleReport.csv -NoClobber -NoTypeInformation -Encoding UTF8 -Force
}

暂无
暂无

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

相关问题 在运行 Powershell 脚本的 Azure DevOps 管道中传递脚本参数 - Pass a script argument in Azure DevOps pipeline which runs a Powershell script 将 Azure Powershell 变量分配给 DevOps 管道变量 - Assign Azure Powershell variable to DevOps Pipeline variable 将变量值从 Powershell 任务中的一个脚本传递到 Azure DevOps 发布管道中下一个 Powershell 任务中的另一个脚本 - Pass variable value from one Powershell script in Powershell task to another script in the next Powershell task in Azure DevOps Release Pipeline 在 Azure DevOps 管道中运行 ansible 脚本 - Running ansible script in Azure DevOps pipeline “创建管道时出错。” 在 Azure 管道自托管代理中运行 powershell 脚本时 - "An error occurred while creating the pipeline." when running powershell script in Azure pipeline self-hosted agent 无法在 Azure DevOps 管道“解析错误”中运行 PowerShell 脚本 - Can't run PowerShell script in Azure DevOps Pipeline 'parse error' Azure DevOps 管道 - 带管道变量的条件表达式 - Azure DevOps Pipeline - condition expression with pipeline variable 如何在 Azure Devops 发布管道中使用 PowerShell 设置环境变量? - How to set an environment variable with PowerShell in Azure Devops release pipeline? Azure DevOps:如何从 Release Pipeline 中的 PowerShell 脚本构建 Azure Pipeline 检索构建工件? - Azure DevOps: How to retrieve a build artifact from build Azure Pipeline from a PowerShell Script in Release Pipeline? 在 Azure Devops 中获取正在运行的 devops 管道实例 - Get running instances of devops pipeline in Azure Devops
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM