azure 如何通过bash脚本执行或读取xlsx文件数据

jvidinwx  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(132)

我有一个bash脚本,它的工作方式类似于从CSV文件在Azure Portal中部署资源。但问题是,
是否可以从xlsx文件做同样的工作。
我不知道该怎么做。
有人来帮帮我吗?
我把我的脚本& xlsx放在下面:

  1. #!/bin/bash
  2. az login --tenant ca64240a-78a0-4116-9504-eab6ead602b9 # DEV
  3. az login --tenant ca64240a-78a0-4116-9504-eab6ead602b9 # TEST
  4. # az login --tenant ca64240a-78a0-4116-9504-eab6ead602b9 # STAGE
  5. # az login --tenant ca64240a-78a0-4116-9504-eab6ead602b9 # PROD
  6. # Prompt for the CSV file path
  7. read -p "Enter the path to the CSV file for SP to Group Member : " csv_file
  8. # Check if the CSV file exists
  9. if [ -f "$csv_file" ]; then
  10. # Read CSV file and populate arrays
  11. while IFS=',' read -r GroupID ServicePrincipal Subscription; do
  12. csv_row+=("$GroupID;$ServicePrincipal;$Subscription")
  13. done < "$csv_file"
  14. # Process Service Principals
  15. echo " Creating Service Principles to groups .... "
  16. for row in "${csv_row[@]}"; do
  17. IFS=';' read -ra ARRAY <<< "$row"
  18. groupId=${ARRAY[0]}
  19. sp_name=${ARRAY[1]}
  20. subscription=${ARRAY[2]}
  21. spObjId=$(az ad sp list --display-name "$sp_name" --query '[0].id' --output tsv)
  22. echo $spObjId
  23. if [ -n "$spObjId" ]; then
  24. # set the subscription
  25. az account set --subscription $subscription
  26. # execute the command against the subscription
  27. az ad group member add --group "$groupId" --member-id "$spObjId"
  28. if [ $? -eq 0 ]; then
  29. echo "Service Principal with Name: $sp_name (Object ID: $spObjId) added to AAD Group '$groupId' successfully."
  30. else
  31. echo "Failed to add Service Principal with Name: $sp_name (Object ID: $spObjId) to AAD Group '$groupId'."
  32. fi
  33. else
  34. echo "Service Principal with Name: $sp_name not found."
  35. fi
  36. done
  37. else
  38. echo "CSV file not found at the specified path."
  39. fi

字符串

cuxqih21

cuxqih211#

我建议最简单的方法是安装xlsx2csv,或者使用相关的包管理器,或者转到https://github.com/dilshod/xlsx2csv
然后你可以简单地将xlsx文档转换为csv,然后你的脚本将以同样的方式工作。
或者,xlsx文档只是xml文档的压缩文件,你可以解压缩xlsx,然后修改你的脚本直接从xml填充,或者你可以将xml转换成csv,然后运行你的脚本不编辑。
这应该让你开始。

相关问题