Techmentor San Francisco 2008

I finished up my slide decks last week for the first Techmentor conference of the year in San Francisco (March 30 -April 3). If you’ve never been to a Techmentor conference you’re missing a great opportunity to hear and see your favorite IT speakers. Plus it’s a lot of fun to meet your peers, swap war stories and even some strategy and tips. I thought you might like to know what sessions I’m doing:

Automating Remote Systems Management with PowerShell v1.0 and WMI
Although PowerShell is an incredibly valuable administrative tool, a major feature is its support for Windows Management Instrumentation (WMI). In this session you’ll learn how to access remote system information, build management reports, monitor WMI events and more all with PowerShell. This will be a demo-rich session with many real-world and practical examples you can take home and use immediately.

Automating Exchange Server 2007 Management
Come see how easy it is to manage your Exchange 2007 servers from the PowerShell console. I’ll guide you through common Exchange management tasks including managing mailboxes, users, contacts, databases and more. Again, this will be a demo-heavy session with many real-world and practical examples.

Mr. Roboto’s Resource Kit
In this session I’ll open up my bag of tricks and tools for managing Windows servers and desktops. From mass password changes to event log management to security Mr. Roboto will show how to keep on top of your environment with free or low-cost alternatives. You’ll leave with plenty of tools you can begin using immediately to make your job easier and maybe even a little fun.

Logon Scripting Fundamentals
Do you have logon scripts for your users? Do you still need them? Do they do what you want them to do? What good are logon scripts anyway? You’ll learn the best way to leverage logon scripts and integrate them into Group Policy. You’ll also see how to create logon scripts with no scripting required. In fact, no scripting experience is required at all for this session.

Finally, if that’s not enough I’m also doing a half-day post-conference workshop. This is separate from the main conference.

Automating Active Directory Management with Windows PowerShell
I will teach you how to use PowerShell to manage Active Directory users, groups and computers. You’ll learn how to manage individual objects as well as bulk creation and management. You’ll learn how easy it is to import a CSV file with 1000 new users and create fully populated accounts in a matter seconds. We’ll explore the ADSI type adapters, the Active Directory PSDrive provider from the PowerShell Community Extensions and the free Quest Active Directory Management Console. This will be a demo-heavy session with many real-world and practical examples you can take home and use immediately. Much of the content will be live demonstrations of material from my forthcoming book, Managing Directory Services with Windows PowerShell. You may bring a laptop to follow along, but this is NOT instructor-led training with hands-on labs. If you come with a laptop, it should have a virtual machine running a Windows 2003/2008 domain controller. The virtual machine should be running PowerShell v1.0 and the current versions of the PowerShell Community Extensions and the Quest Active Directory Management console.

And this is just what I’m doing. Check out the full schedule and register today. You won’t be disappointed. I often hear at conferences from attendees that they often learn something or pickup some tip that more than justifies the conference expense. Something that solves a prickly problem and they leave happy and eager. I hope to see you there. If you haven’t already registered for TechMentor San Francisco, you can receive a $695 discount on the Gold Passport if you register using priority code TPHIC.

Technorati Tags: , , ADSI, Techmentor, , , , ,

PowerShell Process Uptime

Not too long ago, I wrote an MCPMag Tip Sheet column on using the pipeline in PowerShell. I showed how you could get the start time of a specified service:

$svcname=Read-Host “Enter a service name” ; get-process | where {$_.id -eq (get-wmiobject win32_service | where {$_.name -eq $svcname}).ProcessID} | select -property StartTime

We can take this a step further and calculate the Uptime. Let’s look at a quick one-line example using a process:

(get-date).Subtract((Get-Process firefox).starttime)

This expression subtracts the start time property of the Firefox process from the current date and time. When executed, you should get output like this:

Days : 1
Hours : 4
Minutes : 20
Seconds : 29
Milliseconds : 506
Ticks : 1020295067859
TotalDays : 1.18089706928125
TotalHours : 28.34152966275
TotalMinutes : 1700.491779765
TotalSeconds : 102029.5067859
TotalMilliseconds : 102029506.7859

If I wanted nicer output from my one-liner I can clean it up like this:

$u=(get-date).Subtract((Get-Process firefox).starttime);Write-Host $u.Days day $u.hours hours $u.minutes minutes and $u.seconds seconds

This is one single line expression.

Since all services are associated with a process, we can find the uptime of the process and thus the service. Except for some shared services that use svchost, this should be pretty accurate. I’ve split this back up into separate expressions to make it easier to follow:

