-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathremoveUnauthorizedEnvironments.ps1
47 lines (42 loc) · 1.49 KB
/
removeUnauthorizedEnvironments.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
[CmdletBinding(DefaultParameterSetName="Report")]
param (
# Will only report on unauthorised environments
[Parameter(ParameterSetName="Report")]
[switch]$ReportOnly,
# Provide the report path, note: report must end with .csv
[Parameter(ParameterSetName="Report")]
[string]$ReportPath,
# List of approved environments
[Parameter(ParameterSetName="Report")]
[array]$ApprovedEnvironments,
# Will run the script in removal mode
[Parameter(ParameterSetName="Remove")]
[switch]$RemoveUnauthorisedEnvironment,
# You can provide an array of environments, note, this must be the environment GUID ('name' field)
[Parameter(ParameterSetName="Remove")]
[array]$EnvironmentGuid
)
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force
if ( $ReportOnly )
{
if ( Test-Path $ReportPath )
{
Remove-Item $ReportPath
}
"DisplayName, EnvironmentName, CreatedTime, CreatedBy" | Out-File $ReportPath -Encoding ascii -Append
$allEnvironments = Get-AdminEnvironment
foreach ( $env in $allEnvironments )
{
if( $ApprovedEnvironments -notcontains $env.DisplayName )
{
$env.DisplayName + "," + $env.EnvironmentName + "," + $env.CreatedTime + "," + $env.CreatedBy.email | Out-File $ReportPath -Encoding ascii -Append
}
}
}
if ( $RemoveUnauthorisedEnvironment )
{
foreach( $unauthorisedEnvironment in $EnvironmentGuid )
{
Remove-AdminEnvironment -EnvironmentName $unauthorisedEnvironment
}
}