我正在创建一个Azure PS脚本,它将获取所有NSG,然后查找它是否已连接到子网或NIC
当我运行以下命令时,它给出的输出没有“连接到子网”列。但是,当我使用-ResourceGroupName属性运行相同的cmdlet时,它将给出“附加到子网”和“附加到Nic”的输出NoteProperty 这是所希望的。
我无法调试为什么不使用Get-AzureRmNetworkSecurityGroup
添加“附加到子网”列
$NSGdetails = @()
Get-AzureRmNetworkSecurityGroup | ForEach-Object {
$D2 = New-Object PSObject
Write-Host $_.Name
$subnet = $_.Subnets.Id
$nic = $_.NetworkInterfaces.id
$D2 | Add-Member -MemberType NoteProperty -Name "NSG Name" -Value $_.Name -Force
if ($subnet -ne $null) {
$subnetdetail = $subnet.Split('/')[-1]
$D2 | Add-Member -MemberType NoteProperty -Name "Attached To Subnet" -Value $subnetdetail -Force
}
if ($nic -ne $null) {
$nicdetail = $nic.Split('/')[-1]
$D2 | Add-Member -MemberType NoteProperty -Name "Attached To NIC" -Value $nicdetail -Force
}
$NSGdetails += $D2
}
$NSGdetails | Export-Csv -Path "C:\temp\allnicdetails.csv" -NoTypeInformation -Force
下面是Get-AzureRmNetworkSecurityGroup -ResourceGroupName RAH-AQ
PS C:\Users\021176\Desktop> $NSGdetails
NSG Name Attached To Subnet Attached To NIC
-------- ------------------ ---------------
linuxvm-nsg default linuxvm15
redhat-nsg redhat396
下面提到带有Get-AzureRmNetworkSecurityGroup
的输出
PS C:\Users\021176\Desktop> $NSGdetails
NSG Name Attached To NIC
-------- ---------------
demolinux-nsg demolinux76
devnsg devnic
mastesta1-nsg mastesta1275
TutorialVM1 TutorialVM1
filenetapp-nsg filenetapp828
testvm1NSG
nehatest-nsg nehatest995
RHEL-nsg rhel858
linuxvm-nsg linuxvm15
linuxvm-nsg linuxvm15
redhat-nsg redhat396
SQLBackup-nsg sqlbackup454
opnvpntest-nsg opnvpntest764
答案 0 :(得分:1)
Attached To Subnet
属性并未出现在CSV中,因为只有 some 个对象具有该属性,而Export-Csv
决定根据哪个属性解析和编写 first 输入对象。将您的代码更改为始终添加属性,无论其是否有值:
if ($subnet -ne $null) {
$subnetdetail = $subnet.Split('/')[-1]
} else {
$subnetdetail = $null
}
$D2 | Add-Member -MemberType NoteProperty -Name "Attached To Subnet" -Value $subnetdetail -Force
if ($nic -ne $null) {
$nicdetail = $nic.Split('/')[-1]
} else {
$nicdetail = $null
}
$D2 | Add-Member -MemberType NoteProperty -Name "Attached To NIC" -Value $nicdetail -Force