Category Archives: PowerShell

Function to Add a USB Device to ESX Host

PowerCLI has a function to remove a USB device from a host, Remove-UsbDevice. However, what if you need to add a usb device to a VM.  Well you can with a bit of PowerCLI magic.  This solution assumes the device you wish to add remains in the same USB port and that the VM will be on the same host as the USB device of interest.  If you wish you can hack around to insert the host ID and change from path: option to pid: and vid: options.  You can read more about the option at VMware pubs.

Below is a simple function for Add-UsbDevice. It takes a VM name (wildcard if you know it will be unique for ease) and then the path is normally in the form of BUS/0/DEVICE i.e. “2/0/5”.

1 # 2 # Simple to use once connected to the server of interest (not vCenter) 3 # Set-UsbDevice -Name "My VM Name" -UsbPath "2/0/5" 4 # 5 # Find out the UsbPath from lsusb - typically BUS/0/DEVICE - but not guaranteed! 6 # 7 8 Function Set-UsbDevice 9 { 10 11 # We need both parameters 12 [CmdletBinding()] 13 Param( 14 [Parameter(Mandatory=$True,Position=1)] 15 [string]$Name, 16 17 [Parameter(Mandatory=$True)] 18 [string]$UsbPath 19 ) 20 21 #Get our VM then then ID 22 $VM = Get-VM $Name 23 $Id = $VM.Id 24 25 #Create a new Virtual Machine Configuration Specification 26 $newSpec = New-Object VMware.Vim.VirtualMachineConfigSpec 27 $newSpec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec[] (1) 28 $newSpec.deviceChange[0] = New-Object VMware.Vim.VirtualDeviceConfigSpec 29 $newSpec.deviceChange[0].operation = "add" 30 $newSpec.deviceChange[0].device = New-Object VMware.Vim.VirtualUSB 31 $newSpec.deviceChange[0].device.key = -100 32 $newSpec.deviceChange[0].device.backing = New-Object VMware.Vim.VirtualUSBUSBBackingInfo 33 $newSpec.deviceChange[0].device.backing.deviceName = "path:$UsbPath host:localhost" 34 $newSpec.deviceChange[0].device.connectable = New-Object VMware.Vim.VirtualDeviceConnectInfo 35 $newSpec.deviceChange[0].device.connectable.startConnected = $true 36 $newSpec.deviceChange[0].device.connectable.allowGuestControl = $false 37 $newSpec.deviceChange[0].device.connectable.connected = $true 38 $newSpec.deviceChange[0].device.connected = $false 39 40 $_this = Get-View -Id "$Id" 41 $_this.ReconfigVM_Task($newSpec) 42 43 }

If you want to find out the path on your host then using the shell you can use lsusb:

~ # lsusb
Bus 02 Device 06: ID 0424:4030 Standard Microsystems Corp.
Bus 02 Device 05: ID 03f0:1fe0 Hewlett-Packard
Bus 02 Device 04: ID 0fde:ca04
Bus 02 Device 03: ID 0424:2660 Standard Microsystems Corp.
Bus 02 Device 02: ID 8087:8000 Intel Corp.
Bus 02 Device 01: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 01 Device 04: ID 0a12:0001 Cambridge Silicon Radio
Bus 01 Device 03: ID 0403:6001 Future Technology Devices
Bus 01 Device 02: ID 8087:8008 Intel Corp.
Bus 01 Device 01: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 03 Device 01: ID 1d6b:0001 Linux Foundation 1.1 root hub

Use the command like this in PowerCLI once you have connected to the Host:

Set-UsbDevice -name "vSphere *" -UsbPath "2/0/5"

Any questions then please leave a comment.

Simple PowerShell to add PortableApps to your Windows 8 or 8.1 Start Menu

Copy the below code to your machine and edit the $folder line to match the location of your PortableApps you can check you have the correct folder by doing a dir with it and ensuring all your portable apps come up.

To undo what the script does then remove the folder “%appdata%\microsoft\windows\Start Menu\Programs\Portable\“

No warranty or suchlike – for information only.  Publication requires leaving a comment with a reference to each article and source must be provided with all use

 

Save it to your DropBox to run when you go onto a new machine.  Will work best if you always mount your DropBox drive in the same location.  You can always adjust the script to search for the Dropbox folder

### ### Copyright (C) Al West, 2014, TSEW, UK. ### ### Change the line of code below to match the folder where your ### portable apps are - if you omit the filter then everything found ### will be added. Not good! ### ### To delete links remove the following folder ### (copy and paste into explorer window): ### %appdata%\microsoft\windows\Start Menu\Programs\Portable\ ### ### Running this again will only recreate the shortcuts ### It will not delete redundant ones. ### Also not all files have portable in their name so it doesn't ### cover everything. ### $folder = "D:\Dropbox\Portable\PortableApps\*Portable.exe" ### ############################################################## function makeSC ( [string]$portableApp ) { $appName = [io.path]::GetFileNameWithoutExtension($portableApp) $location = $env:APPDATA + "\Microsoft\Windows\Start Menu\Programs\Portable\" $WshShell = New-Object -ComObject WScript.Shell Write-Host "Creating:" $appName " <<>> " $portableApp Write-Host " in:" $location $Shortcut = $WshShell.CreateShortcut( $location + $appName + ".lnk") $Shortcut.TargetPath = $portableApp $Shortcut.Save() } $files = Get-ChildItem -Path ($folder) -Recurse foreach ($file in $files) { makeSC ($file) }

Simple Storing of File Dates and Restoration with PowerShell

All sorts of things mess around with file dates and of concern to me was a number of home videos I transcoded that ended up with new file creation dates that I wanted to retain against the original write time.  We can handle this in Windows with a little PowerShell.  I’ll assume here that all files are in the same path and without their extensions, all files have unique names.

First capture the original creation dates:

dir | Export-CSV originaldates.csv

After your file edits are complete you use the following script (note there is line in there that will swap the extension):

Import-Csv originaldates.csv | `
    ForEach-Object {
        $originalFile = $_.Name
        $newFile = $originalFile.Split(“.”)[0] + “.mp4”
        $oldDate = get-date -date $_.LastWriteTime
        Get-ItemProperty -Path $newFile | ForEach-Object { $_.LastWriteTime = $oldDate }
    }

This post is mostly a reminder for me but might help you out too.  Always backup before changing files and be careful!