Advanced Windows PowerShell Scripting Video Training

Monday, December 31, 2012

2012 has now come to an end. I want to thank all of my clients for allowing me to represent you to your clients. Below is my report card for the year. The blue line is my average score while the orange line represents the average of all instructors. I think this says it all. Please contact me early to reserve your 2013 dates.
Happy New Year!

Friday, December 21, 2012

I am pleased to announce that I am working with Jakub Jareš as the moderator for the Active Directory forums on PowerShell.com. Please feel free to stop by and ask us your Active Directory, PowerShell related questions.

Wednesday, December 19, 2012

On PowerShell.com, I answered a question from a forum member who was receiving a CSV with the list of user names to be added to a group. If the CSV file had a blank line at the end of it, it would cause problems. Below is my code to read in a CSV file and filter out any blank lines before handing the object to PowerShell for processing.

We first import the CSV file into the PowerShell pipeline. Next we pass the object to a ForEach-Object statement. Each object is tested to see if the name property is empty. If it is, then nothing is done. If it contains data, then the user is added to the group. I could not use Where-Object {$_.Name –ne “”} as my filter because Add-ADGroupMember does not accept input from the PowerShell pipeline for its Member parameter.

Monday, December 17, 2012

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix. This customization deals with Group Policy and PowerShell. For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material. I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO. This is a feature that many Group Policy Administrators may not be aware of.

This past summer I attended a presentation at TechEd on Group Policy. One organization in the crowd had over 5,000 Group Policies. In an environment like that, the comment section can be priceless. I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago.

In the Group Policy module for PowerShell V3, there is a New-GPO and a Get-GPO cmdlet, but there is not a Set-GPO cmdlet. Fortunately, the Get-GPO cmdlet has some "put" capabilities to it. Below I I used Get-GPO to grab one of my GPOs and I sent it to Format-List *

Notice the Description field. I did the same thing, but this time I piped it to Get-Member.

The Description field has a "set" flag on it. Next I went ahead, grabbed the object and set the description field.

Wednesday, December 12, 2012

This week in My Windows Server 2012 class, I had an interesting question pop up while we were discussing managed service accounts. The client knew they needed to switch off of their current service accounts because to many people knew the passwords. They knew managed service accounts were the way to go, but did not know how to address the issue of how to discover all the services that they were using the same accounts on across all of their servers. The client asked me if they could do it with PowerShell….absolutely!
The cmdlet below will allow you to pipe in a comma separated list of your server names and the cmdlet will return all the accounts being used by the services running on the servers in your environment. I am using the Test-Connection cmdlet in this code so make sure your servers are able to return pings.

Monday, December 10, 2012

The short answer is no.We do have the ability to import our PowerShell history from one session to another if we save that history to an XML file.We can then import that history into another session.The F7 and up arrow functionality in the PowerShell window is a feature of the window, not PowerShell.Here is an example.

1. Open PowerShell and execute a couple of simple cmdlets.

2. Type Get-History and press Enter.

You can save this information to an xml file for import into another session

Get-History | Export-Clixml History.xml

Either open another PowerShell session or type Clear-History to clear your current session’s history.

Now import your history back in by:

If you press F7, you will not get these items in the history window.You can still use them.Let’s execute the item in ID 312.

Wednesday, December 5, 2012

This was a bit of a different problem. How to find if a property in a custom object has a NULL value. The client had a collection of objects that they gathered from their environment with PowerShell. They needed a way to find out if any of the NoteProperties had a NULL value. I created the function below. You send it the collection of objects you are interested in probing for a NULL property value and it will return the index number of the object in the collection that has one.

This works with objects as well as numbers. The first command creates a collection with 2 duplicates of both 1 and 2. The second command creates another collection with the duplicates filtered out. The Compare-Object cmdlet will first find items that are different between the two sets. By asking for the InputObject property, you will get those objects placed in the pipeline. Finally using the –Unique parameter of Select-Object, you filter any repeat objects. Without this final step, you will get

Wednesday, November 28, 2012

The question that I took this evening involved a user who has some functional PowerShell code. The needed a way to filter out any duplicate objects. They were looking at using several loops. In my college days, that would have been the answer. As my favorite professor, Dan Matthews, put it, “Never Reinvent the Wheel”

PowerShell has built in functionality to remove duplicate items by using the Sort-Object cmdlet. You need to use the –Property parameter to tell PowerShell which proper of the object you are looking for duplicates on and also you need to use the –Unique parameter to tell PowerShell to only leave unique objects (remove duplicates).

Below is some sample code to generate a set of 11 objects. Two of those objects will have a duplicate value in Prop1.

# Create a dynamic array to hold the test objects.

$Array= @()

# Create 10 objects in the dynamic array.

For ($X=0;$X-lt10;$X++)

{

$Obj=New-ObjectPSObject

$Obj|Add-Member-MemberTypeNoteProperty-Name"Prop1"-Value$X

$Obj|Add-Member-MemberTypeNoteProperty-Name"Prop2"-Value$True

$Obj|Add-Member-MemberTypeNoteProperty-Name"Prop3"-Value$False

# Add the object to the array

$Array+=$Obj

}

# Add a duplicate to the array.The duplication will be on the property