Create fluid apps with shell script

With netflix now available in Sweden I made an fluid app for netflix to have in my dock.

I found the script over at lifehacker.

It is really useful because you get to specify what browser you want the fluid app to launch.

Mac: Run Script before or after Wake from Sleep

I have a few scripts that I want want my macbook to run just before I put it to sleep (by simply closing the lid).

To achive this I did the following:

  1. I installed sleepwatcher via brew
    brew install sleepwatcher
  2. Then I copied the launchd files to the ~/Library/LauchAgents directory
    cp /usr/local/Cellar/sleepwatcher/2.2/de.bernhard-baehr.sleepwatcher-20compatibility-localuser.plist ~/Library/LaunchAgents
  3. I restarted my Macbook and created the ~/.sleep script which contains the commands that will run before sleep.. (You could also create a ~/.wakeup script that runs when the mac wakes up.)

Source: EchoDitto Labs, Stack Overflow

Linked: eagerZeroedThick powershell script

I found this great Powershell script that convert virtual disk files to eagered zeroed.

Awesome script! I had to make some changes to make it work in my environment.

 function Set-EagerZeroThick{
 param($vcName, $vmName, $hdName)

# Find ESX host for VM
 $vmImpl = Get-VM $vmName
 Write-Host $vmImpl
 if($vmImpl.PowerState -ne "PoweredOff"){
 Write-Host "Guest must be powered off to use this script !" -ForegroundColor red
 return $false
 }

 $vm = $vmImpl | Get-View
 Write-Host $vm
 $esxName = (Get-View $vm.Runtime.Host).Name
 Write-Host $esxName
# Find datastore path
 $dev = $vm.Config.Hardware.Device[10] | where {$_.DeviceInfo.Label -eq $hdName}
 Write-Host $dev
 if($dev.Backing.thinProvisioned){
 return $false
 }
 $hdPath = $dev.Backing.FileName
 Write-Host $hdPath

# For Virtual Disk Manager we need to connect to the ESX server
 $esxHost = Connect-VIServer -Server $esxName -User $esxAccount -Password $esxPasswd

# Convert HD
 $vDiskMgr = Get-View -Id (Get-View ServiceInstance -Server $esxHost).Content.VirtualDiskManager
 Write-Host $vDiskMgr
 $dc = Get-Datacenter -Server $esxHost | Get-View
 Write-Host $dc
 $taskMoRef = $vDiskMgr.EagerZeroVirtualDisk_Task($hdPath, $dc.MoRef)
 Write-Host $taskMoRef
 $task = Get-View $taskMoRef
 Write-Host $task
 while("running","queued" -contains $task.Info.State){
 $task.UpdateViewData("Info")
 }

 Disconnect-VIServer -Server $esxHost -Confirm:$false

# Connect to the vCenter
 Connect-VIServer -Server $vcName
 if($task.Info.State -eq "success"){
 return $true
 }
 else{
 return $false
 }
}

$vmName = "vm1"
$vCenter = "vcenter_server"
$esxAccount = "rootuser"
$esxPasswd = "password"

Set-EagerZeroThick $vCenter $vmName "Hard disk 1"

To validate before and after running the eageredZeroed script I run the following script:

$vm = Get-VM vm1 | Get-View
$vm.config.Hardware.Device[10].backing
$vm.config.Hardware.Device[10].DeviceInfo

Source: http://www.lucd.info/2009/11/15/scripts-for-yellow-bricks-advise-thin-provisioning-alarm-eagerzeroedthick/

Linked: Move-Template

A couple of days ago I found a really great PowerCLI script that move templates i vSphere. Awesome post and script by afokkema at ict-freak.nl!

Storage vMotion is a great feature to Move your VMs to other datastores. But what if you want to move your Templates?
In the current version of vSphere there is no option within the Client

function Move-Template{
    param( [string] $template, [string] $esx, [string] $datastore)

    if($template -eq ""){Write-Host "Enter a Template name"}
    if($esx -eq ""){Write-Host "Enter an ESX hostname"}
    if($esx -ne "" -and $datastore -eq ""){$vmotion = $true}
    if($datastore -ne ""){$svmotion = $true}

    Write-Host "Converting $template to VM"
    $vm = Set-Template -Template (Get-Template $template) -ToVM 

    if($svmotion){
        Write-Host "Migrate $template to $esx and $datastore"
        Move-VM -VM (Get-VM $vm) -Destination (Get-VMHost $esx) `
        -Datastore (Get-Datastore $datastore) -Confirm:$false
        (Get-VM $vm | Get-View).MarkAsTemplate() | Out-Null
    }        

    if($vmotion){
        Write-Host "Migrate $template to $esx"
        Move-VM -VM $vm -Destination (Get-VMHost $esx) -Confirm:$false
        ($vm | Get-View).MarkAsTemplate() | Out-Null
    }
}

The function above can be used to move a single template via:

Move-Template <template> <esxhost> <datastore>

Source: http://ict-freak.nl/2010/01/21/powercli-move-template/

Linked: Delete Files Older Than x Days on Linux – How-To Geek

Great post on how to delete files older than x days on linux. The How-To Geek is awesome, as usual!

Command Syntax

find /path/to/files* -mtime +5 -exec rm {} \;

Note that there are spaces between rm, {}, and \;

Explanation

  • The first argument is the path to the files. This can be a path, a directory, or a wildcard as in the example above. I would recommend using the full path, and make sure that you run the command without the exec rm to make sure you are getting the right results.
  • The second argument, -mtime, is used to specify the number of days old that the file is. If you enter +5, it will find files older than 5 days.
  • The third argument, -exec, allows you to pass in a command such as rm. The {} \; at the end is required to end the command.

This should work on Ubuntu, Suse, Redhat, or pretty much any version of linux.

Delete Files Older Than x Days on Linux – How-To Geek.

Export a list of all computers in an AD OU

Today I struggled with the problem of exporting a list of computers in a Windows Domain OU to a simple text file. The purpose of the text file is to act as an input for some other neat scripts.

I stumbled across a post on thebackroomtech.com that made the it seem like a piece of cake.

To export a list of all computers and non domain controller servers in an Active Directory OU, use dsquery.exe.  For example, to export all computers in mydomain.com’s servers OU to machines.txt :

DSQUERY COMPUTER “OU=servers,DC=mydomain,DC=com” -o rdn -limit 1000 > c:\machines.txt

Simple and nice!