gpt4 book ai didi

azure - 如果没有数据,需要创建带标题的 Powershell 输出文件

转载 作者:行者123 更新时间:2023-12-03 03:26:55 26 4
gpt4 key购买 nike

我创建了一个从 Azure 导出防火墙规则的脚本,它工作得很好,但是即使没有数据,我也需要创建带标题的 .csv。目前它正在创建一个空白文件。

``function create($path) {
$exists = Test-Path -path $path
Write-Host "tried the following path: $path, it" $(If ($exists) {"Exists"} Else {"Does not Exist!"})
if (!($exists)) { New-Item $path -itemType Directory }
}

# reading file contents
$subs_file = "C:\script\Subscriptions.xlsx"
$azSubs = Import-Excel $subs_file
$azSubs
$output_folder = "C:\audit-automation"
# creating folder for outputing data
create("$output_folder")
# New-Item $output_folder -itemType Directory

# iterating over subscriptions
ForEach ( $sub in $azSubs ) {
# sub
$azsub = $sub.Subscription
# app
$app = $sub.Application
$azsub
$app
# creating folder to save data for apps
# New-Item $output_folder\$app -itemType Directory
# setting config for azure
Set-AzContext -SubscriptionName $azsub

# FIREWALL RULES
$azNsgs = Get-AzNetworkSecurityGroup
# iterating over retrieved NSGs
$Output = ForEach ( $azNsg in $azNsgs ) {
#Export custom rules
Get-AzNetworkSecurityRuleConfig -NetworkSecurityGroup $azNsg | `
Select-Object @{label = 'NSG Name'; expression = { $azNsg.Name } }, `
@{label = 'NSG Location'; expression = { $azNsg.Location } }, `
@{label = 'Rule Name'; expression = { $_.Name } }, `
@{label = 'Source'; expression = { $_.SourceAddressPrefix } }, `
@{label = 'Source Application Security Group'; expression = { $_.SourceApplicationSecurityGroups.id.Split('/')[-1] } },
@{label = 'Source Port Range'; expression = { $_.SourcePortRange } }, Access, Priority, Direction, `
@{label = 'Destination'; expression = { $_.DestinationAddressPrefix } }, `
@{label = 'Destination Application Security Group'; expression = { $_.DestinationApplicationSecurityGroups.id.Split('/')[-1] } }, `
@{label = 'Destination Port Range'; expression = { $_.DestinationPortRange } }, `
@{label = 'Resource Group Name'; expression = { $azNsg.ResourceGroupName } },
@{label = 'Subscription Name'; expression = { $azSub } }
}
# creating folder to save
# New-Item $output_folder\$app\firewall_rules -itemType Directory
create("$output_folder\$app")
$Output | Export-Csv -Path $output_folder\$app\$app-firewall_rules_data$((Get-Date).ToString("yyyy-MM-dd")).csv -Append`

最佳答案

测试 $Output 变量是否包含任何内容 - 如果没有,则创建一个与实际输出对象形状相同的“虚拟对象”,并将其与 ConvertTo-Csv 生成 header :

# ...
create("$output_folder\$app")
$outputPath = "$output_folder\$app\$app-firewall_rules_data$((Get-Date).ToString("yyyy-MM-dd")).csv"

if($Output) {
$Output | Export-Csv -Path $outputPath -Append -NoTypeInformation
}
elseif (-not(Test-Path $outputPath -PathType Leaf)) {
# no output data, and the file doesn't already exist - create headers only
$dummyObject = [pscustomobject]@{
'NSG Name' = ''
'NSG Location' = ''
'Rule Name' = ''
'Source' = ''
'Source Application Security Group' = ''
'Source Port Range' = ''
'Destination' = ''
'Destination Application Security Group' = ''
'Destination Port Range' = ''
'Resource Group Name' = ''
'Subscription Name' = ''
}

# generate dummy CSV document, then discard the row representing the dummy object
$header = $dummyObject |ConvertTo-Csv -NoTypeInformation |Select -SkipLast 1
$header |Set-Content $outputPath

}

关于azure - 如果没有数据,需要创建带标题的 Powershell 输出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75460918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com