Advertisement
Guest User

vmAutomate

a guest
Dec 10th, 2014
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <# Workflow
  2. -- Change Me --
  3. Author: Chris Marasco
  4. Date: 12/08/14
  5. #>
  6. ##Accept a single paramater for a list
  7. Param(
  8. [Parameter(Mandatory=$true,Position=1)]
  9. [string]$listPath,
  10. [Parameter(Mandatory=$true,Position=2)]
  11. [String]$viserver
  12. )
  13. ## Enable Install of AD
  14. Import-Module ServerManager
  15.  
  16. ## Install AD module, Import
  17. try{
  18.     Add-WindowsFeature RSAT-AD-PowerShell
  19. }catch{
  20.     Write-Host -foregroundcolor "yellow" "RSAT-AD-PowerShell Module already added"
  21. }
  22. Import-Module ActiveDirectory
  23.  
  24. ##REMOVE ME
  25. CLS
  26. ##REMOVE ME
  27.  
  28. if( Test-Path($listPath)){
  29.     Import-CSV $listPath | % {
  30.  
  31.         ## Machine variables
  32.         $strNewVMName = $_.name
  33.         $ip = $_.ip
  34.         $gw = $_.gw
  35.         $vlan = $_.vlan
  36.         $rp = $_.rp
  37.         $cluster = $_.cluster
  38.         $strTemplate = [string]$_.Template
  39.         $dns1 = $_.dns1
  40.         $dns2 = $_.dns2
  41.         $cfg  = $_.cfg
  42.         $ds = $_.ds
  43.         $nm = $_.nm
  44.  
  45.         #Validation Check Var
  46.         $valid = $true
  47.  
  48.         ##Validation
  49.         Write-Host "`n******************    [STARTED]   Checking validity of:  $strNewVMName    [STARTED]   ******************"
  50.         ##Check if IP is in use or not [DNS]
  51.         try{
  52.             ([System.Net.Dns]::GetHostEntry($ip).AddressList[0].IPAddressToString)
  53.             Write-host -foregroundcolor "red" "Address:  $ip in use, please fix."
  54.         }catch{
  55.             Write-Host -foregroundcolor "green" "Address:  $ip not in use, proceed"
  56.             $valid = $false
  57.         }
  58.  
  59.         ##Check Gateway
  60.         if(Test-Connection -ComputerName $gw -quiet){
  61.             Write-Host -foregroundcolor "green" "Network connectivity passed for: $gw"
  62.         }else{
  63.             Write-Host -foregroundcolor "red" "Gateway:  $gw failed for $strNewVMName"
  64.             $valid = $false
  65.         }
  66.  
  67.         ##Check Primary DNS
  68.         if(Test-Connection -ComputerName $dns1 -quiet){
  69.             Write-Host -foregroundcolor "green" "Network connectivity passed for: $dns1"
  70.         }else{
  71.             Write-Host -foregroundcolor "red" "Primary DNS:  $dns1 failed for $strNewVMName"
  72.             $valid = $false
  73.         }
  74.  
  75.         ##Check Secondary DNS
  76.         if(Test-Connection -ComputerName $dns2 -quiet){
  77.             Write-Host -foregroundcolor "green" "Network connectivity passed for: $dns2"
  78.         }else{
  79.             Write-Host -foregroundcolor "red" "Secondary DNS:  $dns2 failed for $strNewVMName"
  80.             $valid = $false
  81.         }
  82.  
  83.  
  84.         ## VMware
  85.         try{
  86.             # Adds the base cmdlets
  87.             try{
  88.                 Add-PSSnapin VMware.VimAutomation.Core
  89.             }catch{
  90.                 Write-Host -ForegroundColor "yellow" "PS Snapin installed already"
  91.             }
  92.             if(!(Connect-VIServer $viserver)){Connect-VIServer $viserver}
  93.             $hosts = Get-VMHost
  94.  
  95.             ##Validate VLAN [vSphere Power CLI]
  96.             try{
  97.                 $hosts | Get-VirtualPortGroup | ? {$_.Name -eq $vlan}
  98.                 Write-Host -foregroundcolor "green" "VLAN:  $vlan is valid"
  99.             }catch{
  100.                 Write-Host -foregroundcolor "red" "VLAN:  $vlan is invalid"
  101.                 $valid = $false
  102.             }
  103.  
  104.             ##Validate Resource Pool [vSphere Power CLI]
  105.             try{
  106.                 Get-ResourcePool $rp
  107.                 Write-Host -foregroundcolor "green"  "Recource pool:  $rp is valid"
  108.             }catch{
  109.                 Write-Host -foregroundcolor "red"   "Resource pool:  $rp is invalid"
  110.                 $valid = $false
  111.             }
  112.  
  113.             ##Validate Cluster [vSphere Power CLI]
  114.             try{
  115.                 Get-Cluster $cluster
  116.                 Write-Host -foregroundcolor "green"  "Application Cluster:  $cluster is valid"
  117.             }catch{
  118.                 Write-Host -foregroundcolor "red"  "Application Cluster:  $cluster is invalid"
  119.                 $valid = $false
  120.             }
  121.  
  122.             ##Validate Template [vSphere Power CLI]
  123.             try{
  124.                 Get-Template $strTemplate
  125.                 Write-Host -foregroundcolor "green"  "Template:  $strTemplate is valid"
  126.             }catch{
  127.                 Write-Host -foregroundcolor "red"  "Template:  $strTemplate is invalid"
  128.                 $valid = $false
  129.             }
  130.  
  131.             ##Validate config [vSphere Power CLI]
  132.             try{
  133.                 Get-OSCustomizationSpec $cfg
  134.                 Write-Host -foregroundcolor "green"  "Guest OS Config File:  $cfg is valid"
  135.             }catch{
  136.                 Write-Host -foregroundcolor "red"  "Guest OS Config File:  $cfg is invalid"
  137.                 $valid = $false
  138.             }
  139.  
  140.             ##Validate Datastore
  141.             try{
  142.                 Get-DataStoreCluster -Name $ds
  143.                 Write-Host -foregroundcolor "green"  "Datastore:  $ds is valid"
  144.             }catch{
  145.                 Write-Host -foregroundcolor "red"  "Datastore:  $ds is invalid"
  146.                 $valid = $false
  147.             }
  148.  
  149.             ##Create VM
  150.             if($valid){
  151.                 Write-Host "******************  [COMPLETE]  Checking validity of:  $strNewVMName    [COMPLETE]  ******************`n"
  152.                 Write-Host "******************  [STARTING]  Creating VM $strNewVMName   [STARTING]  ******************`n"
  153.                 ## Create VM
  154.                 try{
  155.                     #$spec = Get-OSCustomizationSpec $cfg | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp  -IpAddress $ip -SubnetMask $nm -DefaultGateway $gw -Dns $dns1,$dns2
  156.                     #New-VM -Name $strNewVMName -Template $strTemplate -Datastore $ds -VMHost $vmhost | Set-VM -OSCustomizationSpec $cfg -Confirm:$$false
  157.                     #Get-VM $strNewVMName | get-networkadapter | set-networkadapter -NetworkName $vlan -Confirm:$$false
  158.                     #Move-VM -vm $strNewVMName -Destination $rp
  159.                 }catch{
  160.                     Write-Host -foregroundcolor "red" "Error creating VM, please see messages above"
  161.                 }
  162.  
  163.                 Write-Host "******************  [FINSISHED]  Creating VM $strNewVMName  [FINSISHED] ******************`n"
  164.             }else{
  165.                 Write-Host "******************  [COMPLETE]  Checking validity of:  $strNewVMName    [COMPLETE]  ******************`n"
  166.                 Write-Host "Unable to create VM $strNewVMName, please check the error messages above"
  167.             }
  168.  
  169.         }catch{
  170.             Write-Host -foregroundcolor "red"  "Error with vCenter Connection $viserver"
  171.         }
  172.     }
  173. }else{
  174.     Write-Host "Please enter a valid path"
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement