PowerShell for Windows Admins


January 7, 2013  4:41 PM

Select-String scenarios – fixed columns

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

 

I had some questions come in after mu recent post regarding select-string. I’ll answer them as a series of posts. First off:

I’m recursively searching thru many files, and want to pull out specific data in ‘fixed column’ positions from the line(s) that match the phrase I’m seeking, i.e. position 10 thru 15 of the line or  position 6 thru the end of the line (which might be unknown).
What is your preferred method for handling this situation?

I started by creating a file

12345ABCD123451234512345
1234512345ABCD1234512345
12345ABCD123451234512345
12345abcd123451234512345
123451234512345ABCD12345
12345ABCD123451234512345
123451234512345ABCD12345
12345123451234512345ABCD
1234512345ABCD1234512345

I want to pick out the string ABCD but ONLY when its in columns6-9.  A quick inspection shows I should get four lines returned.

If you go with a simple match you get all lines returned

PS> Select-String -Path c:\test\*.txt -Pattern “ABCD” -SimpleMatch

C:\test\fxedcol.txt:1:12345ABCD123451234512345
C:\test\fxedcol.txt:2:1234512345ABCD1234512345
C:\test\fxedcol.txt:3:12345ABCD123451234512345
C:\test\fxedcol.txt:4:12345abcd123451234512345
C:\test\fxedcol.txt:5:123451234512345ABCD12345
C:\test\fxedcol.txt:6:12345ABCD123451234512345
C:\test\fxedcol.txt:7:123451234512345ABCD12345
C:\test\fxedcol.txt:8:12345123451234512345ABCD
C:\test\fxedcol.txt:9:1234512345ABCD1234512345

Notice the match is case INSENSITIVE

This means we get into the world of regular expressions – joy!

This will work

Select-String -Path c:\test\*.txt -Pattern “\A.{5}ABCD”

The regular expression means match any 5 characters followed by ABCD starting at the beginning of the string.

Alternatively you could use

Select-String -Path c:\test\*.txt -Pattern “\A\w{5}ABCD”

This is the same except its accepting any word character (letter, digit, math symbol and punctuation)

These two searches are case INSENSITIVE

if you need case sensitivity then compare

Select-String -Path c:\test\*.txt -Pattern “\A\w{5}ABCD”  -CaseSensitive
Select-String -Path c:\test\*.txt -Pattern “\A\w{5}abcd”  -CaseSensitive

or

Select-String -Path c:\test\*.txt -Pattern “\A.{5}ABCD” -CaseSensitive
Select-String -Path c:\test\*.txt -Pattern “\A.{5}abcd” -CaseSensitive

January 5, 2013  7:10 AM

Select-String confusion

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I have seen a lot of confusion recently over the use of Select-String.

One mis-conception is that you need to use Get-Content to pipe the file contents into Select-String.  Not so. Select-String will read the file for you.

If you just want to scan the files in a single folder to find a specific string then Select-String can do the work for you

Select-String -Path C:\Test\*.txt -Pattern "trial" –SimpleMatch

If you need to work through a folder structure add get-ChildItem to the pipeline

Get-ChildItem -Path C:\Test -Filter *.txt -Recurse |
Select-String -Pattern "trial" –SimpleMatch

One line of PowerShell gives you a very powerful way of filtering the files recursively and testing their contents for a given string


January 5, 2013  6:20 AM

Number of processors in a box

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

WMI enables you find the number of processors in your system:

PS> Get-WmiObject -Class Win32_ComputerSystem | fl Number*

NumberOfLogicalProcessors : 2
NumberOfProcessors        : 1

This works fine for Windows Vista/Windows 2008 and above.

Earlier versions of Windows mis-report the number of processors – it counts the number of logical processors reports it as the number of physical processors.

Win32_Processor has the same problem on Windows 2003 and below.

There is a hotfix available from http://support.microsoft.com/kb/932370 that will correct the behaviour of these two WMI classes so that they report correctly


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.


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: