Skip to content

Instantly share code, notes, and snippets.

View jbarber's full-sized avatar

Jonathan Barber jbarber

View GitHub Profile
# Find all of the VMs that have experienced ballooning in the last day
function ballooned? ($vm) {
$d = Get-Stat -memory -entity $vm -start (Get-Date).AddDays(-1) |
?{ $_.MetricId -eq 'mem.vmmemctl.average' } |
?{ $_.value -ne 0 }
return $d.count -ne 0
}
connect-viserver -server virtualcenter -credential (Get-Credential (Get-Item env:username).value)
$vms = get-view -viewtype virtualmachine
# Create /etc/hosts style output of IPs - VM names
get-view -viewtype virtualmachine | ?{ $_.guest.ipaddress } | format-table -hideTableHeaders {$_.guest.ipaddress},name
@jbarber
jbarber / deballoon.ps1
Created July 5, 2010 13:42
Set VM advanced options (stop ballooning)
# VMware: set the advanced configuration option sched.mem.maxmemctl to zero to prevent ballooning
function deballoon ($vmname) {
# Set "sched.mem.maxmemctl" to 0
$opt = new-object Vmware.Vim.OptionValue -Property @{ Key = "sched.mem.maxmemctl"; Value = 0 }
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.extraconfig += $opt
# Use a filter to find a particular VM
$vm = get-view -viewtype "VirtualMachine" -filter @{ "Name" = $vmname }
@jbarber
jbarber / tools-nok.ps1
Created July 5, 2010 13:56
Find all VMs with tools not OK
# Find all VMs without the tools at the latest version/enabled
connect-viserver -server virtualcenter
get-view -viewtype "VirtualMachine" `
| ?{ $_.runtime.powerstate -like 'poweredOn' } `
| ?{ $_.guest.toolsstatus -notlike 'toolsok' } `
| Sort-Object {$_.guest.toolsstatus} `
| format-table name, {$_.guest.toolsstatus}
@jbarber
jbarber / get-vm-macs.ps1
Created October 12, 2010 11:23
Find all the ethernet MACs
get-view -viewtype virtualmachine | %{
$vm = $_.name
$vm.mac = @()
$_.Config.Hardware.Device | ?{ $_.MacAddress } | %{ $vm.mac += $_.MacAddress };
$vm
} | format-table -wrap
@jbarber
jbarber / get-host-hosts.ps1
Created November 22, 2010 11:12
Create /etc/hosts listing for VMware hosts
get-view -viewtype HostSystem | %{
$_ | select-object @{ Name = "IP"; Expression = { $_.config.network.consolevnic | %{ $_.spec.ip.ipaddress } } },name
} | format-table -hidetableheaders
@jbarber
jbarber / add_esx_users.ps1
Created November 25, 2010 11:30
Add new local user to all the ESX servers managed by a vSphere vCenter
$vsphere = "my-vc"
$new_user = "newbie"
$new_user_passwd = "new_sekret"
$new_user_grp = "root"
$root_user = "root"
$root_passwd = "really_sekret"
# Get all of the ESX servers (connect using Windows credentials)
connect-viserver -server $vsphere
@jbarber
jbarber / dns-update.clj
Created April 1, 2011 16:07
Clojure dnsjava example for doing DNS updates
; Clojure example for updating a DNS zone using dnsjava 2.1.1:
; http://www.xbill.org/dnsjava/
; Tested against named 9.3.6 from Red Hat on RHEL5.4
; Run using: java -cp clojure.jar:dnsjava-2.1.1.jar clojure.main dns-update.clj
(import '(org.xbill.DNS Update Name Type TSIG SimpleResolver))
(defn replaceArec [resolver zone host ip ttl]
(let [update (Update. zone)]
(do
(. update replace
@jbarber
jbarber / hosts.clj
Created April 4, 2011 11:20
Example of using vijava API from clojure to list all hostnames/IPs for those VMs that VMware knows the IP of
; Return a list of vectors of hostname/ip address for VMware virtual machines from vCenter
; Export the environment variables:
; VI_USERNAME - your vCenter username
; VI_PASSWORD - your vCenter password
; VI_SERVER - the vCenter hostname/IP
; Invoke with the command line:
; java -cp clojure-1.2.0/clojure.jar:vijava2120100715.jar:dom4j-1.6.1.jar clojure.main hosts.clj
(import '(com.vmware.vim25.mo ServiceInstance InventoryNavigator))
(def si (ServiceInstance.
@jbarber
jbarber / get-sharepoint-list.pl
Created April 4, 2011 17:18
Dumping sharepoint lists from perl
#!/usr/bin/perl
# Script to retrieve a list from sharepoint, which contains
# /etc/hosts type information and dump it in hosts format
#
# It's recomended to enter the username on the command line and let it prompt
# for the password
#
# The column names for the list items are hard coded.
#