我有一个bash脚本,它的工作方式类似于从CSV文件在Azure Portal中部署资源。但问题是,
是否可以从xlsx文件做同样的工作。
我不知道该怎么做。
有人来帮帮我吗?
我把我的脚本& xlsx放在下面:
#!/bin/bash
az login --tenant ca64240a-78a0-4116-9504-eab6ead602b9 # DEV
az login --tenant ca64240a-78a0-4116-9504-eab6ead602b9 # TEST
# az login --tenant ca64240a-78a0-4116-9504-eab6ead602b9 # STAGE
# az login --tenant ca64240a-78a0-4116-9504-eab6ead602b9 # PROD
# Prompt for the CSV file path
read -p "Enter the path to the CSV file for SP to Group Member : " csv_file
# Check if the CSV file exists
if [ -f "$csv_file" ]; then
# Read CSV file and populate arrays
while IFS=',' read -r GroupID ServicePrincipal Subscription; do
csv_row+=("$GroupID;$ServicePrincipal;$Subscription")
done < "$csv_file"
# Process Service Principals
echo " Creating Service Principles to groups .... "
for row in "${csv_row[@]}"; do
IFS=';' read -ra ARRAY <<< "$row"
groupId=${ARRAY[0]}
sp_name=${ARRAY[1]}
subscription=${ARRAY[2]}
spObjId=$(az ad sp list --display-name "$sp_name" --query '[0].id' --output tsv)
echo $spObjId
if [ -n "$spObjId" ]; then
# set the subscription
az account set --subscription $subscription
# execute the command against the subscription
az ad group member add --group "$groupId" --member-id "$spObjId"
if [ $? -eq 0 ]; then
echo "Service Principal with Name: $sp_name (Object ID: $spObjId) added to AAD Group '$groupId' successfully."
else
echo "Failed to add Service Principal with Name: $sp_name (Object ID: $spObjId) to AAD Group '$groupId'."
fi
else
echo "Service Principal with Name: $sp_name not found."
fi
done
else
echo "CSV file not found at the specified path."
fi
字符串
1条答案
按热度按时间cuxqih211#
我建议最简单的方法是安装xlsx2csv,或者使用相关的包管理器,或者转到https://github.com/dilshod/xlsx2csv
然后你可以简单地将xlsx文档转换为csv,然后你的脚本将以同样的方式工作。
或者,xlsx文档只是xml文档的压缩文件,你可以解压缩xlsx,然后修改你的脚本直接从xml填充,或者你可以将xml转换成csv,然后运行你的脚本不编辑。
这应该让你开始。