PowerCLI function Change-Mac

As we needed to change a lot of MAC addresses because we needed to move a lot of VM’s to another vCenter. I created a function to change the MAC-Address of a VM trough PowerCLI.
Because the address is based on the vCenter ID we had some troubles in the past with SCCM and double MAC Addresses so we needed to take this opportunity to fix it to be compliant with the vCenter ID.

function Change-Mac{
<#
.SYNOPSIS
Generates a new Mac-Address based on the existing vCenter ID
.DESCRIPTION
This function creates a new MAC Address while the NIC
configuration is maintained
.NOTES
Source: The Interwebz
Authors: Me, Myself and I
.PARAMETER VM
Specify the virtual machine to change the MAC Address from
.EXAMPLE
PS> Change-Mac -VM VM001
.EXAMPLE
PS> (GC import.txt)|change-mac
#>
Param (
[Parameter(
Valuefrompipeline = $true,
ParameterSetName = "VM",
Mandatory = $true,
HelpMessage = "Enter VM name")]
[String[]]$VM)

process
{
foreach ($vmnic in $VM){
$objState = Get-VM $vmnic | Select-Object PowerState}
If ($objState -match "off")
{
$thisAdapter = Get-NetworkAdapter -Vm $vmnic
foreach ($nic in $thisAdapter)
{
$nic.ExtensionData.AddressType = "Generated"
$nic.ExtensionData.MacAddress = ""
}
Set-NetworkAdapter $thisAdapter -confirm:$false
Write-Host -ForegroundColor Green $vmnic "succesfully edited"
}
else
{
Write-Host -ForegroundColor red $vm "is not powered off, please turn off!"
}
}
}

Where is my PowerShell Profile ?

I was looking where to import functions in to the default Powershell profile.
The file with the custom functions is named : Microsoft.PowerShell_profile.ps1
Put your custom scripts in here and it will load as soon as your powershell session starts.

You also can use $profile to see where your personal profile is located.

It can be found at the locations:

32-bit “C:\Windows\SysWOW64\WindowsPowerShell\v1.0”
64-bit “C:\Windows\System32\WindowsPowerShell\v1.0”

 

 

Find duplicate VM UUID’s

As I created a script to gather all VM-s and their UUID, the purpose was to find all the duplicate UUID’s if there where any.
While my script was running I remembered something to find out the duplicates.
Before I forget I tought to put it here so it’s easily to find again

import-csv file.csv | Group-Object -Property UUID -noelement | ?{$_.Count -gt 1}?{$_.Count -gt 1}
1 2 3