Monthly Archives: December 2012

UK PowerShell Group sessions for 2013

This is the list of proposed sessions for 2013. It is subject to change depending on circumstances.

All sessions are delivered by Live Meeting on Tuesdays at 7:30 UK time

29 January - PowerShell and Active Directory
26 February - PowerShell Advanced Functions
26 March - PowerShell cmdlets for Hyper-V
30 April - Notes from the PowerShell summit (may be changed)
21 May - Powershell Web Access
25 June - guest speaker PowerShell MVP Max Trinidad
30 July - Lessons from the Scripting Games
27 August - PowerShell eventing engine
24 September - CIM - cmdlets and sessions
29 October - PowerShell and XML
26 November - PowerShell type system - formatting and types files
17 December - PowerShell error handling

WMF compatibility

The Windows Management Framework 3.0 has been released as a Windows update.

However there are some compatibility issues as documented on the PowerShell team blog.  if you haven’t see the post it here

http://blogs.msdn.com/b/powershell/archive/2012/12/20/windows-management-framework-3-0-compatibility-update.aspx

Renaming a user

I was asked about searching a user name for a string and replacing it so that the object is renamed.

This is a three stage activity.  First get the user. Two modify the name. Three rename the object.  In active directory the name attribute has the LDAP name of cn but the Microsoft AD cmdlets treta it as name. So we end up with this code:

$user = Get-ADUser -Filter {cn -eq 'GREYIEN Bill'}             
$newname = $user.Name.Replace("YI","A")            
Rename-ADObject -Identity $user -NewName $newname -PassThru


The trick is in the middle line because the name is a string so you can use the standard string methods to perform the search and replacement.  Using –Passthru displays the object so you can see the change has taken place.

PowerShell Deep Dives

PowerShell Deep Dives is a book put together by the PowerShell community.  I’m editing one of the sections and have contributed some of the chapters. Manning have just started releasing it on their MEAP program.  The full book will hopefully be ready in the spring.

Best of all the royalties are being donated to worthwhile cause.

Check it out - http://manning.com/hicks/

Registry oddity

Looking at modifying the registry on a virtual machine while its offline. I mount the VHDX file and can run this  to load the registry

PS> reg load HKLM\VHDSYS h:\windows\system32\config\system
The operation completed successfully.

If I perform a reg unload at this point everything works but if I access the registry – for instance

PS> ls hklm:

and then try and unload I get an error!

PS> reg unload HKLM\VHDSYS
ERROR: Access is denied.

One thing I found is that there is as reference to the remote hive in the variable collection

PS> ls variable:

Name                           Value
----                           -----
$                              HKLM\VHDSYS

Empirically I’ve found that running these commands

ls env:
ls variable:

changes the value of the $ variable

You can then unload the hive

PS> reg unload HKLM\VHDSYS
The operation completed successfully.

PowerShell v3 installed modules

This is the list of installed modules in PowerShell v3 on Windows 8

AppLocker
Appx
BitLocker
BitsTransfer
BranchCache
CimCmdlets
DirectAccessClientComponents
Dism
DnsClient
International
iSCSI
ISE
Kds
Microsoft.PowerShell.Diagnostics
Microsoft.PowerShell.Host
Microsoft.PowerShell.Management
Microsoft.PowerShell.Security
Microsoft.PowerShell.Utility
Microsoft.WSMan.Management
MMAgent
MsDtc
NetAdapter
NetConnection
NetLbfo
NetQos
NetSecurity
NetSwitchTeam
NetTCPIP
NetworkConnectivityStatus
NetworkTransition
PKI
PrintManagement
PSDiagnostics
PSScheduledJob
PSWorkflow
PSWorkflowUtility
ScheduledTasks
SecureBoot
SmbShare
SmbWitness
Storage
TroubleshootingPack
TrustedPlatformModule
VpnClient
Wdac
WebAdministration
WindowsDeveloperLicense
WindowsErrorReporting

This is the corresponding list on PowerShell v3 installed on Windows 7

AppLocker
BitsTransfer
CimCmdlets
ISE
Microsoft.PowerShell.Diagnostics
Microsoft.PowerShell.Host
Microsoft.PowerShell.Management
Microsoft.PowerShell.Security
Microsoft.PowerShell.Utility
Microsoft.WSMan.Management
PSDiagnostics
PSScheduledJob
PSWorkflow
PSWorkflowUtility
TroubleshootingPack

As you can see there is quite a difference!

All of the Windows 8 modules that are highlighted in yellow are CDXML based.  They can’t be made available on Windows 7 because the underlying WMI classes aren’t available

Creating Virtual machines #1: Creating the VM

I’ve had two projects in mind for a while.  First I need to build a new WSUS server in my virtual environment & secondly I want to automate as much of the VM creation and configuration as possible. Oh – and I’m not using SC Virtual Machine Manager. 

I’ll be using the Hyper-V cmdlets that come with Windows 2012.

I covered some of this in PowerShell and WMI but that was for Windows 2008 R2 and I was using James O’Neills hyper-v functions. 

Does it get any easier with the cmdlets

This is what I came up with

