## owner Kope
## Install VM Script on HyperV
## 只支援 2016 以上,powershell版本的需求滿硬的,如果出現一堆error 但hyper-V 端有建出來的話,表示vm 裡面的沒成功
## v1.6 2024/3/29
## 若有跑入domain,local administrator 要在自己去disable
## 111~112 行可切換是否要入domain
## 113 行可切換要disable administrator
## 若是被cs block 請下以下指令
## powershell.exe -ExecutionPolicy Bypass -File "E:\\Install VM in HyperV V1.6.ps1" -MemoryAssigned 1MB

# Variable Setting VM Specification

$Hostname = Read-Host "Hostname "
$IP = Read-Host "IP "
$Gateway = $IP.split(".")[0] + '.' + $IP.split(".")[1] + '.' + $IP.split(".")[2] + '.254'
$VlanId = Read-Host "VlanID "
$CPU = Read-Host "CPU "
[int64]$MEM = 1GB * (Read-host "MEM(GB) ")
$OS = (Read-Host "OS is Windows ") + "_50G_sysprep.vhdx"
[int64]$CDriveSize = 1GB * (Read-host "C Drive Size(GB) ")
$Drive = Read-Host "E/X Drive (Example E:300)"
[int64]$space = 1GB * ($Drive -split ':')[1]
$Drive = ($Drive -split ':')[0]
$DrivePagefile = $Drive + ":\\pagefile.sys"
$PageFileSize = $mem / 1024 / 1024
$DrivePath = "E:\\$Hostname\\" + $Hostname + "_" + $Drive + ".vhdx"
$Cred = Get-Credential -UserName $Hostname'\\administrator' -Message 'Enter Password'

New-VM -Name $Hostname -MemoryStartupBytes $MEM -Path E:\\
$VSwitch = Get-VMSwitch

# Copy VHD From NAS

try {
    Write-host -ForegroundColor Green "Start copy VHD , Please wait"
    Copy-Item E:\\$OS -Destination E:\\$Hostname\\$Hostname"_C.vhdx"
}
catch {
    Write-Host "Error Occured" -BackgroundColor DarkRed
    pause
}

# VM Setting On HyperV

Write-Host -ForegroundColor Green "Start Set Up $drive Drive"
New-VHD -Path $DrivePath -SizeBytes $space -Fix
Resize-VHD -Path E:\\$Hostname\\$Hostname"_C.vhdx" -SizeBytes $CDriveSize
Write-Host -ForegroundColor Green "Start Configure VM-Setting"
Add-VMHardDiskDrive -VMName $Hostname -Path E:\\$Hostname\\$Hostname"_C.vhdx"
Add-VMHardDiskDrive -VMName $Hostname -Path $DrivePath
Set-VM -name $Hostname -ProcessorCount $CPU
Set-VM -name $Hostname -Note $IP
Set-VMProcessor $Hostname -CompatibilityForMigrationEnabled $true
Connect-VMNetworkAdapter -VMName $Hostname -SwitchName $VSwitch.Name
Set-VMNetworkAdapterVlan -VMName $Hostname -Access -VlanId $VlanId
Disable-VMIntegrationService -VMName $Hostname -Name "Time Synchronization"
Set-VM -Name $Hostname -AutomaticStopAction Shutdown
Get-VMDvdDrive -VMName $Hostname -ControllerNumber 1 | Remove-VMDvdDrive

# Start VM

Write-Host -ForegroundColor Green "Starting VM $Hostname"
Start-VM -Name $Hostname
Write-Host -ForegroundColor Green "Start Sleep 3 Min Waitting VM Auto Start"
Start-Sleep 180

# Setting In VM

Write-Host -ForegroundColor Green "Start Set Up Configure In VM"

# Account Setting

Invoke-Command -VMName $Hostname -ScriptBlock { Set-LocalUser -Name "im.infra" -PasswordNeverExpires $true } -Credential ($Cred) 

# C Volume Setting 

Invoke-Command -VMName $Hostname -ScriptBlock { $size = Get-PartitionSupportedSize -DiskNumber 0 -PartitionNumber 2
Resize-Partition -DiskNumber 0 -PartitionNumber 2 -Size $size.SizeMax
} -Credential ($Cred) 

# E or X Volume Setting

Invoke-Command -VMName $Hostname -ScriptBlock { Initialize-Disk 1 –PartitionStyle MBR 
New-Partition –DiskNumber 1 –UseMaximumSize -DriveLetter $using:Drive
Format-Volume -DriveLetter $using:Drive -FileSystem NTFS -Confirm:$false
Set-WmiInstance -Class Win32_PageFileSetting -Arguments @{name = $using:DrivePagefile;
        InitialSize = $using:PageFileSize; MaximumSize = $using:PageFileSize
    } -EnableAllPrivileges | Out-Null
} -Credential ($Cred) 

# Time Zone Setting

Invoke-Command -VMName $Hostname -ScriptBlock { tzutil /s "Atlantic Standard Time_dstoff" } -Credential ($Cred)

# Network Setting

Invoke-Command -VMName $Hostname -ScriptBlock { New-NetIPAddress –InterfaceAlias Ethernet –IPAddress $using:IP –PrefixLength 24 -DefaultGateway $using:Gateway 
Set-DnsClientServerAddress -InterfaceAlias Ethernet -ServerAddresses 10.12.7.233, 10.12.7.211
Disable-NetAdapterBinding -Name "*" -ComponentID ms_tcpip6
} -Credential ($Cred)

# Windows FireWall Setting

Invoke-Command -VMName $Hostname -ScriptBlock { Set-NetFirewallProfile -Profile Domain, Public, Private -Enabled False } -Credential ($Cred)

# HostName Setting

Invoke-Command -VMName $Hostname -ScriptBlock { Rename-Computer -NewName $using:Hostname -Restart } -Credential ($Cred)
Write-Host -ForegroundColor Green "Start Sleep 1 Min Waitting VM Auto Start"
Start-Sleep 60

# Add Domain Naruto

Write-Host -ForegroundColor Green "Start Sleep 1 Min Waitting VM Add Domain"
Invoke-Command -VMName $Hostname -ScriptBlock {Add-Computer -DomainName "naruto.web" -Restart } -Credential ($Cred)

Write-Host -ForegroundColor Green "Start Sleep 1 Min Waitting VM Auto Start"
Start-Sleep 60

# Add Domain UserGroup and disable local admin

Invoke-Command -VMName $Hostname -ScriptBlock {Add-LocalGroupMember -Group 'Administrators' -Member 'InfraTeam'
Add-LocalGroupMember -Group 'Administrators' -Member 'IM-PSteam'    
Add-LocalGroupMember -Group 'Remote Desktop Users' -Member 'InfraSD'
Disable-LocalUser -Name "administrator"
} -Credential ($Cred)

# install zabbix

$domainUser = Read-Host "Your domain user name (Example naruto\\example.user)"
$Cred = Get-Credential -UserName $domainUser -Message 'Enter Password'

Invoke-Command -VMName $Hostname -ScriptBlock {
Copy-Item -Path "\\\\10.12.7.225\\infra\\Yuske\\Zabbix\\OnPrem\\zabbix1\\zabbix-agent" -Destination "C:\\zabbix-agent" -Recurse
C:\\zabbix-agent\\zabbix-install-agent.ps1
} -Credential ($Cred)

Write-Host -ForegroundColor Green "Complete!"
Pause