将 Azure 存储帐户属性保存到 CSV
Save Azure Storage Account properties to CSV
我正在 运行 按照一段代码从我所有订阅的所有存储帐户中获取存储帐户 TLS 版本。目前唯一的问题是我无法将结果导出为 CSV,因此我无法转发结果。向代码中添加什么以便将存储帐户名称、资源组、订阅和 TSL 版本保存到一行但在不同的列中?
[PsObject[]]$data = @()
#$Subscriptions = Get-AzSubscription
#foreach ($sub in $Subscriptions) {
#Get-AzSubscription -SubscriptionName $sub.Name | Set-AzContext
$storageaccounts = Get-AzStorageAccount
$storageaccounts| ForEach-Object {
Write-Host Checking $_.StorageAccountName
$tls = (Get-AzStorageAccount -ResourceGroupName $_.ResourceGroupName -Name $_.StorageAccountName).MinimumTlsVersion
# create a custom object to store the information about a storage account
$tlsVersionData = [PsObject]@{
"StorageAccountName" = $_.StorageAccountName
"ResourceGroupName" = $_.ResourceGroupName
"TLSVersion" = $tls
}
# add that custom object to the array
$data += $tlsVersionData
}
#}
# write a CSV file containing this data
$data | Export-Csv C:\temp\data.csv
写入主机在屏幕上显示有关存储帐户的正确信息,但我有点不知道如何将数据保存到 CSV。
请尝试如下操作:
[PsObject[]]$data = @() #define an array that will hold the data
$Subscriptions = Get-AzSubscription
foreach ($sub in $Subscriptions) {
Get-AzSubscription -SubscriptionName $sub.Name | Set-AzContext
$storageaccounts = Get-AzStorageAccount
$storageaccounts| ForEach-Object {
Write-Host Checking $_.StorageAccountName
$tls = (Get-AzStorageAccount -ResourceGroupName $_.ResourceGroupName -Name $_.StorageAccountName).MinimumTlsVersion
# create a custom object to store the information about a storage account
$tlsVersionData = [PsObject]@{
"StorageAccountName" = $_.StorageAccountName
"ResourceGroupName" = $_.ResourceGroupName
"TLSVersion" = $tls
}
# add that custom object to the array
$data += $tlsVersionData
}
}
# write a CSV file containing this data
$data | Export-Csv .\data.csv
尽量避免使用 +=
加法向数组(具有静态长度)添加元素。
这样做会浪费时间和内存,因为对于添加到其中的每一项,整个数组都需要在内存中重建。
修复方法是让 PowerShell 为您收集数据,如下所示。
关于 Csv 的输出,将开关 -NoTypeInformation
添加到 Export-Csv cmdlet,这样它就不会在顶部写入字段类型信息。
$Subscriptions = Get-AzSubscription
$data = foreach ($sub in $Subscriptions) {
# suppress output on this line
$null = Get-AzSubscription -SubscriptionName $sub.Name | Set-AzContext
Get-AzStorageAccount | ForEach-Object {
Write-Host Checking $_.StorageAccountName
$tls = (Get-AzStorageAccount -ResourceGroupName $_.ResourceGroupName -Name $_.StorageAccountName).MinimumTlsVersion
# create and output a custom object to store the information about a storage account
[PsCustomObject]@{
StorageAccountName = $_.StorageAccountName
ResourceGroupName = $_.ResourceGroupName
TLSVersion = $tls
}
}
}
# write a CSV file containing this data
$data | Export-Csv -Path .\data.csv -NoTypeInformation
注意:Export-Csv
默认使用逗号作为字段分隔符。如果您想要不同的字符,可以使用 -Delimiter
参数指定。
事实上,您可以像这样在没有 ForEach-Object 循环的情况下编写上面的代码:
$Subscriptions = Get-AzSubscription
$data = foreach ($sub in $Subscriptions) {
# suppress output on this line
$null = Get-AzSubscription -SubscriptionName $sub.Name | Set-AzContext
# let Select-Object output the objects that will be collected in variable $data
Get-AzStorageAccount | Select-Object StorageAccountName, ResourceGroupName,
@{Name = 'TLSVersion'; Expression = {$_.MinimumTlsVersion}}
}
# write a CSV file containing this data
$data | Export-Csv -Path .\data.csv -NoTypeInformation
我正在 运行 按照一段代码从我所有订阅的所有存储帐户中获取存储帐户 TLS 版本。目前唯一的问题是我无法将结果导出为 CSV,因此我无法转发结果。向代码中添加什么以便将存储帐户名称、资源组、订阅和 TSL 版本保存到一行但在不同的列中?
[PsObject[]]$data = @()
#$Subscriptions = Get-AzSubscription
#foreach ($sub in $Subscriptions) {
#Get-AzSubscription -SubscriptionName $sub.Name | Set-AzContext
$storageaccounts = Get-AzStorageAccount
$storageaccounts| ForEach-Object {
Write-Host Checking $_.StorageAccountName
$tls = (Get-AzStorageAccount -ResourceGroupName $_.ResourceGroupName -Name $_.StorageAccountName).MinimumTlsVersion
# create a custom object to store the information about a storage account
$tlsVersionData = [PsObject]@{
"StorageAccountName" = $_.StorageAccountName
"ResourceGroupName" = $_.ResourceGroupName
"TLSVersion" = $tls
}
# add that custom object to the array
$data += $tlsVersionData
}
#}
# write a CSV file containing this data
$data | Export-Csv C:\temp\data.csv
写入主机在屏幕上显示有关存储帐户的正确信息,但我有点不知道如何将数据保存到 CSV。
请尝试如下操作:
[PsObject[]]$data = @() #define an array that will hold the data
$Subscriptions = Get-AzSubscription
foreach ($sub in $Subscriptions) {
Get-AzSubscription -SubscriptionName $sub.Name | Set-AzContext
$storageaccounts = Get-AzStorageAccount
$storageaccounts| ForEach-Object {
Write-Host Checking $_.StorageAccountName
$tls = (Get-AzStorageAccount -ResourceGroupName $_.ResourceGroupName -Name $_.StorageAccountName).MinimumTlsVersion
# create a custom object to store the information about a storage account
$tlsVersionData = [PsObject]@{
"StorageAccountName" = $_.StorageAccountName
"ResourceGroupName" = $_.ResourceGroupName
"TLSVersion" = $tls
}
# add that custom object to the array
$data += $tlsVersionData
}
}
# write a CSV file containing this data
$data | Export-Csv .\data.csv
尽量避免使用 +=
加法向数组(具有静态长度)添加元素。
这样做会浪费时间和内存,因为对于添加到其中的每一项,整个数组都需要在内存中重建。
修复方法是让 PowerShell 为您收集数据,如下所示。
关于 Csv 的输出,将开关 -NoTypeInformation
添加到 Export-Csv cmdlet,这样它就不会在顶部写入字段类型信息。
$Subscriptions = Get-AzSubscription
$data = foreach ($sub in $Subscriptions) {
# suppress output on this line
$null = Get-AzSubscription -SubscriptionName $sub.Name | Set-AzContext
Get-AzStorageAccount | ForEach-Object {
Write-Host Checking $_.StorageAccountName
$tls = (Get-AzStorageAccount -ResourceGroupName $_.ResourceGroupName -Name $_.StorageAccountName).MinimumTlsVersion
# create and output a custom object to store the information about a storage account
[PsCustomObject]@{
StorageAccountName = $_.StorageAccountName
ResourceGroupName = $_.ResourceGroupName
TLSVersion = $tls
}
}
}
# write a CSV file containing this data
$data | Export-Csv -Path .\data.csv -NoTypeInformation
注意:Export-Csv
默认使用逗号作为字段分隔符。如果您想要不同的字符,可以使用 -Delimiter
参数指定。
事实上,您可以像这样在没有 ForEach-Object 循环的情况下编写上面的代码:
$Subscriptions = Get-AzSubscription
$data = foreach ($sub in $Subscriptions) {
# suppress output on this line
$null = Get-AzSubscription -SubscriptionName $sub.Name | Set-AzContext
# let Select-Object output the objects that will be collected in variable $data
Get-AzStorageAccount | Select-Object StorageAccountName, ResourceGroupName,
@{Name = 'TLSVersion'; Expression = {$_.MinimumTlsVersion}}
}
# write a CSV file containing this data
$data | Export-Csv -Path .\data.csv -NoTypeInformation