$svcname=Read-Host “Enter a service name”
$p=Get-Process | where {$_.id -eq (Get-WmiObject win32_service | where {$_.name -eq $svcname}).ProcessID} | select -property StartTime
write-host $svcname service
$u=(get-date).Subtract($p.StartTime)
Write-Host $u.Days day $u.hours hours $u.minutes minutes and $u.seconds seconds

Not too shabby. The process is a little tedious because I have to query each service via WMI to get it’s associated process. To wrap this up, let’s go whole hog and report on the uptime for each running service. You’ll really need to put this in a script.

Function Get-ProcessUptime {
param([string]$svcname)
$p=Get-Process | where {$_.id -eq (Get-WmiObject win32_service | where {$_.name -eq $svcname}).ProcessID} | select -property StartTime
$u=(get-date).Subtract($p.StartTime)
Write-Host $svcname service `t $u.Days day $u.hours hours $u.minutes minutes and $u.seconds seconds
}

foreach ($svc in (get-service | where {$_.status -eq “running”})) {Get-ProcessUptime $svc.name}

I’ve put the uptime code in a function that takes a service name as a parameter. I then get a list of running services and pass each one to the function. There’s probably a more efficient way to do this, but it doesn’t jump out at me right now.

Bonus:

Get uptime for all processes:

foreach ($proc in (Get-Process | where {$_.name -ne “Idle” -AND $_.name -ne “System”})) {
$u=(get-date).Subtract($proc.starttime);
Write-Host $proc.name `t $u.Days day $u.hours hours $u.minutes minutes and $u.seconds seconds
}

Technorati tags: , , , MCPMag, ,

More Fun with Get-Content

A few followups on my recent post about Get-Content. First, you can also use the CAT alias if you’re used to the Unix world, or TYPE in place of get-content. The Get-Content cmdlet does have a parameter called -totalcount that will return the specified number of lines from the beginning of the file:

cat c:\boot.ini -totalcount 3

Unfortunately, you can’t specify a range or start at the end of the file. However, there is a way to simplify the one-liner I used. You can use the built in common parameters and specify a variable with Get-Content. This command will get the last five lines of the file log.txt:

cat log.txt -outvariable c >$null ; $c[($c.count-5)..($c.count)]

I’m redirecting the output of the first half of the expression to $null otherwise I’d see the entire file in addition to the last five lines.

That is much more efficient. Now I don’t have to get the count or length ahead of time. And if you wanted to wrap this up in a function, it is pretty simple:

Function GetTail {
param([string]$file,[int]$lines)
cat $file -outvariable c >$null ; $c[($c.count-$lines)..($c.count)]

}

To use:

GetTail log.txt 5

Will return the last five lines of log.txt.

Technorati tags: , , GetContent, ,

PowerShell Get Content

One slick trick you can do in PowerShell with text files that’s difficult to accomplish without extra tools is to display sections of text files without showing the entire file. When you use the Get-Content cmdlet, the resulting content is treated as an array of strings. This means you can simply specify the line you want displayed like this:

(get-content c:\boot.ini)[0]

Don’t forget that arrays start counting at 0. If you want to see a range of lines, simply specify the range:

(get-content c:\boot.ini)[0..5]

This will display the first 6 lines of c:\boot.ini. Want to look at the last X number of lines? Well you first need to get the number of lines.

(get-content c:\boot.ini).Length

Once you know the length, specify whatever range you want. My boot.ini is 7 lines long so if I wanted to see the last 3 lines I would simply run

(get-content c:\boot.ini)[5..7]

Of course, this little tidbit wouldn’t be complete without a one-liner to get the last 5 lines without knowing in advance how long the file is:

$c=get-content c:\boot.ini;$l=$c.length;$c[($l-5)..$l]

If you specify a number greater than the value of $l then the entire file will be returned.

Obviously the boot.ini file is hardly an exciting thing to test so try this out on log files on your systems.

These commands are very similar to the Head and Tail utilities and in fact you could easily turn these into your own Head and Tail functions.

Technorati tags: , , GetContent,

Powershell Help Examples

It may seem like a little thing, but the Get-Help cmdlet in PowerShell has some features you may not be aware of. I’m always using the -full parameter to see all the help information, primarily so I can see the examples. Well Get-Help has a -examples switch which won’t display syntax help, but it will show you all the examples from the help file. Try these commands to see for yourself

help get-wmiobject -examples
help get-process -examples

Technorati Tags: