Skip to content

Instantly share code, notes, and snippets.

Jonathan Barber jbarber

View GitHub Profile
View have_ballooned.ps1
# 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
View create-hosts.ps1
# 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 Jul 5, 2010
Set VM advanced options (stop ballooning)
View deballoon.ps1
# 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 Jul 5, 2010
Find all VMs with tools not OK
View tools-nok.ps1
# 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 Oct 12, 2010
Find all the ethernet MACs
View get-vm-macs.ps1
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 Nov 22, 2010
Create /etc/hosts listing for VMware hosts
View get-host-hosts.ps1
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 Nov 25, 2010
Add new local user to all the ESX servers managed by a vSphere vCenter
View add_esx_users.ps1
$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
View background.sh
ctrl-z
bg
touch /tmp/stdout
touch /tmp/stderr
gdb -p $!
# In GDB
p dup2(open("/tmp/stdout", 1), 1)
p dup2(open("/tmp/stderr", 1), 2)
@jbarber
jbarber / dns-update.clj
Created Apr 1, 2011
Clojure dnsjava example for doing DNS updates
View dns-update.clj
; 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 Apr 4, 2011
Example of using vijava API from clojure to list all hostnames/IPs for those VMs that VMware knows the IP of
View hosts.clj
; 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.
You can’t perform that action at this time.