PowerShell for Windows Admins


January 3, 2013  6:01 AM

PowerShell workflow articles

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I’ve written a series of articles on PowerShell workflows that are appearing on the Scripting Guy blog. The first two in the series have been published at:

http://blogs.technet.com/b/heyscriptingguy/archive/2012/12/26/powershell-workflows-the-basics.aspx

http://blogs.technet.com/b/heyscriptingguy/archive/2013/01/02/powershell-workflows-restrictions.aspx

 

Enjoy

December 20, 2012  11:16 AM

UK PowerShell Group sessions for 2013

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

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


December 20, 2012  10:20 AM

WMF compatibility

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

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


December 19, 2012  9:43 AM

Renaming a user

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

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.


December 18, 2012  1:28 PM

PowerShell Deep Dives

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

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/


December 14, 2012  8:26 AM

Registry oddity

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

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.


December 14, 2012  4:17 AM

PowerShell v3 installed modules

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

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


December 12, 2012  2:18 PM

Creating Virtual machines #1: Creating the VM

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

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.


December 11, 2012  3:03 PM

PowerShell Jobs session recording

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

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


December 11, 2012  11:14 AM

Clearing AD values

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

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. 


Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: