Saturday, 25 February 2012

PowerShell script to launch Netflix in UK / US mode

I'm a Netflix subscriber in the UK and it was brought to my attention that Netflix can be launched in US mode by changing which DNS servers you point at. This then allows you to access different content based on the location. As I use my Media Center PC to display content I figured a nice desktop icon would be a good option.

This caused me to write the following PowerShell script to automate the whole process.
The script gets your existing DNS entries (multiple NICs work) and saves them to a file. It then changes the DNS entries of all IP enabled NICs to those specified in the script (I've used Sky DNS servers for the UK ones). It then launches IE and points to in a Theater style window. At that point the script waits for IE to be closed then it reverts the DNS entries on the NICs back to their original settings.

I've tested this on two machines so far; one with a single external NIC and one with two external NICs. This now works for me.

I created the Netflix US shortcut on the desktop with the following line:

powershell.exe -command "& 'C:\PATH\netflix.ps1' US"

I created the Netflix UK shortcut on the desktop with the following line:

powershell.exe -command "& 'C:\PATH\netflix.ps1' UK"

Give it a try and let me know how you get on...
$DNSUK = "",""
$DNSUS = "",""
$Website = ""
($DNS -eq "UK")
{$SetDNS = $DNSUK}
($DNS -eq "US")
{$SetDNS = $DNSUS}
{"You need to specify UK or US. i.e. Netflix.ps1 UK"
#Let's obtain your current DNS settings
$NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled=True"
$File = New-Item -type file "NIC Information.txt" -Force
foreach ($NIC in $NICs)
$NICName = $NIC.Description

$OriginalDNS = $NIC.DNSServerSearchOrder
$DHCPSetting = $NIC.DHCPEnabled
#Let's determine if you're a DHCP client
($DHCPSetting -eq "True")
{"Your DNS servers are automatically assigned"
add-content $file "$NICName,$DHCPSetting,NULL"}
{"Your DNS servers are manually set to: $OriginalDNS"
add-content $file "$NICName,$DHCPSetting,$OriginalDNS"}

#Set DNS Setting to location specified
"For this session your DNS has been changed to $SetDNS"
#END of foreach
#Open Internet Explorer
$IE=New-Object -com internetexplorer.application

#Monitor Internet Explorer process
"Waiting for Internet Explorer window to close"
(Get-Process -Name iexplore)| Where-Object {$_.MainWindowHandle -eq $ie.HWND}| Wait-Process
#Set DNS back to original setting
$list=Get-content ".\NIC Information.txt"
Foreach ($line in $list)
 $splitLine = $line.split(",")
 $Desc = $splitLine[0]
 $DHCPStatus = $splitLine[1]
 $DNSFromFile = $splitLine[2]

($DHCPStatus -eq "True")
"DHCP was enabled on adapter $Desc"
$NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled=True" | where{$_.Description -eq "$Desc"}
"DHCP was not enabled on adapter $Desc"
#End of foreach
del ".\NIC Information.txt"

Saturday, 11 February 2012

Create VHD (PowerShell version)

In an attempt to learn PowerShell I've updated the CreateVHD script. It should now be saved as CreateVHD.ps1. The same parameters as before are required:

CreateVHD.ps1 {VHD Name} {Size in GB} {Configure BootFromVHD : YES}

A new feature of this version is that it creates a batch file to remove the GUID entry from the boot process using BCDEDIT. The new file will be named Delete - {VHD Name}.cmd.

See previous post for more information:

#Define Parameters Section
#Variables section
$WimFile = "E:\Sources\install.wim"
$ImageIndex = 1
$TargetDrive = "C:"
$VHDFolder = "\VMs"
#Verify parameters meet conditions
($VDisk -eq "")
{"You need to specify a name for the Virtual Disk file {Parameter 1}"
$VDiskFP = "$TargetDrive$VHDFolder\$VDisk.vhd"
"Virtual Hard Disk Name: $VDiskFP"}
($DiskSizeGB -eq "")
{"You need to specify a disk size in GB {Parameter 2}"
$DiskSizeMB = $DiskSizeGB * 1024
"Disk size is $($DiskSizeMB)MB"
($BootFromVHD -eq "")
{"Boot from VHD will not be enabled"}
($BootFromVHD -eq "YES")
{"Boot from VHD will be enabled"}
{"Boot From VHD if specified must be set to Yes {Parameter 3}"
((Test-Path -path $VDiskFP) -eq $True)
"Error: $VDiskFP already exists"
#Create the Diskpart-create.txt file
$File = New-Item -type file "Diskpart-create.txt" -Force
add-content $file "create vdisk file=$VDiskFP maximum=$DiskSizeMB type=expandable"
add-content $file "select vdisk file=$VDiskFP"
add-content $file "attach vdisk"
add-content $file "create partition primary"
add-content $file "format quick FS=NTFS label=$VDiskFP"
add-content $file "assign letter=v"
add-content $file "active"
diskpart /s Diskpart-create.txt
del diskpart-create.txt
"Applying Windows image No. $ImageIndex from $WimFile to $VDiskFP"
.\imagex /apply $WimFile $ImageIndex V:
bcdboot V:\Windows /s V:

#Create the Diskpart-detach.txt file
$File = New-Item -type file "Diskpart-detach.txt" -Force
add-content $file "select vdisk file=$VDiskFP"
add-content $file "detach vdisk"
diskpart /s Diskpart-detach.txt
del diskpart-detach.txt
#Create the Boot From VHD Menu Option
($BootFromVHD -eq "YES")
$VdiskBCD = ('"{0}"' -f $VDisk)
$VdiskBCD = " bcdedit /copy {current} /d $VdiskBCD "
$VdiskBCD = ('"{0}"' -f $VDiskBCD)
$VdiskBCD = "cmd /c $VdiskBCD > GUID.txt"
$File = New-Item -type file "RunBCD.cmd" -Force
add-content $file "@echo off"
add-content $file "$VdiskBCD"
del .\RunBCD.cmd
$GUID = get-content .\GUID.txt
$GUID = $GUID -split "to ", 2
$GUID = $GUID -split ".", 0, "simplematch"
$GUID = $GUID[1]
del .\GUID.txt
cmd /c "bcdedit /set $GUID device vhd=[locate]$VHDFolder\$Vdisk.vhd"
cmd /c "bcdedit /set $GUID osdevice vhd=[locate]$VHDFolder\$Vdisk.vhd"
cmd /c "bcdedit /set $GUID detecthal on"
cmd /c "bcdedit /default $GUID"

#Create a batch file to remove the GUID from the boot config
$File = New-Item -type file "Delete - $Vdisk.cmd" -Force
$DeleteGUID = "bcdedit /delete $GUID"
$DeleteGUID = ('"{0}"' -f $DeleteGUID)
$DeleteGUID = "cmd /c $DeleteGUID"
add-content $file "$DeleteGUID"

Monday, 6 February 2012

Script to create VHD, populate it from WIM file and then set the boot from VHD option

I've been working in my lab environment at home on the Private Cloud stuff and I tend to use Oracle's VirtualBox product. I decided that I wanted to change one of my machines to be a physical box though as I'm somewhat resource constrained. Normally I use the VBoxmanage clonehd to clone my Sysprepped base machine; however that's not going to work in the physical world. So I fired up my BaseVM using a Win 2008 R2 setup DVD, imagex'd the drive and transferred my new WIM file out to my host machine.

I then wrote the following script which creates the VHD file using diskpart, populates it using imagex, detaches the disk and then optionally adds the VHD file to the boot from VHD option on Win7 / 2008 R2. This allows me to fire up a new machine fairly quickly on either physical or virtual hardware. Given the VHD nature of the file it's also even easier to switch to different hypervisors such as Hyper-V.

Note1: The credit for some of the script belongs to Dan Stolts from the following article:
Note2: You will also need imagex.exe either in the same location as the script or in a pathed location.
Note3: Remember to specify your variables in the variables section.

------SAVE BELOW AS A .CMD/.BAT FILE-------------

REM This batch file will perform the following actions
REM Create a virtual disk (VHD format)with the name of the first parameter
REM with a size in GB of the second parameter
REM It will then attach the disk to the local machine and apply the image from the WIM file specified
REM It will then detach the disk
REM If the optional YES 3rd parameter is set it will then add the VHD as a boot option to the local machine
REM Verify Parameters are set correctly
REM ===================================
if "%1" == "" (
    goto :Usage
if "%2" == "" (
    goto :Usage
if "%3" == "YES" (
    goto :Continue
if "%3" == "yes" (
    goto :Continue
if "%3" == "Yes" (
    goto :Continue
if "%3" == "" (
    goto :Continue
goto Usage
REM Variables Section
REM =================
set VHD=%1
set Size=%2
set WimFile=C:\VMs\BaseVM.wim
set ImageIndex=1
set TargetDrive=C:
set VHDFolder=\VMs
if exist %TargetDrive%%VHDFolder%\%1.vhd goto ErrorExist
REM Diskpart Section
REM ================
ECHO Creating VHD file %TargetDrive%%VHDFolder%\%1.vhd with a maximum size of %Size%GB
IF EXIST Diskpart-Create.txt DEL Diskpart-Create.txt
ECHO create vdisk file=%TargetDrive%%VHDFolder%\%1.vhd maximum=%size0 type=expandable > Diskpart-create.txt
ECHO select vdisk file=%TargetDrive%%VHDFolder%\%1.vhd>> Diskpart-create.txt
ECHO attach vdisk >> Diskpart-create.txt
ECHO create partition primary >> Diskpart-create.txt
ECHO format quick FS=NTFS label=OS >> Diskpart-create.txt
ECHO assign letter=v >> Diskpart-create.txt
ECHO active >> Diskpart-create.txt
ECHO exit >> Diskpart-create.txt
diskpart /s Diskpart-create.txt
REM Imagex Section
REM ==============
ECHO Applying image
imagex /apply %WimFile% %ImageIndex% V:
bcdboot V:\Windows /s V:

REM Detach VHD
REM ==========
ECHO Detaching Image
IF EXIST Diskpart-Detach.txt DEL Diskpart-Detach.txt
ECHO select vdisk file=%TargetDrive%%VHDFolder%\%1.vhd > Diskpart-Detach.txt
ECHO detach vdisk >> Diskpart-Detach.txt
diskpart /s Diskpart-Detach.txt

REM Add VHD to Boot Menu
REM ====================

if "%3" == "YES" (
    goto :Boot2VHD
if "%3" == "yes" (
    goto :Boot2VHD
if "%3" == "Yes" (
    goto :Boot2VHD
if "%3" == "" (
    goto :End
goto Usage

set VHDDESC="Boot from VHD"
@echo VHDFileName=%1
@echo Creating VHD boot entry for %VHDDESC%....
cmd /c " bcdedit /copy {current} /d %VHDDESC% " > VHDGUID.txt
FOR /F "tokens=7 delims=. " %%A in (VHDGUID.txt) DO set GUID=%%A
@echo VHD boot entry GUID = %GUID%
set BCDFName=%VHDFolder%\%1.vhd
@echo Configuring VHD boot entry....
@echo setting Device... vhd=[locate]%BCDFName%
bcdedit /set %GUID% device vhd=[locate]%BCDFName%
@echo setting OSDevice... vhd=[locate]%BCDFName%
bcdedit /set %GUID% osdevice vhd=[locate]%BCDFName%
bcdedit /set %GUID% detecthal on
bcdedit /default %GUID%
Goto End
ECHO The file %TargetDrive%%VHDFolder%\%1.vhd already exists. Please delete before recreating
goto End
ECHO Please specify both {Name} and {Size in GB} parameters
ECHO Optional 3rd parameter: Specify YES to add Boot2VHD feature on local machine
ECHO E.G. CreateVHD.cmd Win2008R2 100 YES
if exist Diskpart-create.txt del Diskpart-create.txt
if exist Diskpart-detach.txt del Diskpart-detach.txt
if exist VHDGUID.txt del VHDGUID.txt