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

Wednesday, 4 January 2012

DFS Share Command Line

On my home server I've configured a DFS share to present one shared folder to include files from 2 shared folders on separate hard disks. The script needs to be run every time a new folder is added to either of the shared folders.

This script is below:

@echo off
REM Clean up section
dfsutil root remove "\\MEDIA\TV Shows"
net share "TV Shows" /DELETE
rd "E:\ServerFolders\TV Shows"
REM Query the folders and output to two temp text files
dir \\MEDIA\TV1 /ad /b> TempTV1.txt
dir \\MEDIA\TV2 /ad /b> TempTV2.txt

md "E:\ServerFolders\TV Shows"
net share "TV Shows"="E:\ServerFolders\TV Shows"
dfsutil root addstd "\\MEDIA\TV Shows"
for /F "delims=*(()(" %%1 IN (TempTV1.txt) DO dfsutil link add "\\MEDIA\TV Shows\%%1" "\\MEDIA\TV1\%%1" "%%1"
for /F "delims=*(()(" %%1 IN (TempTV2.txt) DO dfsutil link add "\\MEDIA\TV Shows\%%1" "\\MEDIA\TV2\%%1" "%%1"
del TempTV1.txt
del TempTV2.txt

Tuesday, 3 January 2012

What the hell is a private cloud?

This can be seen as a cynics journey into the cloud. Over the last 5 or so years everyone's talked about cloud computing with no one really having a clue. Now it seems that the cloud is starting to arrive and I figure it's time to find out what it is.
Step 1 : Learn what the hell is a cloud...

My first port of call is the Microsoft Virtual Academy. A free Microsoft resource set up as if it's an online college. Clearly very Microsoft focussed but that's good when you want to learn Microsoft products.

Track: Microsoft Private Cloud Infrastructure

Module 1: Microsoft Private Cloud Infrastructure - Overview
Link to video:

My review of the first one: Very informative video - had to download it via WMV file though as streaming it stopped in a couple of places and it refused to rebuffer. The accompanying PDF file supplied all the supporting text that the presenter read which is useful. The exam however failed miserably as question #5 did not have any answers listed which made it tricky to answer - I have had to wait for it to time out twice now to let me try and pass the module.

Still - despite this issue a very good start.

Module 2: Microsoft Private Cloud Infrastructure - Configuration
Link to video:

The second one continued in the same format with a very good level of information, I already know a fair bit about Windows clustering and Hyper-V so not too much in depth learning required. It's a good source to define what a private cloud is and how the components fit together.

Module 3: Microsoft Private Cloud Infrastructure - Management
Link to video:

The third one continued in the same style but focussed around management. Lots more information about best practices.

Overall this course was very useful in giving an overview of the Private Cloud. I'm itching now though to start to get to the nitty gritty details. High level information is useful to understand everything but I'm one of those people who needs to see it working.

So I now know what a Microsoft Private Cloud is and what components comprise it. At the minute it seems to be in a state of transition with the new 2012 generation of System Center products coming out.

Having done the first track I moved onto the second applicable one on the MVA website.

Track 2 : Planning, Building and Managing a Private Cloud

I think this was a useful track to do as well. The first module was very much aimed at promoting the cloud experience (which wasn't really what I was looking for). I accidentally switched my brain off a couple of time during this one. The presenter didn't really inspire me that much. The second module had Symon Perriman from the first track who I thought did a great job so that was ok. The second and third modules had a lot more screencasts which I find useful. Overall though most of the information was already provided in the first track but it certainly didn't hurt to get it again.

A quick summary

But basically a private cloud comprises a companies locally hosted hyper-v servers running guest machines. It is like a traditional virtualised environment with a suite of management tools wrapped around it. The core features are:

Windows Server 2008 R2 running Hyper-V
Active Directory, DNS, DHCP
System Center Virtual Machine Manager 2008 / 2012
System Center Operations Manager 2007 R2 / 2012
System Center Configuration Manager 2007 R3 / 2012
System Center Opalis / Orchestrator
System Center Service Manager 2010 / 2012
System Center DPM 2010 / 2012

You certainly get the impression from the videos that the 2012 products have been designed with Private Clouds in mind specifically the VMM 2012.

As a quick overview:

System Center Virtual Machine Manager (SCVMM) is used to control/manage/configure the virtual machines running on the Hyper-V hosts

System Center Operations Manager (SCOM) is used to provide software and hardware monitoring of the hosts and guests as well as providing troubleshooting information and PRO tips to SCVMM to control VM placement.

System Center Configuration Manager (SCCM) is used to provide software updates, software installation, license management, OS deployment, software/hardware inventory, reporting and script deployment.

System Center Opalis / Orchestrator is used as a work flow tool to create custom actions using a graphical user interface; this integrates well into the System Center Service Manager.

System Center Service Manager (SCSM) is used to provide helpdesk services as well as being able to respond to certain conditions. For instance if SCOM raises an alert into SCSM a rule can be configured to run an Opalis/Orchestrator work flow to go and resolve that issue.

System Center Data Protection Manager (SCDPM) is used to provide backup / restore services.

My next steps...

Well having gained an understanding of what a Private Cloud is I figure I now need to go and learn the additional components I don't know. Given I already know SCCM, SCVMM, Hyper-V and failover clustering I am planning on tackling SCOM next. I think I'm going to do SCOM 2007 R2 first and then 2012. I don't expect to get a thorough in depth knowledge but hopefully enough to be able to implement it in a private cloud, deploy agents, deploy management packs and install the PRO tips for SCVMM.