november 2008 - Posts

list the number of Virtual Machines pr. Datastore
24 nov 2008 15:35

This VI Toolkit script lists how many VMs are associated with each datastore. Additionally it shows datastore capacity, freespace, and % freespace.

It prompts for output file (which is HTML) when you run the script, and automatically loads the .html file aftwards.

This script can be way smaller. If you dont care about fancy HTML output (i know i do) and automacically launching the output file, just cut those sections out.

# Asks user for HTML output file
$strFile = (Read-Host "Enter full path to HTML output file:")

# Defines HEAD information for HTML formatting
$head = "<style>`n"
$head += "body {font-family: Verdana; font-size: 11px;}`n"
$head += "table {border-width: 1px;}`n"
$head += "td {border-width: 1px; padding: 2px; border-style: solid; border-color: #afafaf;}`n"
$head += "th {text-align: left;}`n"
$head += "</style>`n"

# Connect to VC
Connect-VIServer -Server <server_name>

# Gets datastores and creates empty array
$ds = Get-Datastore | Sort-Object Name | % { Get-View $_.ID }
$myCol = @()

# Loops thrue each entry and renames VM property
Foreach ($store in $ds)
{
 $myObj = "" | Select-Object Datastore, VMs, "Size (GB)", "Free (GB)", "Free (%)"
 $myObj."Datastore" = $store.Info.Name
 $myObj."VMs" = $store.Vm.Length
 $myObj."Size (GB)" = [math]::Round( ($store.Summary.Capacity/1GB),0 )
 $myObj."Free (GB)" = [math]::Round( ($store.Summary.Freespace/1GB),0 )
 $myObj."Free (%)" = [math]::Round( ($store.Summary.Freespace/$store.Summary.Capacity * 100),0 )
 $myCol += $myObj
}

# Converts array output to HTML
$myCol | ConvertTo-Html -Head $head > $strFile

# Creats IE object, and launches the output file
$objIE = New-Object -com "InternetExplorer.Application"
$objIE.Navigate($strFile)
$objIE.ToolBar = 0
$objIE.StatusBar = 0
$objIE.Visible = $True

 Remember to change the <server_name> value to your VC server IP/Hostname

 

reset ESX root password using VI toolkit
17 nov 2008 15:30

The first credential box is for VirtualCenter login, then the script asks for the current root password (through a read-host)

Second credential box is for the new root password (to secure the input)

Connect-VIServer -Server <server_name>

$rootpswd = (Read-Host "Type the CURRENT root password")
$newrootpswd = Get-Credential root

$accspec = New-Object VMware.Vim.HostPosixAccountSpec
$accspec.id = "root"
#$accspec.password = $newrootpswd
$accspec.Password = $newrootpswd.GetNetworkCredential().Password
$accspec.shellAccess = "/bin/bash"

#Use Get-VMHost -Name "*<filter>*" if you want to target specific ESX hosts
Get-VMHost | Sort Name | %{

Connect-VIServer $_.Name -User root -Password $rootpswd
$si = Get-View ServiceInstance
$acctMgr = Get-View -Id $si.content.accountManager
 
$acctMgr.UpdateUser($accspec)
}

Thanks to LucD for input at the VMware communities!