-
Notifications
You must be signed in to change notification settings - Fork 1
/
Create_Azure_Migrate_Local_Admin_on_Hyper-V.ps1
93 lines (60 loc) · 4.34 KB
/
Create_Azure_Migrate_Local_Admin_on_Hyper-V.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
<#
.SYNOPSIS
A script used to create an Azure Migrate Local Admin account on a standalone non-domain joined Hyper-V host.
.DESCRIPTION
A script used to create an Azure Migrate Local Admin account on a standalone non-domain joined Hyper-V host.
This Local Admin account will also added to the following groups: Hyper-V Administrators, Remote Management Users, and Performance Monitor Users.
.NOTES
Filename: Create_Azure_Migrate_Local_Admin_on_Hyper-V.ps1
Created: 03/12/2020
Last modified: 04/12/2020
Author: Wim Matthyssen
PowerShell: 5.1
Action: Change variables were needed to fit your needs.
Disclaimer: This script is provided "As IS" with no warranties.
.EXAMPLE
.\Create_Azure_Migrate_Local_Admin_on_Hyper-V.ps1
.LINK
https://wmatthyssen.com/2020/12/06/azure-migrate-source-validation-of-a-standalone-non-domain-joined-hyper-v-host-fails-with-error-access-is-denied/
#>
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Variables
$global:currenttime= Set-PSBreakpoint -Variable currenttime -Mode Read -Action {$global:currenttime= Get-Date -UFormat "%A %m/%d/%Y %R"}
$foregroundColor1 = "Red"
$writeEmptyLine = "`n"
$writeSeperatorSpaces = " - "
$localUserName = "aa_azmig"
$localUserFullName = $localUserName
$localUserDescription = "Azure Migrate Adminstrator account"
$administratorsGroup = "Administrators"
$hvAdministratorsGroup = "Hyper-V Administrators"
$remoteManagementUsersGroup = "Remote Management Users"
$performanceMonitorUsersGroup = "Performance Monitor Users"
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Prerequisites
## Check if running as Administrator, otherwise close the PowerShell window
$CurrentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$IsAdministrator = $CurrentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if ($IsAdministrator -eq $false) {
Write-Host ($writeEmptyLine + "# Please run PowerShell as Administrator" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine
Start-Sleep -s 5
exit
}
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Create a secure password for the Local Admin account
$secPassword = Read-Host " # Type in a password for the Local Admin account" $localUserName "which meets the complexity requirements" –AsSecureString
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Create Local Admin account
New-LocalUser -Name $localUserName -Password $secPassword -FullName $localUserFullName -Description $localUserDescription -UserMayNotChangePassword -PasswordNeverExpires –Verbose
Write-Host ($writeEmptyLine + " # Local Admin Account " + $localUserName + " created" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Add Local Admin account to the required groups
Add-LocalGroupMember -Group $administratorsGroup -Member $localUserName –Verbose
Add-LocalGroupMember -Group $hvAdministratorsGroup -Member $localUserName –Verbose
Add-LocalGroupMember -Group $remoteManagementUsersGroup -Member $localUserName –Verbose
Add-LocalGroupMember -Group $performanceMonitorUsersGroup -Member $localUserName –Verbose
Write-Host ($writeEmptyLine + " # Local Admin Account " + $localUserName + " added to the required groups" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------