Skip to content

Instantly share code, notes, and snippets.

$strNewVPG = "newVirtualPortGroup"
$strNewVlanTag = "123"
$ObjAllHosts = Get-VMHost | Sort-Object -Property Name
ForEach($objHost in $ObjAllHosts){
$strVSwitch = Get-Virtualswitch -VMHost (Get-VMHost $objHost) | Where-Object { $_.Name -like "VMswitch" }
Write-Host "Adding Virtual Port Group $strNewVPG with VLAN Tag $strNewVlanTag to $objHost"
New-VirtualPortGroup -Name $strNewVPG -VirtualSwitch $strVSwitch -VLanId $strNewVlanTag
}
$strOldVPG = "OldVPGName"
$strNewVPG = "NewVPGName"
$ObjAllHosts = (Get-VMHost | Sort-Object -Property Name)
ForEach($objHost in $ObjAllHosts){
Write-Host "Changing Virtual Port Group Settings on" $objHost
$strVSwitch = Get-Virtualswitch -VMHost (Get-VMHost -Name $objHost) | Where-Object { $_.Name -match "VMswitch" }
$objOldVPG = Get-VirtualPortGroup (Get-VMHost -Name $objHost) | Where-Object { $_.Name -match $strOldVPG }
Write-Host "Removing Virtual Port Group" $objOldVPG
Remove-VirtualPortGroup -VirtualPortGroup $objOldVPG -Confirm:$false -WhatIf
Write-Host "Adding Virtual Port Group" $strNewVPG "with VLAN Tag" $objOldVPG.VLanID
@BenNeise
BenNeise / AddDiskSizeAsACustomAttribute.ps1
Created October 29, 2013 13:55
Add disk size as a custom attribute. Old script, there are easier ways to do it these days. http://ben.neise.co.uk/index.php/2009/04/add-disk-size-information-to-the-vi-client-using-powershell/
$VCServerName = "MYVCSERVER"
$VC = Connect-VIServer -Server $VCServerName
$SI = Get-View ServiceInstance
$CFM = Get-View $SI.Content.CustomFieldsManager
# Variables
$CustomFieldName = "HD Size (GB)"
$ManagedObjectType = "VirtualMachine"
# Check if the custom field already exists
@BenNeise
BenNeise / AddDrivePersistenceAsCustomAttribute.ps1
Last active December 26, 2015 21:19
Add drive persistence as a custom attribute for VMs and Templates
$VCServerName = "MyVCServer"
$VC = Connect-VIServer $VCServerName
$SI = Get-View ServiceInstance
$CFM = Get-View $SI.Content.CustomFieldsManager
# Variables
$CustomFieldName = "HD Persistence"
$ManagedObjectType = "VirtualMachine"
# Check if the custom field already exists
# Check if Servers on Text List exist on VMware
# Assumes that the VM object name matches the server's DNS name
# Set this to the text file containing the list of servers, one per line
$strServerList = "C:\path\to\textFile.txt"
# Create empty array for servers which are found
$arrFoundServers = @()
# Assign all of the VM objects to a variable
# Get all templates
$objTemplates = Get-Template
# Loop through the templates
ForEach ($objTemplate in $objTemplates){
# Set the $StrInterimTemplateName variable to the template name, replacing the string "Tmpl" with an empty string
$StrInterimTemplateName = ($objTemplate.Name -replace("Tmpl",""))
# As the string we've just removed might be anywhere in the name, we need to replace double spaces with single
$StrInterimTemplateName = ($StrInterimTemplateName -replace(" "," "))
# And also remove trailing spaces from the start, or the end of the string
@BenNeise
BenNeise / AddHashTableOfVirtualPortGroupsTovSphereHosts.ps1
Created October 29, 2013 14:52
Script to add a hash table full of virtual port groups to vSphere hosts
# Sets up virtual port groups on all hosts connected to a specific vCenter Server
# Name of vCenter Server
$strVCenterServer = "your.vCenter.Server"
# VLANs and associated VPGs
$ArrVLANs = @{
"123" = "vlanA";
"456" = "vlanB";
"789" = "vlanC";
@BenNeise
BenNeise / getVCenterBuildNumbers.ps1
Created October 29, 2013 14:54
Script to connect to a list of vCenter Servers, and get their version numbers, as well as the version numbers of their hosts
# Script to connect to a list of vCenter Servers, and get their version numbers, as well as the version numbers of their hosts
# Ben Neise
# 02/10/09
# Array of vCenter Servers
$arrVCenterServers = @("server1","server2","server3")
# Create empty arrays for the results
$arrTableVCs = @()
$arrTableHosts = @()
# Script to deploy linked clones
# List of custom attributes which you're wanting to copy from the template or parent to the newly created machine
# (Machines deployed from templates no longer inherit CAs in vSphere 4.0)
# These help us track provenance, and provide information to the user
$arrStrAttributesToCopy = @(
"AD Object Location",
"Customisation",
"Infrastructure Consultant",
"Logon Administrator Name",
@BenNeise
BenNeise / Invoke-SubversionScript.ps1
Created November 20, 2013 08:59
Given a valid PS1 file at a Subversion URL and credentials (Basic authentication) invokes the script on the local machine.
Function Invoke-SubversionScript {
<#
.Synopsis
Runs a script directly from Subversion.
.Description
Given a valid Subversion URL and credentials (Basic authentication) invokes the script on the local machine.
.Parameter Url
The URL of the script. Should be a valid URL, and end in PS1