Advertisement
Workspace-Guru

New-VMDevice

Feb 18th, 2018
4,931
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#  
  2.     .NOTES
  3.     ===========================================================================
  4.      Created on:    18-2-2018
  5.      Created by:    Chris Twiest
  6.      Organization:  Workspace-Guru.com      
  7.     ===========================================================================
  8.     .DESCRIPTION
  9.     This script will read in a CSV file with Devicenames, PVS MAC, PROD MAC, PROD IP and PVS IP.
  10.     It will create these machines in VMWare with PowerCLI from a template and set the Mac Address for PVS and PROD network.
  11. #>
  12.  
  13.  
  14. #### Parameters
  15. $CSVPath = "C:\Temp\PVS.csv"
  16. $vCenterserver = "vcenter01.domain.com"
  17. $vCenterUser = "administrator@domain.com"
  18. $vCenterPassword = "P@ssw0rd!"
  19. $VMHost = "vmhost01.domain.com"
  20. $VMTemplate = 'CXA-Template-2016'
  21. $Datastore = 'VMWare01'
  22. $PRODLan = "VM Network"
  23. $PVSLan = "PVS Network"
  24.  
  25. #### Load in PowerCLI
  26. # Returns the path (with trailing backslash) to the directory where PowerCLI is installed.
  27. if ( !(Get-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) ) {
  28.     if (Test-Path -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\VMware, Inc.\VMware vSphere PowerCLI' ) {
  29.         $Regkey = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\VMware, Inc.\VMware vSphere PowerCLI'
  30.        
  31.     } else {
  32.         $Regkey = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\VMware, Inc.\VMware vSphere PowerCLI'
  33.     }
  34.     . (join-path -path (Get-ItemProperty  $Regkey).InstallPath -childpath 'Scripts\Initialize-PowerCLIEnvironment.ps1')
  35. }
  36. if ( !(Get-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) ) {
  37.     Write-Host "VMware modules not loaded/unable to load"
  38. }
  39.  
  40. ### Read CSV
  41. $csv = Import-Csv $CSVpath -Delimiter ","
  42. $csvHostnames = $CSV.Hostname
  43.  
  44. ### Connect vSphere
  45. Connect-VIServer -server $vCenterserver -user $vCenterUser -Password $vCenterPassword
  46.  
  47. ### Create VM and set Mac
  48. foreach ($hostnames in $CSV) {
  49.  
  50. $hostname = $hostnames.hostname
  51. $MacPVS = $hostnames.MacPVS
  52. $MacProd = $hostnames.MacProd
  53.  
  54. New-VM -Name $hostname -Template $VMTemplate -VMHost $VMHost -Datastore $Datastore
  55. Get-VM $hostname | Get-NetworkAdapter | ?{$_.NetworkName -eq $PRODLAN } | Set-NetworkAdapter -MacAddress $MacProd -Confirm:$false
  56. Get-VM $Hostname | Get-NetworkAdapter | ?{$_.NetworkName -eq $PVSLAN} | Set-NetworkAdapter -MacAddress $MacPVS -Confirm:$false
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement