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"

No comments:

Post a Comment