Repository navigation
Expand file tree
/
Copy pathbatch-update-users.sh
More file actions
executable file
·107 lines (93 loc) · 2.62 KB
/
Copy pathbatch-update-users.sh
File metadata and controls
executable file
·107 lines (93 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
#
# Batch Update Users Script
#
# Update multiple users from a CSV file containing user information.
#
# CSV Format:
# email,department,title,manager
#
# Usage:
# ./batch-update-users.sh [csv-file]
set -euo pipefail
# Input file (default: users-to-update.csv)
INPUT_FILE="${1:-users-to-update.csv}"
# Check if file exists
if [ ! -f "$INPUT_FILE" ]; then
echo "Error: File not found: $INPUT_FILE"
echo ""
echo "Usage: $0 [csv-file]"
echo ""
echo "CSV format:"
echo " email,department,title,manager"
echo " jdoe@example.com,Engineering,Senior Engineer,manager@example.com"
echo " jsmith@example.com,Sales,Account Executive,sales-mgr@example.com"
echo ""
echo "Example CSV file: users-to-update.csv"
exit 1
fi
# Count total users
TOTAL_USERS=$(($(wc -l < "$INPUT_FILE") - 1))
if [ "$TOTAL_USERS" -lt 1 ]; then
echo "Error: CSV file contains no user records"
exit 1
fi
echo "========================================="
echo "Batch User Update"
echo "========================================="
echo "Input file: $INPUT_FILE"
echo "Total users: $TOTAL_USERS"
echo "========================================="
echo ""
# Show preview
echo "Preview of first 3 users:"
head -n 4 "$INPUT_FILE" | column -t -s,
echo ""
# Confirm before proceeding
read -p "Proceed with batch update? (y/n) " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Cancelled"
exit 0
fi
echo ""
# Initialize counters
SUCCESS_COUNT=0
FAIL_COUNT=0
CURRENT=0
# Process each user
tail -n +2 "$INPUT_FILE" | while IFS=, read -r EMAIL DEPT TITLE MANAGER; do
CURRENT=$((CURRENT + 1))
echo "[$CURRENT/$TOTAL_USERS] Processing: $EMAIL"
echo " Department: $DEPT"
echo " Title: $TITLE"
echo " Manager: $MANAGER"
# Update the user
if gac user update \
--dept "$DEPT" \
--title "$TITLE" \
--manager "$MANAGER" \
"$EMAIL" 2>&1 | grep -q "success\|updated"; then
echo " ✓ Updated successfully"
SUCCESS_COUNT=$((SUCCESS_COUNT + 1))
else
echo " ✗ Update failed"
FAIL_COUNT=$((FAIL_COUNT + 1))
fi
echo ""
# Small delay to avoid rate limiting
sleep 0.5
done
echo "========================================="
echo "Batch Update Complete"
echo "========================================="
echo "Total users processed: $TOTAL_USERS"
echo "Successful updates: $SUCCESS_COUNT"
echo "Failed updates: $FAIL_COUNT"
echo "========================================="
if [ "$FAIL_COUNT" -gt 0 ]; then
echo "⚠ Some updates failed. Check error messages above."
exit 1
else
echo "✓ All users updated successfully"
fi