Created
August 2, 2016 14:38
-
-
Save MSAdministrator/369ed2620a58c715ed9226e8db026fb1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.Synopsis | |
Short description | |
.DESCRIPTION | |
Long description | |
.EXAMPLE | |
Example of how to use this cmdlet | |
.EXAMPLE | |
Another example of how to use this cmdlet | |
#> | |
function New-TestVM | |
{ | |
[CmdletBinding()] | |
[OutputType([int])] | |
Param | |
( | |
[Parameter(Mandatory=$true, | |
ValueFromPipelineByPropertyName=$true)] | |
$NewVMName, | |
[Parameter(Mandatory=$true, | |
ValueFromPipelineByPropertyName=$true)] | |
$Log, | |
# Param2 help description | |
[Parameter(Mandatory = $false)] | |
[switch]$Office2007x86, | |
[Parameter(Mandatory = $false)] | |
[switch]$Office2010x86, | |
[Parameter(Mandatory = $false)] | |
[switch]$Office2010x64, | |
[Parameter(Mandatory = $false)] | |
[switch]$Office2013x86, | |
[Parameter(Mandatory = $false)] | |
[switch]$Office2013x64, | |
[Parameter(Mandatory = $false)] | |
[switch]$Office2016x86, | |
[Parameter(Mandatory = $false)] | |
[switch]$Office2016x64, | |
[Parameter(Mandatory = $false)] | |
[switch]$Windows7x86, | |
[Parameter(Mandatory = $false)] | |
[switch]$Windows7x64, | |
[Parameter(Mandatory = $false)] | |
[switch]$Windows10x86, | |
[Parameter(Mandatory = $false)] | |
[switch]$Windows10x64 | |
) | |
Begin | |
{ | |
try | |
{ | |
Add-PSSnapin vmware* | |
if (Get-Item 'C:\Program Files (x86)' -ErrorAction SilentlyContinue) | |
{ | |
. "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1" | |
} | |
else | |
{ | |
. "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1" | |
} | |
} | |
catch | |
{ | |
} | |
$NewVMObject = @{} | |
switch ($PSBoundParameters.Keys) | |
{ | |
'Office2007x86' { $NewVMObject.OfficePath = 'C:\Testing_Share\Office_2007_x86\' } | |
'Office2010x86' { $NewVMObject.OfficePath = 'C:\Testing_Share\Office_2010_x86\' | |
$NewVMObject.OfficeCustomization = 'Office_2010_x86_Email_Profile.msp' } | |
'Office2010x64' { $NewVMObject.OfficePath = 'C:\Testing_Share\Office_2010_x64\' | |
$NewVMObject.OfficeCustomization = 'Office_2010_x64_Email_Profile.msp' } | |
'Office2013x86' { $NewVMObject.OfficePath = 'C:\Testing_Share\Office_2013_x86\' | |
$NewVMObject.OfficeCustomization = 'Office_2013_x86_Email_Profile.msp' } | |
'Office2013x64' { $NewVMObject.OfficePath = 'C:\Testing_Share\Office_2013_x64\' } | |
#'Office2016x86' { $NewVMObject.OfficePath = 'C:\Testing_Share\Office_2016_x86\' } | |
#'Office2016x64' { $NewVMObject.OfficePath = 'C:\Testing_Share\Office_2016_x64\' } | |
'Windows7x86' { $NewVMObject.OSTemplate = 'Windows 7 x86' } | |
'Windows7x64' { $NewVMObject.OSTemplate = 'Win7x64-Master-Template' } | |
'Windows10x86' { $NewVMObject.OSTemplate = 'Windows 10 x86' } | |
'Windows10x64' { $NewVMObject.OSTemplate = 'Win10x64-Master-Template' } | |
} | |
$NewVMObject.NewVMName = $NewVMName | |
$VMTemplates = @() | |
#Replace Cluster Name with a known cluster name in your environment | |
foreach($vmhost in get-cluster '{CLUSTER_NAME}' | get-vmhost) | |
{ | |
$VMTemplates += get-template -Location $vmhost | select name,@{n='VMHOST';e={$vmhost.name}} | |
} | |
foreach ($item in $VMTemplates) | |
{ | |
if (($item).Name -eq $($NewVMObject.OSTemplate)) | |
{ | |
$NewVMObject.VMHost = $Item.VMHOST | |
} | |
} | |
if ($null -eq $NewVMObject.VMHost) | |
{ | |
#Break | |
} | |
} | |
Process | |
{ | |
try | |
{ #replace Folder Location if needed | |
$NewVM = New-VM -Name $($NewVMObject.NewVMName) -Template $($NewVMObject.OSTemplate) -VMHost $($NewVMObject.VMHost) -Location 'FOLDER_LOCATION' | |
$VM = Get-VM $NewVM | |
} | |
catch | |
{ | |
Write-LogEntry -type Error -message $_ -Folder $Log -CustomMessage 'Creating new VM from Template' | |
Write-LogEntry -type Error -message $_.Exception.Message -Folder $Log | |
Write-LogEntry -type Error -message $_.Exception.ItemName -Folder $Log | |
} | |
<# #Removing Mounted ISO Drive | |
try | |
{ | |
$CDDrive = Get-CDDrive -VM $VM | |
Remove-CDDrive -CD $CDDrive -Confirm | |
} | |
catch | |
{ | |
} | |
#Create new CD Drive | |
try | |
{ | |
$CD = New-CDDrive -VM $VM | |
} | |
catch | |
{ | |
echo $_.Exception.GetType().FullName, $_.Exception.Message | |
} | |
#> | |
#Remove Network Adapter | |
try | |
{ | |
#Remove Network Adapter | |
$RemoveNetworkAdapter = Get-VM $VM | Get-NetworkAdapter | Remove-NetworkAdapter -Confirm:$false | |
} | |
catch | |
{ | |
Write-LogEntry -type Error -message $_ -Folder $Log -CustomMessage 'Removing VMs Network Adapter' | |
Write-LogEntry -type Error -message $_.Exception.Message -Folder $Log | |
Write-LogEntry -type Error -message $_.Exception.ItemName -Folder $Log | |
} | |
#Adding New Network Adapter | |
try | |
{ | |
$NewNetworkAdapter = New-NetworkAdapter -VM $VM -NetworkName 'VMNetwork' -StartConnected:$true -Confirm:$false | |
} | |
catch | |
{ | |
Write-LogEntry -type Error -message $_ -Folder $Log -CustomMessage 'Adding a Network Adapter to VM' | |
Write-LogEntry -type Error -message $_.Exception.Message -Folder $Log | |
Write-LogEntry -type Error -message $_.Exception.ItemName -Folder $Log | |
} | |
#move VM To our Folder | |
#Power On the VM | |
try | |
{ | |
$StartVM = Start-VM -VM $VM | |
} | |
catch | |
{ | |
Write-LogEntry -type Error -message $_ -Folder $Log -CustomMessage 'Powering on VM' | |
Write-LogEntry -type Error -message $_.Exception.Message -Folder $Log | |
Write-LogEntry -type Error -message $_.Exception.ItemName -Folder $Log | |
} | |
<# #Set the CD Drive as Active and Connected to VM | |
try | |
{ | |
Set-CDDrive -CD $CD -StartConnected $true -Connected $true | |
} | |
catch | |
{ | |
echo $_.Exception.GetType().FullName, $_.Exception.Message | |
} | |
#> | |
#Set the Network Adapter. | |
try | |
{ | |
$SetNetworkAdapter = Get-VM $VM | Get-NetworkAdapter | Set-NetworkAdapter -StartConnected:$true -Connected:$true -Confirm:$false | |
} | |
catch | |
{ | |
Write-LogEntry -type Error -message $_ -Folder $Log -CustomMessage 'Setting Network Adapter status to Connected' | |
Write-LogEntry -type Error -message $_.Exception.Message -Folder $Log | |
Write-LogEntry -type Error -message $_.Exception.ItemName -Folder $Log | |
} | |
#Sleep for 5 minutes to let VM boot. | |
Start-Sleep -Seconds 300 | |
$VMWareTools = Update-Tools -VM $VM -NoReboot | |
#Define file information. Include File Name, Parameters, Source and Destination | |
$OfficeSetupFile = "Setup.exe" | |
$DstPath = "c:\temp\" | |
$SrcPath = $NewVMObject.OfficePath | |
$Fullpath = $SrcPath + $File | |
$Command = $DstPath + $OfficeSetupFile + ' /adminfile C:\temp\Updates\Office_2010_x86_Email_Profile.msp' | |
#Copying Office Setup Files to VM | |
try | |
{ | |
$CopyOffice = Copy-VMGuestFile -VM $VM -Source $SrcPath -Destination $DstPath -LocalToGuest -GuestUser admin -GuestPassword 'password' -Force | |
} | |
catch | |
{ | |
Write-LogEntry -type Error -message $_ -Folder $Log -CustomMessage 'Copying Office version to VM' | |
Write-LogEntry -type Error -message $_.Exception.Message -Folder $Log | |
Write-LogEntry -type Error -message $_.Exception.ItemName -Folder $Log | |
} | |
#Invoking Office Setup configuration | |
try | |
{ | |
$Result = Invoke-VMScript -VM $VM -ScriptText $Command -GuestUser admin -GuestPassword 'password' | |
} | |
catch | |
{ | |
Write-LogEntry -type Error -message $_ -Folder $Log -CustomMessage 'Attempting to install Office silently' | |
Write-LogEntry -type Error -message $_.Exception.Message -Folder $Log | |
Write-LogEntry -type Error -message $_.Exception.ItemName -Folder $Log | |
} | |
} | |
End | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment