Create project environment for class 20-410

This script is used in class Microsoft(20-410) for setting up project environment.


##################################################################
#   * Please create hyper-V network switch manually
#   * Set switch to be private. Name "Private Network"
#   * Please run this script as Administrator privilege
#   
#   Raymond Liu 
#   Team: Round 3
#
#   2016 / 10 / 26 v1
#   2016 / 11 / 9  v1.3
################# Settings ################################

$MECO_DC1      = "MECO-DC1"                                                                 # Implement server with: AD DS, DNS, DHCP
$MECO_DC2      = "MECO-DC2"                                                                 # Implement Secondary AD DS role only
$MECO_FS1      = "MECO-FS1"                                                                 # Implement domain joined File Server
$MECO_WS1      = "MECO-WS1"                                                                 # Implement domain joined Intranet Web Server
$MECO_CL1      = "MECO-CL1"                                                                 # Implement Domain joined client workstation
$MECO_CL2      = "MECO-CL2"                                                                 # Implement Domain joined client workstation
$MECO_RTR1     = "MECO-RTR1"                                                                # Implement Domain joined RRAS Routing Server and VPN
$MECO_NAT      = "MECO-NAT"                                                                 # Implement NAT Serevr
$RAM           = 2GB  				                                                        # RAM assigned to VM
$VHD           = 127GB  			                                                        # Size of Hard-Drive for VM
$GENERATION    = 1                                                                          # VM Generation 
$VM_Folder     = "C:\Project"                                                               # Location of the VM and VHDX files
$VMSwitch      = "Private Network"                                                          # Name of the Hyper-v Network Switch
$VMSwitch2     = "External"                                                                 # Name of the Hyper-v Network Switch
$W8ISO         = "..\OS\Windows 8.1 Enterprise 64BIT_English_MLF_X18-96759.iso"	            # Windows 8.1 ISO
$W12ISO        = "..\OS\en_windows_server_2012_r2_with_update_x64_dvd_6052708.iso"	        # Windows Server 2012 R2 ISO

################# Settings ################################

################# Create Hyper-V VM folder ################
# Create Hyper-V VM folder
try {

    MD $VM_Folder -ErrorAction SilentlyContinue

} catch {

    Write-Host("`n[ERROR] Can't Create VM Folder. `n") -ForegroundColor Red
    Exit 1 

}

################# Create Hyper-V VM folder ################

################# Create VM ###############################
# Create VM
try {

    New-VM -Name $MECO_DC1 `
           -Path $VM_Folder `
           -MemoryStartupBytes $RAM `
           -NewVHDPath $VM_Folder\$MECO_DC1"\Virtual Hard Disks\"$MECO_DC1.vhdx `
           -NewVHDSizeBytes $VHD `
           -SwitchName $VMSwitch `
           -Generation $GENERATION

} catch {

    Write-Host("`n[ERROR] Can't Create VM MECO_DC1. `n") -ForegroundColor Red
    Exit 1 

} # MECO_DC1

try {

    New-VM -Name $MECO_DC2 `
           -Path $VM_Folder `
           -MemoryStartupBytes $RAM `
           -NewVHDPath $VM_Folder\$MECO_DC2"\Virtual Hard Disks\"$MECO_DC2.vhdx `
           -NewVHDSizeBytes $VHD `
           -SwitchName $VMSwitch `
           -Generation $GENERATION

} catch {

    Write-Host("`n[ERROR] Can't Create VM MECO_DC2. `n") -ForegroundColor Red
    Exit 1 

} # MECO_DC2

try {

    New-VM -Name $MECO_FS1 `
           -Path $VM_Folder `
           -MemoryStartupBytes $RAM `
           -NewVHDPath $VM_Folder\$MECO_FS1"\Virtual Hard Disks\"$MECO_FS1.vhdx `
           -NewVHDSizeBytes $VHD `
           -SwitchName $VMSwitch `
           -Generation $GENERATION

} catch {

    Write-Host("`n[ERROR] Can't Create VM MECO_FS1. `n") -ForegroundColor Red
    Exit 1 

} # MECO_FS1


try {

    New-VM -Name $MECO_WS1 `
           -Path $VM_Folder `
           -MemoryStartupBytes $RAM `
           -NewVHDPath $VM_Folder\$MECO_WS1"\Virtual Hard Disks\"$MECO_WS1.vhdx `
           -NewVHDSizeBytes $VHD `
           -SwitchName $VMSwitch `
           -Generation $GENERATION

} catch {

    Write-Host("`n[ERROR] Can't Create VM MECO_WS1. `n") -ForegroundColor Red
    Exit 1 

} # MECO_WS1


try {

    New-VM -Name $MECO_CL1 `
           -Path $VM_Folder `
           -MemoryStartupBytes $RAM `
           -NewVHDPath $VM_Folder\$MECO_CL1"\Virtual Hard Disks\"$MECO_CL1.vhdx `
           -NewVHDSizeBytes $VHD `
           -SwitchName $VMSwitch `
           -Generation $GENERATION

} catch {

    Write-Host("`n[ERROR] Can't Create VM MECO_CL1. `n") -ForegroundColor Red
    Exit 1 

} # MECO_CL1


try {

    New-VM -Name $MECO_CL2 `
           -Path $VM_Folder `
           -MemoryStartupBytes $RAM `
           -NewVHDPath $VM_Folder\$MECO_CL2"\Virtual Hard Disks\"$MECO_CL2.vhdx `
           -NewVHDSizeBytes $VHD `
           -SwitchName $VMSwitch `
           -Generation $GENERATION

} catch {

    Write-Host("`n[ERROR] Can't Create VM MECO_CL2. `n") -ForegroundColor Red
    Exit 1 

} # MECO_CL2


