PowerShell for Windows Admins


January 4, 2013  11:59 AM

Finding the domain controller that authenticated you

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

A question on my blog asked how do you know which domain controller you are running against when you search Active Directory. Unless you explicitly instruct your script to use a specific domain controller it will use the one to which you authenticated.

You can find the DC to which you authenticated with this simple function

function get-logonserver{
$env:LOGONSERVER -replace "\\", ""
}

January 3, 2013  1:11 PM

Displaying data from multiple servers as HTML

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

A forum question regarding retrieving WMI based data from multiple servers and displaying it as HTML was interesting.  I would approach it like this

$servers = Get-Content -Path C:\scripts\servers.txt            
$data = @()            
foreach ($server in $servers){            
 $compdata = New-Object -TypeName PSObject -Property @{            
  Computer = $server            
  Contactable = $false            
  LastBootTime = ""            
  AllowTSConnections = $false            
 }            
            
 if (Test-Connection -ComputerName $server -Quiet -Count 1){            
   $compdata.Contactable = $true            
               
   $os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $server            
   $compdata.LastBootTime = $os.ConvertToDateTime($os.LastBootUpTime)            
            
   $ts = Get-WmiObject -Namespace root\cimv2\terminalservices -Class Win32_TerminalServiceSetting -ComputerName $server -Authentication PacketPrivacy            
   if ($ts.AllowTSConnections -eq 1){            
    $compdata.AllowTSConnections = $true            
   }            
 }            
             
 $data += $compdata            
}            
$data            
$data | ConvertTo-Html | Out-File -FilePath c:\scripts\report.html            
Invoke-Item -Path c:\scripts\report.html

Put the list of servers in a text file & read it in via get-content.

use foreach to iterate over the list of servers.

For each server create an object and then test if you can ping the server. Note that the default setting for Contactable is $false so don’t need to deal with that case.

Get the WMI data and set the properties on the object.

Add the object to an array

After you’ve hit all the servers use ConvertTo-Html and write to a file with out-file.

use Invoke-Item to view the report


January 3, 2013  12:48 PM

Ensuring that parameter values are passed to your function

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

A question on the forum about a function had me thinking. The user had defined two parameters for the function and then used Read-Host to get the values.

NO

Much better way is to use an advanced function and make the parameters mandatory

function Getuserdetails {            
[CmdletBinding()]            
param (            
[parameter(Mandatory=$true)]            
[string]$Givenname,             
            
[parameter(Mandatory=$true)]            
[string]$Surname            
)             
            
Get-ADUser -properties telephonenumber,office -Filter {(GivenName -eq $Givenname) -and (Surname -eq $Surname)}             
            
}

If you call the function and don’t give values for the parameters you will be prompted for them

The other point is the –Filter property on get-aduser.  Don’t put quotes round the variable


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


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: