Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .NOTES
- ===========================================================================
- Created on: 18-2-2018
- Created by: Chris Twiest
- Organization: Workspace-Guru.com
- ===========================================================================
- .DESCRIPTION
- This script will read in a CSV file with Devicenames, PVS MAC, PROD MAC, PROD IP and PVS IP.
- It will create these machines in VMWare with PowerCLI from a template and set the Mac Address for PVS and PROD network.
- #>
- #### Parameters
- $CSVPath = "C:\Temp\PVS.csv"
- $vCenterserver = "vcenter01.domain.com"
- $vCenterUser = "administrator@domain.com"
- $vCenterPassword = "P@ssw0rd!"
- $VMHost = "vmhost01.domain.com"
- $VMTemplate = 'CXA-Template-2016'
- $Datastore = 'VMWare01'
- $PRODLan = "VM Network"
- $PVSLan = "PVS Network"
- #### Load in PowerCLI
- # Returns the path (with trailing backslash) to the directory where PowerCLI is installed.
- if ( !(Get-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) ) {
- if (Test-Path -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\VMware, Inc.\VMware vSphere PowerCLI' ) {
- $Regkey = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\VMware, Inc.\VMware vSphere PowerCLI'
- } else {
- $Regkey = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\VMware, Inc.\VMware vSphere PowerCLI'
- }
- . (join-path -path (Get-ItemProperty $Regkey).InstallPath -childpath 'Scripts\Initialize-PowerCLIEnvironment.ps1')
- }
- if ( !(Get-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) ) {
- Write-Host "VMware modules not loaded/unable to load"
- }
- ### Read CSV
- $csv = Import-Csv $CSVpath -Delimiter ","
- $csvHostnames = $CSV.Hostname
- ### Connect vSphere
- Connect-VIServer -server $vCenterserver -user $vCenterUser -Password $vCenterPassword
- ### Create VM and set Mac
- foreach ($hostnames in $CSV) {
- $hostname = $hostnames.hostname
- $MacPVS = $hostnames.MacPVS
- $MacProd = $hostnames.MacProd
- New-VM -Name $hostname -Template $VMTemplate -VMHost $VMHost -Datastore $Datastore
- Get-VM $hostname | Get-NetworkAdapter | ?{$_.NetworkName -eq $PRODLAN } | Set-NetworkAdapter -MacAddress $MacProd -Confirm:$false
- Get-VM $Hostname | Get-NetworkAdapter | ?{$_.NetworkName -eq $PVSLAN} | Set-NetworkAdapter -MacAddress $MacPVS -Confirm:$false
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement