-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathAdministratorRole.psm1
57 lines (49 loc) · 1.73 KB
/
AdministratorRole.psm1
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
Set-StrictMode -Version Latest
function Test-AdministratorRole {
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
return $isAdmin
<#
.SYNOPSIS
Gets a value indicating whether the current user is an administrator.
.DESCRIPTION
Returns $True or $False, depending on whether the current user is in the built-in Administatrators role
.OUTPUTS
System.Boolean. Returns a value indicating whether the current user is a administrator
#>
}
function Assert-AdministratorRole {
param(
[parameter(position = 0)]
[string]$errorMessage = "You do not have Administrator rights to run this script!`nPlease re-run this script as an Administrator!"
);
$isAdmin = (Test-AdministratorRole)
if (-not $isAdmin) {
throw $errorMessage
}
<#
.SYNOPSIS
Asserts that the current user is an administrator. Throws an error if not.
.DESCRIPTION
Checks whether the current user is in the built-in Administrator role, and throws an error message if not.
.PARAMETER errorMessage
The message to throw when the user is not an administrator
#>
}
function Invoke-Elevated {
param(
[parameter(position = 0, mandatory)]
[ScriptBlock] $ScriptBlock
)
Start-Process Powershell -Verb runAs $ScriptBlock
<#
.SYNOPSIS
Invokes the provided scriptblock as an administrator.
.DESCRIPTION
Opens a new elevated powershell which runs the provided scriptblock.
.PARAMETER ScriptBlock
The ScriptBlock to run in the elevated shell.
#>
}
Export-ModuleMember Test-AdministratorRole
Export-ModuleMember Assert-AdministratorRole
Export-ModuleMember Invoke-Elevated