Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <# Workflow
- -- Change Me --
- Author: Chris Marasco
- Date: 12/08/14
- #>
- ##Accept a single paramater for a list
- Param(
- [Parameter(Mandatory=$true,Position=1)]
- [string]$listPath,
- [Parameter(Mandatory=$true,Position=2)]
- [String]$viserver
- )
- ## Enable Install of AD
- Import-Module ServerManager
- ## Install AD module, Import
- try{
- Add-WindowsFeature RSAT-AD-PowerShell
- }catch{
- Write-Host -foregroundcolor "yellow" "RSAT-AD-PowerShell Module already added"
- }
- Import-Module ActiveDirectory
- ##REMOVE ME
- CLS
- ##REMOVE ME
- if( Test-Path($listPath)){
- Import-CSV $listPath | % {
- ## Machine variables
- $strNewVMName = $_.name
- $ip = $_.ip
- $gw = $_.gw
- $vlan = $_.vlan
- $rp = $_.rp
- $cluster = $_.cluster
- $strTemplate = [string]$_.Template
- $dns1 = $_.dns1
- $dns2 = $_.dns2
- $cfg = $_.cfg
- $ds = $_.ds
- $nm = $_.nm
- #Validation Check Var
- $valid = $true
- ##Validation
- Write-Host "`n****************** [STARTED] Checking validity of: $strNewVMName [STARTED] ******************"
- ##Check if IP is in use or not [DNS]
- try{
- ([System.Net.Dns]::GetHostEntry($ip).AddressList[0].IPAddressToString)
- Write-host -foregroundcolor "red" "Address: $ip in use, please fix."
- }catch{
- Write-Host -foregroundcolor "green" "Address: $ip not in use, proceed"
- $valid = $false
- }
- ##Check Gateway
- if(Test-Connection -ComputerName $gw -quiet){
- Write-Host -foregroundcolor "green" "Network connectivity passed for: $gw"
- }else{
- Write-Host -foregroundcolor "red" "Gateway: $gw failed for $strNewVMName"
- $valid = $false
- }
- ##Check Primary DNS
- if(Test-Connection -ComputerName $dns1 -quiet){
- Write-Host -foregroundcolor "green" "Network connectivity passed for: $dns1"
- }else{
- Write-Host -foregroundcolor "red" "Primary DNS: $dns1 failed for $strNewVMName"
- $valid = $false
- }
- ##Check Secondary DNS
- if(Test-Connection -ComputerName $dns2 -quiet){
- Write-Host -foregroundcolor "green" "Network connectivity passed for: $dns2"
- }else{
- Write-Host -foregroundcolor "red" "Secondary DNS: $dns2 failed for $strNewVMName"
- $valid = $false
- }
- ## VMware
- try{
- # Adds the base cmdlets
- try{
- Add-PSSnapin VMware.VimAutomation.Core
- }catch{
- Write-Host -ForegroundColor "yellow" "PS Snapin installed already"
- }
- if(!(Connect-VIServer $viserver)){Connect-VIServer $viserver}
- $hosts = Get-VMHost
- ##Validate VLAN [vSphere Power CLI]
- try{
- $hosts | Get-VirtualPortGroup | ? {$_.Name -eq $vlan}
- Write-Host -foregroundcolor "green" "VLAN: $vlan is valid"
- }catch{
- Write-Host -foregroundcolor "red" "VLAN: $vlan is invalid"
- $valid = $false
- }
- ##Validate Resource Pool [vSphere Power CLI]
- try{
- Get-ResourcePool $rp
- Write-Host -foregroundcolor "green" "Recource pool: $rp is valid"
- }catch{
- Write-Host -foregroundcolor "red" "Resource pool: $rp is invalid"
- $valid = $false
- }
- ##Validate Cluster [vSphere Power CLI]
- try{
- Get-Cluster $cluster
- Write-Host -foregroundcolor "green" "Application Cluster: $cluster is valid"
- }catch{
- Write-Host -foregroundcolor "red" "Application Cluster: $cluster is invalid"
- $valid = $false
- }
- ##Validate Template [vSphere Power CLI]
- try{
- Get-Template $strTemplate
- Write-Host -foregroundcolor "green" "Template: $strTemplate is valid"
- }catch{
- Write-Host -foregroundcolor "red" "Template: $strTemplate is invalid"
- $valid = $false
- }
- ##Validate config [vSphere Power CLI]
- try{
- Get-OSCustomizationSpec $cfg
- Write-Host -foregroundcolor "green" "Guest OS Config File: $cfg is valid"
- }catch{
- Write-Host -foregroundcolor "red" "Guest OS Config File: $cfg is invalid"
- $valid = $false
- }
- ##Validate Datastore
- try{
- Get-DataStoreCluster -Name $ds
- Write-Host -foregroundcolor "green" "Datastore: $ds is valid"
- }catch{
- Write-Host -foregroundcolor "red" "Datastore: $ds is invalid"
- $valid = $false
- }
- ##Create VM
- if($valid){
- Write-Host "****************** [COMPLETE] Checking validity of: $strNewVMName [COMPLETE] ******************`n"
- Write-Host "****************** [STARTING] Creating VM $strNewVMName [STARTING] ******************`n"
- ## Create VM
- try{
- #$spec = Get-OSCustomizationSpec $cfg | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $ip -SubnetMask $nm -DefaultGateway $gw -Dns $dns1,$dns2
- #New-VM -Name $strNewVMName -Template $strTemplate -Datastore $ds -VMHost $vmhost | Set-VM -OSCustomizationSpec $cfg -Confirm:$$false
- #Get-VM $strNewVMName | get-networkadapter | set-networkadapter -NetworkName $vlan -Confirm:$$false
- #Move-VM -vm $strNewVMName -Destination $rp
- }catch{
- Write-Host -foregroundcolor "red" "Error creating VM, please see messages above"
- }
- Write-Host "****************** [FINSISHED] Creating VM $strNewVMName [FINSISHED] ******************`n"
- }else{
- Write-Host "****************** [COMPLETE] Checking validity of: $strNewVMName [COMPLETE] ******************`n"
- Write-Host "Unable to create VM $strNewVMName, please check the error messages above"
- }
- }catch{
- Write-Host -foregroundcolor "red" "Error with vCenter Connection $viserver"
- }
- }
- }else{
- Write-Host "Please enter a valid path"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement