我有一个命令,返回AWS中用户所属的组数。用户可以所属的组数可能会有所不同。
我想将这些数据存储在excel中,每个用户的名字旁边都有一个组列表。我希望每个小组都有自己的领域。但是,群体的数量可能会有所不同。
例如,以下用户都有不同数量的组:
aws iam list-groups-for-user --user-name tdunphy --profile=company-prod | jq -r '.Groups[].GroupName'
grp-quicksight
agility-admin
grp-account-bill
grp-sag
grp-flow-log-user
company_SAG
grp-cloud-formation
grp-cloudops
aws iam list-groups-for-user --user-name broberts --profile=company-prod | jq -r '.Groups[].GroupName'
agility-admin
grp-account-bill
grp-sag
grp-cloud-formation
grp-cloudops
aws iam list-groups-for-user --user-name ejimenez --profile=company-prod | jq -r '.Groups[].GroupName'
agility-admin
grp-cloudops理想情况下,我希望像这样水平地显示这些数据:
User Name Groups
tdunphy grp-quicksight agility-admin grp-account-bill grp-sag (etc)
broberts agility-admin grp-account-bill grp-sag grp-cloud-formation每个组名都应该在它自己的列中。但我不确定这样会不会很整洁。如果这样做看起来更好的话,我愿意横向列出组名。
我将把AWS命令中的组名转储到文本文件中。然后以编程方式将该文本文件转储到CSV中。
如何存储每个用户的每个组列表,每个组都有自己的字段?我无法预测用户将属于多少组。是否有一个干净的方法来实现这一点?
发布于 2018-09-15 08:35:52
此代码生成以下数据。

有几个假设。
Sub BuildAWSGroups()
Dim AWSPrefix As Long, FindName As Long, NextFree As Long, LastFree As Long, EndRow As Long, runningtotal As Long
Dim UserName As String
Dim awsrow As Long, outputrow As Long
Dim wsi, wso As Worksheet
' name the sheets
Set wsi = Sheets("aws_input")
Set wso = Sheets("aws_output")
' length of the AWS prefix -> aws iam list-groups-for-user --user-name
AWSPrefix = 41
'set values to all the variables
FindName = 0
NextFree = 0
LastFree = 0
EndRow = 0
runningtotal = 0
awsrow = 1
outputrow = 2
'Find the last non-blank cell in column A(1)
EndRow = wsi.Cells(Rows.Count, 1).End(xlUp).Row
' do stuff so long as you don't go past the last row of data
Do While runningtotal <= EndRow
' the last test for NextFree throws an error, so we'll look for the error
On Error Resume Next
' get the next non-blank row number
NextFree = wsi.Range("a" & awsrow & ":a" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
' is there was an error then we need to calculate the next non-blank cell differently
If NextFree = LastFree Then
NextFree = Cells.Find(What:="*", _
After:=Range("a" & (LastFree + 2)), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
NextFree = awsrow + NextFree
End If
' find location of the user name in the aws data row
wsi.Activate
FindName = InStr((AWSPrefix + 1), wsi.Cells(awsrow, 1).Value, " ")
'extract the user name
UserName = Trim(Mid(wsi.Cells(awsrow, 1).Value, (AWSPrefix + 1), (FindName - AWSPrefix)))
'copy user name to output
wso.Cells(outputrow, 1).Value = UserName
' copy the aws groups for this user
wsi.Activate
wsi.Range(Cells((awsrow + 1), 1), Cells((NextFree - 1), 1)).Copy
'paste and transpose the groups to the user row on the output sheet
wso.Activate
wso.Cells(outputrow, 2).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
' update variables for the loop
LastFree = NextFree
awsrow = LastFree + 2
outputrow = outputrow + 1
runningtotal = awsrow
Loop
End Subhttps://stackoverflow.com/questions/52322169
复制相似问题