function new-virtualmachine {            
[CmdletBinding()]            
param (            
 [parameter(Mandatory=$true)]            
 [string]$name,            
             
 [parameter(Mandatory=$true)]            
 [string]$path,            
 [Int64]$mem = 4GB,            
 [string]$vswitch = "Local Area Connection - Virtual Network",            
            
 [ValidateSet("Windows2012", "Windows2008R2", "Windows7")]            
 [string]$iso,            
            
 [switch]$startvm            
)            
            
Write-Verbose -Message "Testing VM Path"            
$vpath = Join-Path -Path $path -ChildPath $name            
if (-not (Test-Path -Path $vpath)){            
 New-Item -Path $path -Name $name -ItemType Directory            
}            
            
switch ($iso){            
 "Windows2012" {$isopath = "C:\Source\Windows 2012 RTM\en_windows_server_2012_x64_dvd_915478.iso"}            
 "Windows2008R2" {$isopath = "C:\Source\Window 2008R2\en_windows_server_2008_r2_standard_enterprise_datacenter_and_web_with_sp1_x64_dvd_617601.iso" }            
 "Windows7" {$isopath = "C:\Source\Windows7 RTM\en_windows_7_ultimate_x86_dvd_x15-65921.iso"}            
}            
            
New-VM -Name $name -MemoryStartupBytes $mem -Path $vpath -BootDevice CD -NewVHDPath "$vpath\$name.vhdx" -NewVHDSizeBytes 120GB -SwitchName $vswitch            
Add-VMDvdDrive -VMName $name -Path $isopath            
            
if ($startvm){Start-VM -Name $name}            
}


The new VM name and path are mandatory, I’ve set a default memory size of 4GB  and added a default Virtual switch. I’m adding the iso file containing the OS I want to install – that’s constrained by the set validation and I have a final parameter that allows me to start the VM.



if the path in which I want to create the VM doesn’t exist I create it.



A switch statement is used to set the full path to the iso file.



The New-VM cmdlet is used to create the VM based on the information provided.  I do hard code the fact that I’m setting the VM to boot from the CD and that the virtual disk will be 120GB



Add-VMDvdDrive is used to add the DVD drive to the VM (the controller location is automatically worked out) and the iso file mounted.



If the startvm switch is used then the VM starts and OS install commences. I’m deliberately NOT automating the OS setup at this time – that may be another project.

PowerShell Jobs session recording

The recording, slides and demo scripts from tonight’s session on PowerShell Jobs is available from

https://skydrive.live.com/?cid=43cfa46a74cf3e96#cid=43CFA46A74CF3E96&id=43CFA46A74CF3E96%2140429

Clearing AD values

A reader left a comment on this post http://msmvps.com/blogs/richardsiddaway/archive/2012/12/09/bulk-modifications-using-set-aduser.aspx

I was asked to show how to use the –Clear parameter.

Using the same CSV file this works:

$users = Import-Csv -Path C:\Scripts\adtest.csv                        
# Loop through CSV and update users if the exist in CVS file                        
                        
foreach ($user in $users) {                        
#Search in specified OU and Update existing attributes                        
 Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -Properties * -SearchBase "cn=Users,DC=manticore,DC=org" |                        
  Set-ADUser -Clear l, physicalDeliveryOfficeName, division                        
}


The difference with clear is that you have to use the proper LDAP attribute name – you can find this using ADSIEdit if you don’t know what it is. 

Keep taking the tablets

Tablets seem to be all the rage at the moment. I’ve been working with three different types over the last month or so and thought I’d share my observations. These are base on my personal circumstances and needs – yours may be quite different.

The 3 tablets are in order that I started using them:

  • ipad 3
  • Kindle Fire HD
  • Microsoft Surface

The ipad is currently the iconoclastic tablet and has had masses written about. At the moment its the least useful of the tablets. I’ll explain why in a minute.

The Kindle Fire HD is relatively new and is a heavily customised versions of Android. Its a 7 inch device with a very nice email client that can access multiple accounts and a reasonably fast browser. Its size makes it small enough to use one handed as an e-reader. The other two being 1o inch tablets are really meant for two hands and are not as comfortable to use as e-readers. Being bigger they are more susceptible to glare on the screen. The smaller Kindle screen is easier to angle for glare avoidance.

You can synchronise files using the USB cable.

A simple Kindle is the best option if all you want is an e-reader – its small enough to slip in a pocket. Ideal for travelling. If you want a bit more the Fire is not much bigger and not that much more expensive.

The Surface is the youngest of the tablets. It runs the RT version of Windows 8 plus full versions of the Office products – Word, Excel & PowerPoint. Windows 8 enables the synchronisation of settings across multiple machines – I have a laptop, netbook and tablet running Windows 8 and a number of settings including IE favourites automatically sync. It also looks like wireless network settings sync!

The Surface cover folds down to give a keyboard. Its not something that I would want to use all day every day but with a touchpad mouse its more than adequate – I recently prepared a 2000 word article on it with no problems. The flip out stand on the back of the Surface props the screen at a good angle for use.

A full size USB port is a huge bonus for the Surface. It gives me options for mobile working – SkyDrive or USB. Covers all eventualities.

The Surface also includes PowerShell v3.  Just the console not ISE. Its a constrained version of PowerShell but it there.

By contrast the ipad has a usable app that functions as a mobile white board. That’s all I’ve really found useful.

Much is made of the numbers of apps available in the various app stores but if you actually look at them you have to wonder – how many versions of Sudoko do you really need?

Between the Kindle Fire and the Surface I have my leisure and business mobile needs met. The ipad doesn’t bring anything new to the table – that I need and so doesn’t get used.

As I said at the beginning these are my observations based on my needs and experiences. Yours may well be different.