Tuesday, 21 May 2013

Scripting Games - Beginner Event 4


The Scripting Games has been discussed in the previous post. The games are now four events old.  Event 4 of the Beginners track posed the challenge below:

Dr. Scripto isn’t saying that he dislikes auditors, but they do seem to show up at the most inconvenient times—and with the oddest requests. So he’s tossing this particular auditor request over to you.

This auditor would like a report that shows 20 randomly selected (well, as random as you can get) users from Active Directory. For each user, the auditor wants to see their user name, their department and title, and the last time they logged on. You also need to show the date their password was last changed, and whether the account is disabled or locked out. So that’s seven pieces of information. You’re to put that information into an HTML-based report file, and the file must show the date and time that the report was generated. Please make sure that all of the dates are normal looking, human-readable dates and times.

Keep your command as concise as possible, although that doesn’t mean you’re not allowed to use full command and parameter names—that’s always okay to do! A domain admin will always run the command, and the resulting HTML file will be manually emailed to the requesting auditor.

My solution for the event was:

Import-module ActiveDirectory

Get-ADUser -Filter * -Properties Name,Department,Title,LastLogonDate,PasswordLastSet,LockedOut,Enabled | get-random -count 20 | select Name,Department,Title,PasswordLastSet,LastLogonDate,LockedOut,Enabled | ConvertTo-Html -PostContent "<hr>","Report Generated at : ", (get-date) | Set-Content "C:\Users Report.htm"

The first line imports the ActiveDirectory module with the assumption that the module is not pre-loaded. The ‘Properties’ switch in Get-ADUser switch specifies the user attributes to be retrieved that are not displayed by default; ex: Dpartment, Title, LastLogonDate.  ‘Get-Random’ command in the pipeline outputs random 20 users.  The required properties are then selected and the output is converted to HTML and saved on the local drive.

Helpful links:




Tuesday, 14 May 2013

Scripting Games - Beginner Event 3


The Scripting Games are a six week event that lets you showcase your scripting skills based on six challenges. You can either choose compete in the Advanced or the Beginner’s track. I have registered for the beginner’s event since it is my first year being part of the games. It is a great learning experience to see such diverse approaches on any challenge. If you haven’t registered yet, it’s not too late. Please follow the link below for more details:


The Games just concluded its third event. The beginner track event challenged the competitors to come up with a one liner for the below scenario:  

Dr. Scripto has been fielding a lot of calls from the Help Desk lately. They’ve been asking him to look up information about the local hard drives in various servers –mainly size and free space information. He doesn’t mind helping, but all the requests have been getting in the way of his naps. He’s asked you to write a one-liner command that can get the information for him – and he wants the output in an
HTML file.

















The Doctor says you don’t need to parameterize your command – it’s okay to write it to run against localhost, and he can just change that computer name as needed in the future. The resulting HTML does need to go into an HTML file on disk someplace, and he wants you to pay special attention to the following:


• The browser displays “Disk Free Space Report” in the page tab when viewing the report.
• “Local Fixed Disk Report” is in the H2 (“Heading 2”) HTML style.
• The report ends with an HTML horizontal rule and the date and time that the report was generated.
• The size and free space values are shown as gigabytes (GB) and megabytes (MB) respectively, each to two decimal places.

The command you write can assume that both WMI and CIM are available on the remote computers, and that all the necessary firewall rules and authentication have already been taken care of.


 The one-liner solution I came up for the challenge was:

Get-WmiObject -Class "win32_logicaldisk" -Computername "localhost"-Filter "Drivetype = 3" | Select-Object @{Name="Drive";Expression={$_.DeviceID}},@{Name="Size(GB)";Expression={"{0:N2}" -f ($_.Size/1GB)}},@{Name="FreeSpace(MB)";Expression={"{0:N2}" -f ($_.FreeSpace/1MB)}} | ConvertTo-Html -title "Drive Free Space Report" -precontent "<H2>Local Fixed Disk Report</H2>" -body $_  -postcontent "<hr>",(get-date) | Set-Content "C:\FreeDiskReport.htm"

The report generated using the above command:





Sunday, 12 May 2013

Working With Multi-Valued Attributes - Part 2

Part 1 of the series dealt with 'set' cmdlets with respect to multi-valued attributes.

Now in part 2, we are going to look at the challenges faced when retrieving multi-valued attributes using 'Get' cmdlets and workarounds for them. If the number of the values stored in the attribute is high, then displaying the entire list on screen is not possible as powershell tends to append the list with “. . . “ implying that there are more values in the property than displayed on screen.

Get-DistributionGroup "Test DL" | fl Name, AcceptMessagesOnlyFrom 



Exporting the values to a CSV/ text file is also a concern, since the actual values in the attribute are not exported to the file as seen below.

Get-DistributionGroup "Test DL" | Select-Object Name, AcceptMessagesOnlyFrom | Export-Csv "C:\DL.csv" -Notype







The  pipeline can be manipulated to export all the attributes as shown below:

 Get-DistributionGroup "Test DL"  | Select-Object Name,@{Name="AcceptMessagesOnlyFrom";Expression={[string]::join(";",$_.AcceptMessagesOnlyFrom)}} | Export-Csv c:\DL.csv –NoTypeInformation


The output is shown below : 

The text in image above has been altered to confine within the image size to display all the values contained in the CSV file.