PowerShell for Windows Admins


June 27, 2013  1:58 PM

Automation tools?

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

In this post http://richardspowershellblog.wordpress.com/2013/06/23/opinionautomate-or-suffer/ I talked about the need for admins to learn to automate. A couple of comments brought up the need for tools to create our automation scripts.

I remember the 4th generation languages of the late 1980s & 1990s. The promised that you wouldn’t have to code – just drag & drop onto the designer, answer a few questions and your application was created.

The worked great – as long as you wanted a cookie cutter application that only had limited functionality and was dog slow.

Automating code generation, in my experience, is a hard task. Compare the PowerShell snippets that come out of AD Admin center or the Exchange Management Console with what you actually write. They are usually very verbose and include lots of stuff you don’t need.

If someone can create a tool that creates my code for me, with minimal input on my side then I’ll sign up for it. I suspect it’ll be a long time coming. Until then happy coding.

June 27, 2013  1:46 PM

Windows Management Framework 4.0

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The PowerShell 4.0 preview is now available. It comes as part of WMF 4.0

Windows Management Framework 4.0 Preview includes updates to Windows PowerShell, Windows PowerShell ISE, Windows PowerShell Web Services (Management OData IIS Extension), Windows Remote Management (WinRM), Windows Management Infrastructure (WMI), the Server Manager WMI provider, and a new feature for 4.0, Windows PowerShell Desired State Configuration (DSC).

Download is available at

http://www.microsoft.com/en-us/download/details.aspx?id=39347

Note: It only installs on these versions of Windows

- Windows 7 with Service Pack 1
- Windows Server 2008 R2 with Service Pack 1
- Windows Server 2012
Windows 8 is not an option


June 26, 2013  4:09 PM

Dropping a database

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

A question came up on the forum regarding dropping a database & I realised it was something I hadn’t done before.

SMO provides a set of classes for managing SQL Server. You get SMO when you install the SQL Server management tools

Import the module to load SMO assemblies

import-module sqlps

get the server object and view the databases
$server = New-Object Microsoft.SqlServer.Management.Smo.Server(“w12scorc”)
$server.Databases

view the target database
$server.databases["mydb"]

drop the database and view the databases again
$server.databases["mydb"].Drop()
$server.databases


June 26, 2013  2:06 PM

RDP annoyance

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I’ve RDPing into a number of servers from different systems recently and the way the screen resolution changes to match your monitor size is annoying. If I use the shortcut to the session I get whatever sizes were set in the last session so often logon, curse when the size isn’t convenient and logoff and reset.

maybe I should just buy a giant monitor?


June 26, 2013  1:59 PM

Windows Server 2012 R2

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The Windows Server 2012 R2 preview is available on MSDN subscriber downloads. It has PowerShell v4!!!!


June 26, 2013  1:55 PM

Follow up on user group

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I did a test tonight and the sound is definitely better over Lync. The sound wasn’t perfect but was much better than Live Meeting. Actually, seemed usable.

The system seems a lot more responsive in terms of client screen refresh. When showing slides the client refreshes very quickly. Showing code seemed better – not as quick bet definitely better than Live Meeting. I’ll try a new microphone and see if that improves things


June 25, 2013  1:43 PM

UK PowerShell group

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

As you are probably aware the UK PowerShell group has been very quiet over the last few months. This has been due to my personal circumstances – hopefully now resolved.

I want to re-start the group and was wondering what people wanted.

When I started the group 6 years ago we were the only PowerShell user group in the world! Things have changed. Back then I could easily organise physical meetings where the personal contact and ability to talk to people who were also learning PowerShell was invaluable. Its not possible to get the venues these days so I switched to using Live Meeting. Not as satisfying but much easier from a logistics stand point. There are problems with sound quality over Live Meeting. I’ve tried it on a number of networks from a number of machines and the sound is poor at best and impossible to listen to at worst.

The big problem for any user group is finding speakers. There aren’t that many people willing to speak on any particular topic and many of them aren’t based in the UK.

At the moment I’m thinking of a web cast every other month. I can’t do every month – I just don’t have the time. If I can find other speakers to fill the gaps that will be great but I’m working on worst case. Six sessions a year I can manage.

This supposes that I can resolve the sound problems. I’m experimenting with Lync and hope that will solve the problem. if it doesn’t then a total rethink is required.

So the question to the PowerShell community – what do you want?


June 25, 2013  12:46 PM

The inverse association

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

In my last post I showed how to find the groups of which a local user is a member. A comment was left asking about the inverse relationship.

In this case we can just turn the code round.

Get-CimInstance -ClassName Win32_Group |
foreach {
$users = Get-CimAssociatedInstance -InputObject $psitem `
-ResultClassName Win32_UserAccount |
select -ExpandProperty Caption

$psitem | Add-Member -MemberType NoteProperty -Name ‘Users’ `
-Value ($users -join “;”) -PassThru

} | select Name, Users

Note: You can’t do this as a matter of course with all WMI relationships. Some relationships are one way in that A has a link to B but B has no way to link back to A unless you can find a property in common.


June 24, 2013  3:23 PM

WMI association example

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

A question came up on the Powershell.org forum about finding the groups of which local accounts are members

You can get account data using Win32_UserAccount

Group information is held in Win32_Group. You can see the relationship between users and groups by dumping the Win32_GroupUser instances. You will see a load of entries like this

GroupComponent : Win32_Group (Name = “Administrators”, Domain = “RSLAPTOP01″)
PartComponent : Win32_UserAccount (Name = “Administrator”, Domain = “RSLAPTOP01″)
PSComputerName : CimClass : root/cimv2:Win32_GroupUser
CimInstanceProperties : {GroupComponent, PartComponent}
CimSystemProperties : Microsoft.Management.Infrastructure.CimSystemProperties

WMI classes have associations – in this case there is an association between the Win32_User and the Win32_Group classes. The Win32_GroupUser can be thought of as the linking class. What we need to do to is to go from the individual instances of Win32_User (the users) to the associated groups.

Something like this should do it

$data = Get-CimInstance -ClassName Win32_UserAccount -Filter “LocalAccount = $true” |
foreach {
$groups = Get-CimAssociatedInstance -InputObject $PSItem -ResultClassName Win32_Group | select -ExpandProperty Name
$PSItem | Add-Member -MemberType NoteProperty -Name “Groups” -Value ($groups -join “;”) -PassThru
}
$data | select Caption, Groups

WMI – a bit convoluted but it always gets there


June 24, 2013  12:04 PM

Touching files

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Unix has a command called touch that allows you to set the access time on a file. PowerShell doesn’t have a direct equivalent but it is very easy to perform the same task:

$date = (Get-Date).AddMonths(-2)
Get-ChildItem -Path C:\Teszzt2 -Filter f*.txt |
Set-ItemProperty -Name LastWriteTime -Value $date -PassThru |
Set-ItemProperty -Name LastAccessTime -Value $date -PassThru |
Set-ItemProperty -Name CreationTime -Value $date

Set the date you want. Get the files and pipe into Set-ItemProperty. The example shows LastWriteTime, LastAccessTime and CreationTime all being modified. Change the code to just change what you need.

You can see the results

Get-ChildItem -Path C:\Teszzt2 -Filter f*.txt |
select Name, LastAccessTime, LastWriteTime, CreationTime


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: