Category: PowerShell

PowerCLI: Search VM By MAC Address

PowerCLI: Search VM By MAC Address

The below snippet is pretty useful for when you have a mac address conflict between your VM’s. The VMware alarm/flag does not tell you the VM that it MAC conflicts with so time to open up PowerCLI and run the below example

 

Get-VM | Get-NetworkAdapter | Where {$_.MacAddress -eq “00:59:59:c9:da:9a”} | select @{Name="VM";Expression={$_.Parent}},MacAddress | ft -AutoSize

 

Advertisements
PS: Windows Update

PS: Windows Update

Needing to run Windows Updates on your machine from PowerShell this is the module for you.

https://gallery.technet.microsoft.com/scriptcenter/2d191bcd-3308-4edd-9de2-88dff796b0bc

Download and extract the folder a place it in the following directory

%WINDIR%\System32\WindowsPowerShell\v1.0\Modules

Import the module into your PowerShell session

Import-Module PSWindowsUpdate

To download and install windows updates run the following

Get-WUInstall

Screenshot of Module In action

powershell-windows-update-screenshot

PowerCLI: Rename Windows Guest OS Hostname

PowerCLI: Rename Windows Guest OS Hostname

Okay, today I had run a script but I had not taken into account the character limit of the Windows hostname. The Result was I had 14 VM’s that are all running with the same hostname and I was not prepared to go into every single VM and change the hostname manually.

So the solution, I had been fishing around the internet for a quick solution to this problem and the solution was to use PowerCli with the Invoke-VMscript command which turns out to be pretty powerful so below is an example of using Invoke-VMscript to change the Windows Hostname

Invoke-VMScript -VM VM-Windows-01 -ScriptType Powershell -ScriptText 'Rename-Computer -NewName VM-WIN-01 -Restart' -GuestUser Administrator -GuestPassword ChangeMe
Invoke-VMScript -VM VM-Windows-02 -ScriptType Powershell -ScriptText 'Rename-Computer -NewName VM-WIN-02 -Restart' -GuestUser Administrator -GuestPassword ChangeMe
Invoke-VMScript -VM VM-Windows-03 -ScriptType Powershell -ScriptText 'Rename-Computer -NewName VM-WIN-03 -Restart' -GuestUser Administrator -GuestPassword ChangeMe

 

PowerCLI: SAN Usage Report

PowerCLI: SAN Usage Report

 

Okay, today I have been tasked to find out where all of our storage has gone and because this SAN LUN is shared between a multiple of systems I have decided to generate the report based on Folder size within the LUN. Please see below for the script to run from PowerCLI

$searchSpec1 = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
$searchSpec1.details = New-Object VMware.Vim.FileQueryFlags
$searchSpec1.details.fileSize = $true
$searchSpec1.query += New-Object VMware.Vim.FolderFileQuery
$searchSpec2 = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
$searchSpec2.details = New-Object VMware.Vim.FileQueryFlags
$searchSpec2.details.fileSize = $true
$searchSpec2.query += New-Object VMware.Vim.FileQuery
$report = @()
Get-Datastore | Get-View | % {
$browser = Get-View -Id ($_.Browser)
$dsPath = "[" + $_.Summary.Name + "] /"
$folderList = $browser.SearchDatastore($dsPath, $searchSpec1)foreach($folder in $folderList.File){
$folderPath = $dsPath + $folder.Path
$fldresult = $browser.SearchDatastore($folderPath, $searchSpec2)
$fldSize = 0
foreach($file in $fldresult.File){$fldSize += $file.FileSize}
$row = "" | select DSName, FolderName, FolderSize
$row.DSName = $folderList.FolderPath
$row.FolderName = $folder.Path
$row.FolderSize = $fldSize
$report += $row
}
}
$report | Export-Csv "c:\powershell\Foldersize.csv" -noTypeInformation

Once you have harvested your CSV import it into excel and use the following on the custom format for file size

[<500000]#,##0" B ";[<500000000]#,##0,," MB";#,##0,,," GB"