try {

    New-VM -Name $MECO_RTR1 `
           -Path $VM_Folder `
           -MemoryStartupBytes $RAM `
           -NewVHDPath $VM_Folder\$MECO_RTR1"\Virtual Hard Disks\"$MECO_RTR1.vhdx `
           -NewVHDSizeBytes $VHD `
           -SwitchName $VMSwitch `
           -Generation $GENERATION

} catch {

    Write-Host("`n[ERROR] Can't Create VM MECO_RTR1. `n") -ForegroundColor Red
    Exit 1 

} # MECO_RTR1


try {

    New-VM -Name $MECO_NAT `
           -Path $VM_Folder `
           -MemoryStartupBytes $RAM `
           -NewVHDPath $VM_Folder\$MECO_NAT"\Virtual Hard Disks\"$MECO_NAT.vhdx `
           -NewVHDSizeBytes $VHD `
		   -SwitchName $VMSwitch2 `
           -Generation $GENERATION
		   
    Add-VMNetworkAdapter -VMName $MECO_NAT -SwitchName $VMSwitch

} catch {

    Write-Host("`n[ERROR] Can't Create VM MECO_NAT. `n") -ForegroundColor Red
    Exit 1 

} # MECO_NAT

################# Create VM ###############################


################# Set VM ISO ##############################
# Set VM ISO
try {

    Set-VMDvdDrive -VMName $MECO_DC1 `
                   -Path $W12ISO

    Write-Host("Done Create VM: --> " + $MECO_DC1) -ForegroundColor Green

} catch {

    Write-Host("`n[ERROR] Can't Set VM MECO_DC1. `n") -ForegroundColor Red
    Exit 1 
} # MECO_DC1

try {

    Set-VMDvdDrive -VMName $MECO_DC2 `
                   -Path $W12ISO

    Write-Host("Done Create VM: --> " + $MECO_DC2) -ForegroundColor Green

} catch {

    Write-Host("`n[ERROR] Can't Set VM MECO_DC2. `n") -ForegroundColor Red
    Exit 1 
} # MECO_DC2

try {

    Set-VMDvdDrive -VMName $MECO_FS1 `
                   -Path $W12ISO

    Write-Host("Done Create VM: --> " + $MECO_FS1) -ForegroundColor Green

} catch {

    Write-Host("`n[ERROR] Can't Set VM MECO_FS1. `n") -ForegroundColor Red
    Exit 1 
} # MECO_FS1

try {

    Set-VMDvdDrive -VMName $MECO_WS1 `
                   -Path $W12ISO

    Write-Host("Done Create VM: --> " + $MECO_WS1) -ForegroundColor Green

} catch {

    Write-Host("`n[ERROR] Can't Set VM MECO_WS1. `n") -ForegroundColor Red
    Exit 1 
} # MECO_WS1

try {

    Set-VMDvdDrive -VMName $MECO_CL1 `
                   -Path $W8ISO


    Write-Host("Done Create VM: --> " + $MECO_CL1) -ForegroundColor Green

} catch {

    Write-Host("`n[ERROR] Can't Set VM MECO_CL1. `n") -ForegroundColor Red
    Exit 1 
} # MECO_CL1


try {

    Set-VMDvdDrive -VMName $MECO_CL2 `
                   -Path $W8ISO


    Write-Host("Done Create VM: --> " + $MECO_CL2) -ForegroundColor Green

} catch {

    Write-Host("`n[ERROR] Can't Set VM MECO_CL2. `n") -ForegroundColor Red
    Exit 1 
} # MECO_CL2


try {

    Set-VMDvdDrive -VMName $MECO_RTR1 `
                   -Path $W12ISO

    Write-Host("Done Create VM: --> " + $MECO_RTR1) -ForegroundColor Green

} catch {

    Write-Host("`n[ERROR] Can't Set VM MECO_WS1. `n") -ForegroundColor Red
    Exit 1 
} # MECO_RTR1


try {

    Set-VMDvdDrive -VMName $MECO_NAT `
                   -Path $W12ISO

    Write-Host("Done Create VM: --> " + $MECO_NAT + "`n") -ForegroundColor Green

} catch {

    Write-Host("`n[ERROR] Can't Set VM MECO_NAT. `n") -ForegroundColor Red
    Exit 1 
} # MECO_NAT



################# Set VM ISO ##############################


# Star VM
try {

    #Start-VM $MECO_DC1

} catch {

    #Write-Host("`n[ERROR] Can't Start VM MECO_DC1. `n") -ForegroundColor Red
    #Exit 1 

}



Exit 1

[Scripts] Send-MailMessage with For loop

課堂上想起來老師曾經提到的Send-MailMessage,就自己用windows 2012 R2 架了一個SMTP Server,然後寫了這一個小script。

$bomb = 2

$From = 'demo@demogmail.com'
$To = 'demo@demogmail.com'
$Subject = 'Hello World!'
$Body = 'I <3<3<3<3<3 Powershell'
$SMTPServer = "localhost"
$SMTPPort = "25"




for($i=1; $i -le $bomb; $i++) {

Send-MailMessage -From $From `
                 -to $To `
                 -Subject $Subject `
                 -Body $Body `
                 -SmtpServer $SMTPServer `
                 -port $SMTPPort
}