-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathNew-WKSWorkSpacesReport.ps1
268 lines (238 loc) · 10.5 KB
/
New-WKSWorkSpacesReport.ps1
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<#
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: MIT-0
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#>
# Prompting for a path to store logs with a default path provided and normalizes path with trailing \ if not present
$defaultreportpath = "C:\temp\"
if (!($reportpath = Read-Host "Path to store WorkSpaces creation log output: [$defaultreportpath]")) { $reportpath = $defaultreportpath }
if($reportpath -notmatch '\\$') {$reportpath = $reportpath + "\"}
If(!(test-path $reportpath)){$createfolder = New-Item -ItemType Directory -Path $reportpath}
# Define / list the regions that WorkSpaces are supported in
$WorkSpacesRegions = "us-east-1", `
"us-west-2",`
"ap-northeast-2",`
"ap-southeast-1",`
"ap-southeast-2",`
"ap-northeast-1",`
"ca-central-1",`
"eu-central-1",`
"eu-west-1",`
"eu-west-2",`
"sa-east-1"
Do {
Write-Host "`n`n`nHere are the WorkSpaces supported regions:"
$WorkSpacesRegions | sort
$Region = Read-Host -Prompt 'Type the region identifier to use for provisioning'
}
While (-not ($WorkSpacesRegions | Where-Object {$_ -eq $Region}))
Write-host "Region" $Region "selected."
#---------------------------------------------------------------------------------------------
# Prompting for number of days to set as a bar to determine inactivity
$defaultinactivedays = 60
Do {
[regex] $daysregex = "^\d{1,3}$"
if (!($inactivedays = Read-Host "Number of days used to determine inactive WorkSpaces (maximum 999): [$defaultinactivedays]")) { $inactivedays = $defaultinactivedays }
} while ($inactivedays -inotmatch $daysregex)
# Convert captured data to integer
$inactivedays = [int]$inactivedays
if($inactivedays -ge 455) {"CloudWatch aggregates log data so you may need to modify the period queried per this link`
https://aws.amazon.com/about-aws/whats-new/2016/11/cloudwatch-extends-metrics-retention-and-new-user-interface/"}
$StartDate = (Get-Date).AddDays(-$inactivedays)
$EndDate = Get-Date
#---------------------------------------------------------------------------------------------
# Define CloudWatch dataset
$dimension1 = New-Object Amazon.CloudWatch.Model.Dimension
$dimension1.set_Name("WorkspaceId")
# Setting this to 1 day (60 seconds * 60 minutes * 24 hours in a day) to allow querying larger data points - see above link relating to CloudWatch metrics aggregation
$period = 86400
#---------------------------------------------------------------------------------------------
$report=@()
# Get all WorkSpaces in the specified region with the appropriate fields for the report
Try
{
$WorkSpaces = Get-WKSWorkspace -Region $Region | Select-Object username, computername, workspaceid, ipaddress, directoryid, bundleid, subnetid, state, RootVolumeEncryptionEnabled, UserVolumeEncryptionEnabled, WorkspaceProperties
}
Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-host $ErrorMessage
Write-host $FailedItem
Break
}
$WorkSpacesCount = $WorkSpaces.count
Write-Host "Capturing info for $($WorkSpaces.count) WorkSpaces"
foreach ($WorkSpace in $WorkSpaces){
# Loop through each WorkSpace capturing custom info from Active Directory and appending it to the variable that will be export to the specified log file
Write-Host "Enumerating workspace $($WorkSpace.WorkspaceId) assigned to user $($WorkSpace.UserName)"
$samlookup = $WorkSpace.username
$computername = $WorkSpace.computername
# An adhoc or inline query can be used for each of the AD User and Computer attributes but doing so up front for all attributes decreases report run time by nearly 50%
# Query AD for all user attributes needed below
Try
{
$ADUserInfo = get-aduser -filter {samaccountname -eq $samlookup} -properties Name, Enabled, Department, EmailAddress, Manager, MobilePhone | Select-Object *, @{label="ADUserManager";expression={(Get-ADUser $_.Manager -Properties DisplayName).DisplayName}}
}
Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-host $ErrorMessage
Write-host $FailedItem
Break
}
# Query AD for all computer attributes needed below
Try
{
$ADComputerInfo = get-adcomputer -filter {name -eq $computername} -properties Created, OperatingSystem
}
Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-host $ErrorMessage
Write-host $FailedItem
Break
}
# Query AWS for the connected state of the WorkSpace
Try
{
$WorkSpaceConnectionInfo = Get-WKSWorkspacesConnectionStatus -Region $Region -WorkspaceId $WorkSpace.workspaceid
}
Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-host $ErrorMessage
Write-host $FailedItem
Break
}
# Query AWS for the subnet info for the WorkSpace
Try
{
$WorkSpaceSubnetInfo = Get-EC2Subnet -Region $Region -SubnetId $WorkSpace.SubnetId
}
Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-host $ErrorMessage
Write-host $FailedItem
Break
}
# Query CloudWatch to determine whether WorkSpace is inactive
Try
{
$dimension1.set_Value($WorkSpace.WorkspaceId)
$data = Get-CWMetricStatistics -Region $Region -Namespace "AWS/WorkSpaces" -MetricName "ConnectionSuccess" -UtcStartTime $StartDate -UtcEndTime $EndDate -Period $period -Statistics @("Maximum") -Dimensions @($dimension1)
if(($data.datapoints.Maximum | sort -Unique | select -Last 1) -ge 1){
# logins found
$WorkSpaceUnused = $false
}Else{
# no logins found
$WorkSpaceUnused = $true
}
}
Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-host $ErrorMessage
Write-host $FailedItem
Break
}
# Build the report object with all required properties
$obj = New-Object -TypeName PSObject -Property @{
"UserName" = $WorkSpace.UserName
"ADUserFullName" = $ADUserInfo.Name
"ADUserDepartment" = $ADUserInfo.Department
"ADUserEnabled" = $ADUserInfo.Enabled
"ADUserEmailAddress" = $ADUserInfo.EmailAddress
"ADUserManager" = $ADUserInfo.ADUserManager
"ADUserMobilePhone" = $ADUserInfo.MobilePhone
"ComputerName" = $WorkSpace.ComputerName
"ADComputerCreated" = $ADComputerInfo.Created
"ADComputerOperatingSystem" = $ADComputerInfo.OperatingSystem
"WorkSpaceId" = $WorkSpace.WorkspaceId
"ConnectionState" = $WorkSpaceConnectionInfo.ConnectionState
"ConnectionStateCheckTimestamp" = $WorkSpaceConnectionInfo.ConnectionStateCheckTimestamp
"LastKnownUserConnectionTimestamp" = $WorkSpaceConnectionInfo.LastKnownUserConnectionTimestamp
"WorkSpaceUnusedForDefinedPeriod" = $WorkSpaceUnused
"TagsJoin" = (Get-WKSTag -Region $Region -WorkspaceId $WorkSpace.workspaceid | Select-Object @{N='Tags';E={$_.Key,$_.Value -join ':'}}).tags -join ";"
"WorkSpaceState" = $WorkSpace.State
"ComputeType" = $WorkSpace.WorkspaceProperties.ComputeTypeName
"IpAddress" = $WorkSpace.IpAddress
"Directory" = (get-dsdirectory -Region $Region -DirectoryID $WorkSpace.DirectoryId).alias
"DirectoryId" = $WorkSpace.DirectoryId
"Bundle" = (Get-WKSWorkspaceBundle -Region $Region -BundleId $WorkSpace.BundleId).Name
"BundleId" = $WorkSpace.BundleId
"SubnetLabel" = $WorkSpaceSubnetInfo.Tag.Where({$_.Key -eq "Name"}).value
"SubnetId" = $WorkSpace.SubnetId
"SubnetAZ" = $WorkSpaceSubnetInfo.AvailabilityZone
"SubnetAZId" = $WorkSpaceSubnetInfo.AvailabilityZoneId
"SubnetAvailableIpAddressCount" = $WorkSpaceSubnetInfo.AvailableIpAddressCount
"RootEncryption" = $WorkSpace.RootVolumeEncryptionEnabled
"RootVolumeSizeGib" = $WorkSpace.WorkspaceProperties.RootVolumeSizeGib
"UserEncryption" = $WorkSpace.UserVolumeEncryptionEnabled
"UserVolumeSizeGib" = $WorkSpace.WorkspaceProperties.UserVolumeSizeGib
"RunningMode" = $WorkSpace.WorkspaceProperties.RunningMode
"TimeoutMinutes" = $WorkSpace.WorkspaceProperties.RunningModeAutoStopTimeoutInMinutes
"Region" = $Region
}
# Append each WorkSpace to the report object so all objects can be written to disk at the same time
$report += $obj | Select-Object username, `
ADUserFullName, `
ComputerName, `
ADComputerCreated, `
ADComputerOperatingSystem, `
WorkSpaceId, `
ConnectionState, `
ConnectionStateCheckTimestamp, `
LastKnownUserConnectionTimestamp, `
WorkSpaceUnusedForDefinedPeriod, `
WorkSpaceState, `
ComputeType, `
ipaddress, `
Directory, `
directoryid, `
Bundle, `
bundleid, `
SubnetLabel, `
SubnetId, `
SubnetAZ, `
SubnetAZId, `
SubnetAvailableIpAddressCount, `
RootEncryption, `
RootVolumeSizeGib, `
UserEncryption, `
UserVolumeSizeGib, `
RunningMode, `
TimeoutMinutes, `
ADUserDepartment, `
ADUserEnabled, `
ADUserEmailAddress, `
ADUserManager, `
ADUserMobilePhone, `
Region, `
TagsJoin
# Decrement the count of WorkSpaces so the user sees a progress indicator
$WorkSpacesCount--
Write-Host "$($WorkSpacesCount) WorkSpaces remain"
# Delay to prevent AWS API Throttling
Start-Sleep -Milliseconds 750
}
# Write the report to disk
$report | Sort-Object UserName, Directory | Export-Csv ($reportpath + "workspacesreport.csv") -notypeinformation -Append