Wednesday, April 3, 2013

Start-RDP PowerShell cmdlet

Start-RDP cmdlet

I have for a long time been irritated about the missing RDP support in PowerShell. And for that reason i have created a wrapper function that can start RDP sessions without having to enter credentials in all sessions.

The code can be found over at PoshCode.Org/embed/4068 or below.



Monday, January 23, 2012

Powershell SVmotion Move VM one HD at a time

When i had to move some rather big vm's with 5-15 HD's and a size of 3-6TB attached, I wanted to ensure the vm didn't get corrupt. SO i wondered f it was possible to move vm's one HD at a time. and yes it is. but previously you had to do it via some very tricky calls to the api now its just a cmd-let that handles it.

$vm= get-vm $name
if (($vm) -and (get-datastore $datastore)) {
$Harddisks = Get-HardDisk -vm $vm
Foreach ($hd in $harddisks) {
       Set-HardDisk -HardDisk $hd -Datastore $datastore -Confirm:$false
$vm | move-vm -datastore $datastore -Confirm:$false

The script explained. first i find the vm, and the target datastore. then i step through the HD's one by one and use the set-harddisk cmd-let to place the HD on the target datastore. at the end i move the remainig part of the vm thats the vmx file, logs and so to the target drive.

This was done on ESX5i,  vcenter5.0 and PowerCLI 5.0

Tuesday, August 10, 2010

Do a task in your sleep. :-P

Have you had to start a logtime running script at inhuman hours, and never really found a way to do this except through the task manager. I created this small script that will accept any input and pass it along the pipe. Just prep the command and start it and it will look like starting at the specified time.

Param (
    [Parameter(Position=0, ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]$itemlist,

# ContinueAt format "YYYYMMDDHHmm"

BEgin {
    $now = get-date

    If ($ContinueAt -ne "") {
        $When = Get-date -year $ContinueAt.Substring(0,4) -Month $ContinueAt.SubString(4,2) -day $ContinueAt.Substring(6,2) -Hour $ContinueAt.Substring(8,2) -Minute $ContinueAt.Substring(10,2)

        while ($now -lt $when) {
            Write-Verbose "Sleeping until: $ContinueAt"
            Sleep 10
            $now = get-date
    Write-verbose "Done Sleeping"
    Foreach ($item in $itemlist) {

Hope this helps someone out there.

Thursday, September 10, 2009

Powershell ESX Lun Balance script

Back again from a long break. I found out that many of the unix based scripts are going to be obsolete within a couple of month. so here is a powershell version of my earlier published script. Thanks to Justin Emerson, for the basics of this script. a little copy paste and some mods.

# Cluster/host lun balance script
# Allan Christiansen
Write-Host "This script will modify the policy of all your shared LUNs on all ESX Servers" -ForegroundColor Cyan
Write-Host "in a Cluster to Fixed and select a preferred path in a round-robin fashion." -ForegroundColor Cyan
if ($args.Length -lt 2) {
Write-Host "usage: -c or -s "
exit 1
} else {
if ($args[0] -eq "-c") { $VMHosts = Get-Cluster $args[1] | Get-VMHost }
if ($args[0] -eq "-s") { $VMHosts = Get-VMHost $args[1] }
Write-Host "Modifiying ESX hosts:" $VMHosts
foreach ($VMHost in $VMHosts)
$luns = $VMHost|get-scsilun -luntype disk| where-object {$_.ConsoleDeviceName -like "/vmfs/devices/disks/vml*"}| Where-object {$_.CanonicalName -notlike "vmhba32*"} |Sort-Object CanonicalName
$firstLUNPaths = Get-ScsiLunPath $luns[0]
$numPaths = $firstLUNPaths.Length
$count = 0
foreach ($lun in $luns)
"count   : $count"
"numpath : $numPaths"
if ($count -ge $numPaths) { $count = 0 }
$paths = Get-ScsiLunPath -ScsiLun $lun
$lun|Set-ScsiLun -MultipathPolicy Fixed -PreferredPath $paths[$count]
$count += 1
# Sleep for some seconds as some arrays dont like doing this too fast. and with running vm's its .....
Start-Sleep -Seconds 3
# Thanks to Justin Emerson, for the basics of this.

Tuesday, February 10, 2009

ESX iscsi enable script using vi-toolkit.

Below is a script for enabling and rescanning Iscsi target for valid vmfs volumes. I'm using Vi-Toolkit for this.
Param ($VMhost = "")
$iscsiHba = "vmhba32"
$iscsiServer = "Iscsi-SAN-IP"
$iscsiPort = 3260
$target = New-Object VMware.Vim.HostInternetScsiHBASendTarget
$target.address = $iscsiServer
$target.port = $iscsiPort
$iscsiauthprop = New-Object VMware.Vim.HostInternetScsiHbaAuthenticationProperties
#$iscsiauthprop.ChapAuthEnabled = "true"
#$iscsiauthprop.ChapName = "Chapuser"
#$iscsiauthprop.ChapSecret = "ChapSecret"
$h = Get-VMHost $VMhost
Foreach ($hostView in ( Get-View -VIObject $h)) {
    $storageSystem = Get-View $hostView.configManager.storageSystem
    # Enable software iSCSI controller
    # Add iSCSI Server for dynamic discovery
    $storageSystem.AddInternetScsiSendTargets($iscsiHba, $target)
    # Scan for iSCSI devices

I used this for setting the same settings for all my hosts and ensuring that they all are 100% alike.

Friday, October 17, 2008

List vm usage and assosiated datastore

Hi all,
in a fairly large environment the tracking of a VM's Disk usage and the datastore on who i resides i a trivial task.
$datastoreExp = @{N="Datastore"; E={ ($_ | get-datastore | select-object -first 1).Name }}
$diskSizeExp = @{N="Total Disk"; E={ ($_ | get-harddisk | measure-object -property CapacityKB -sum).Sum }}
get-vm | select Name, $datastoreExp, $diskSizeExp | sort -property Datastore,"Total Disk"
 This small PowerShell script pulls a list of all VMs and the disk ussage pr. vm and on witch datastore it is connected to/placed. Be aware if an iso is mounted from a datastore this will generate an aditional entry for the VM in question.

Thursday, September 18, 2008

Updating tools on VM's in Vmware

I'm back, after a long break, with other important stuff on the horizon. I realize i have a couple of hundred vm's that needed the tools to be update and i didn't want to do it manually one by one. So i again looked into the VI-toolkit for Vmware and found it pretty easy to update all vm's with a simple script.
$VmList = Get-VM | Where-Object {$_.Powerstate -eq "PoweredON"} | % { Get-View $_.ID } | where {$_.guest.toolsstatus -notmatch "Ok"} | Select Name
foreach ($vm in $VmList) {
  "Updating :" $vm
  Update-Tools $vm
I hope this can help others.