For AWS administrators and cloud engineers, managing Amazon WorkSpaces across multiple regions can be a laborious undertaking. Although AWS offers various tools for handling resources across different regions, there may be instances where custom scripts are necessary to automate certain tasks. In this blog post, we will delve into a PowerShell script that streamlines the process of searching for users with multiple Amazon WorkSpaces in various regions, grouping them accordingly, and exporting the data to a csv file for further analysis.
Prerequisites
Ensure you have the following:
- An AWS account with permission to access Workspaces.
- PowerShell installed on your computer.
- AWS PowerShell module installed (you can install it using the command
Install-Module -Name AWSPowerShell
).
The PowerShell Script
The PowerShell script below searches for users having workspaces in multiple regions, and exports the results to a CSV file. You can modify the $regions
array to include the regions you want to search for workspaces.
The script can be broken down into the following parts:
Defining the regions to search for WorkSpaces
Initializing an empty hashtable to store WorkSpaces by user
Looping through each region to get a list of WorkSpaces in that region using the
Get-WKSWorkspace
cmdletLooping through each Workspace and grouping them by user
Checking if each user has WorkSpaces in all regions
Outputting a CSV file with information about each user’s WorkSpaces
Code in Detail
Here’s a breakdown of what each section does.
1
2
3
4
5
6
# Define the regions to search for Workspaces
$regions = @("us-east-1", "ap-south-1", "eu-west-2", "sa-east-1")
# Initialize an empty hashtable to store Workspaces by user
$workspacesByUser = @{}
We define an array of regions to search for Workspaces. In this case, we’re searching for Workspaces in the US East (N. Virginia), Asia Pacific (Mumbai), Europe (London), and South America (Sao Paulo) regions. We then initialize an empty hashtable to store the Workspaces we find, grouped by user.
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
# Loop through each region
foreach ($region in $regions) {
Write-Host "Searching for Workspaces in $region..."
# Use the Get-WKSWorkspace cmdlet to get a list of Workspaces in the region
try {
$workspaces = Get-WKSWorkspace -Region $region -ErrorAction Stop
} catch {
Write-Error "Failed to get Workspaces in $region : $_"
continue
}
# Loop through each Workspace and group them by user
foreach ($workspace in $workspaces) {
$user = $workspace.UserName
if ($workspacesByUser.ContainsKey($user)) {
$workspacesByUser[$user]["$region"] = $workspace
} else {
$workspacesByUser[$user] = @{
"$region" = $workspace
}
}
}
}
In this section, we loop through each region in the $regions
array and use the Get-WKSWorkspace
cmdlet to get a list of Workspaces in that region. If an error occurs, we catch it and write an error message to the console.
We then loop through each Workspace we find and group them by user in the $workspacesByUser
hashtable. If a user already exists in the hashtable, we add the Workspace to their existing entry. If not, we create a new entry for them.
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
# Loop through each user and check if they have Workspaces in all regions
$output = foreach ($user in $workspacesByUser.Keys) {
$workspaces = $workspacesByUser[$user]
$hasWorkspacesInMultipleRegions = $true
$workspaceProperties = @{ Username = $user }
# Loop through each region and check if the user has a Workspace in it
foreach ($region in $regions) {
if ($workspaces.ContainsKey($region)) {
$workspace = $workspaces[$region]
$workspaceProperties["$region" + "_WorkspaceID"] = $workspace.WorkspaceId
$workspaceProperties["$region" + "_ComputerName"] = $workspace.ComputerName
$workspaceProperties["$region" + "_BundleID"] = $workspace.BundleId
$workspaceProperties["$region" + "_RootVolumeSize"] = $workspace.WorkspaceProperties.RootVolumeSizeGib
$workspaceProperties["$region" + "_UserVolumeSize"] = $workspace.WorkspaceProperties.UserVolumeSizeGib
$workspaceProperties["$region" + "_ComputeType"] = $workspace.WorkspaceProperties.ComputeTypeName
} else {
$hasWorkspacesInMultipleRegions = $false
}
}
if ($hasWorkspacesInMultipleRegions) {
Write-Host "User $user - Workspaces found in multiple regions"
[PSCustomObject]$workspaceProperties
}
}
Once we have grouped the Workspaces by user, we will loop through each user and check if they have Workspaces in all regions. We will use the hashtable we created earlier to get the list of Workspaces for that user, and then loop through each region to check if the user has a Workspace in it. If the user has a Workspace in a region, we will retrieve its details and add them to a hashtable. If the user does not have a Workspace in a region, we will set a flag to indicate that the user does not have Workspaces in multiple regions.
1
2
3
4
# Export the output to a CSV file
$output | Export-Csv -Path "c:\UsersWithAwsWorkspacesInMultipleRegions.csv" -NoTypeInformation
The $output variable holds a collection of custom objects with workspace properties of users who have workspaces in multiple regions. The Export-Csv cmdlet is then used to write this data to a CSV file.
Conclusion
By implementing just a few lines of code, we can automate the process of searching for workspaces, categorizing them by user and region, and verifying their presence across all regions. We can then analyze this data to make informed cost analysis decisions regarding whether to maintain resources or reduce costs to save money.