Scripting Games – what’s wrong with this

I noticed code like this in quite a few entries in for Event 1

Get-ChildItem -path C:\Application\log -Recurse -Filter *.log | Where-Object{$_.LastWriteTime -lt [DateTime]::Now.Subtract([TimeSpan]::FromDays(90))} | ForEach-Object {…}

From the title it should be obvious that there’s something I don’t like. 

The where-object re-calculates the date to test for EVERY object on the pipeline.

That’s not efficient.

Put the calculation outside your pipeline

$testdate = [DateTime]::Now.Subtract([TimeSpan]::FromDays(90))

or

$testdate = (get-date).AddDays(-90)

which I personally think is simpler

Your pipeline then becomes

Get-ChildItem -path C:\Application\log -Recurse -Filter *.log | Where-Object{$_.LastWriteTime –lt $testdate} | ForEach-Object {…}

Much simpler and more efficient.

I wonder if putting the calculation into the pipeline is part of the almost religious fervour surrounding the “one-liner”.  if you can sensibly put your code into 1 line – read one pipeline because that’s what we’re really doing – then do so. But don’t make it more inefficient as a consequence.

Posted in Powershell, Powershell Basics, Scripting Games | Leave a comment

AD Management in a Month of Lunches–new MEAP

Chapters 12 and 13 have been added to the Manning Early Access Program

Chapter 12 shows you how to manage your domain controllers

Chapter 13 teaches how to protect the data in your Active Directory

 

You can order the MEAP from www.manning.com/siddaway3

Posted in Active Directory, Books, PowerShell and Active Directory | Leave a comment

Creating DNS PTR records

When I was writing the DNS chapter of PowerShell in Practice I couldn’t get the CreateInstanceFromPropertyData  method on the MicrosoftDNS_PTRType  class to work. Revisiting DNS for AD management in a Month of lunches this time round I have access to the CIM cmdlets so can put the parameter names in.  This gives usage like this.  I’ve shown Invoke-WmiMethod and Invoke-CimMethod so you can see the parameter names:

Invoke-WmiMethod -Namespace root\MicrosoftDNS -Class MicrosoftDNS_PTRType `
-Name CreateInstanceFromPropertyData `
-ArgumentList “175.168.192.in-addr.arpa”, ‘server02′, ’55.175.168.192.in-addr.arpa’,
“ADMLServer02.admldns.test” 

Invoke-CimMethod -Namespace root\MicrosoftDNS -ClassName MicrosoftDNS_PTRType `
-MethodName CreateInstanceFromPropertyData `
-Arguments @{Containername = “175.168.192.in-addr.arpa”;
DnsServerName = ‘server02′; OwnerName = ’55.175.168.192.in-addr.arpa’;
PTRDomainName =”ADMLServer02.admldns.test”}

 

If you have access to Windows 2012 then you are better off using the cmdlet

Add-DnsServerResourceRecordPtr –Name ‘54’ `
–ZoneName “175.168.192.in-addr.arpa” `
–PtrDomainName  ‘ADMLServer01.admldns.test’  `
–ComputerName server02

 

Which ever method you use – you can easily create PTR records

Posted in DNS, PowerShell and WMI, PowerShell V2, PowerShell V3 | Leave a comment

Scripting Games-Voting

Still time to vote on event 4 – the numbers of votes are falling off slightly – especially in the Advanced events. This is your opportunity to observe what other people are doing, comment and very possibly learn.

Posted in Scripting Games | Leave a comment

PowerShell Deep Dive–MEAP now complete

The final chapters of PowerShell Deep Dive have been added to the MEAP


http://www.manning.com/hicks/

Enjoy

Posted in Books, PowerShell V3 | Leave a comment

AD Month of Lunches–Chapter 11 in MEAP

The MEAP for AD Management in a Month of Lunches has been extended with the inclusion of chapter 11 – Creating Domain Controllers.

www.manning.com/siddaway3

Enjoy

Posted in PowerShell and Active Directory, Active Directory, Books | Leave a comment

Scripting Games – Filter early again

Grading the scripts in Event 4 and the one thing that jumps out is the amount of unnecessary data being carried through the scripts

You were asked for 7 properties off 20 random users

Get-ADUser has a –properties parameter. USE it to restrict the properties you return. You don’t NEED all the other properties

Next select you 20 users as soon as possible

get-aduser | get-random

will do that.  You can then format just the few properties you need on the 20 objects you have left

FILTER EARLY

Posted in Powershell, PowerShell and Active Directory, Scripting Games | Leave a comment