[英]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:需要注意的事项:
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
.-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
。Get-AzNetworkInterface
, so you can just specify the name in the parameter. Get-AzNetworkInterface
的一个参数,因此您只需在参数中指定名称即可。 Using Where-Object
is superfluous.Where-Object
是多余的。$list = Get-AzNetworkInterface -ResourceGroupName $ResourceGroup
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
,因为它没有重复的条件。
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.