Archive

Archive for the ‘PowerShell’ Category

PowerShell: Active Directory, and User Groups

August 10th, 2010 No comments

Matt Simmons posted a question today on Twitter

Anyone know how to show a user's group membership in powershell?August 10, 2010 15:55 via HootSuite

The cool thing about PowerShell, like Perl, if it can be done one way, there are probably 10 other ways to achieve the same thing. Here are a couple… Read more…

Categories: PowerShell Tags:

PowerShell: Loops, ForEach, ForEach-Object, and control functions

July 13th, 2010 No comments

In my previous post about PowerShell and BITS, I stumbled on a weird quirk in the ForEach-Object function, which had me scratching my head for a bit.

Read more…

Categories: PowerShell Tags:

PowerShell and BITS

July 8th, 2010 4 comments

Ever had to download a bunch of files from a website, and didn’t want to have to write an HTTP handler for it? This comes up on a regular basis here. We get requests from customers that have transferred from one vendor to another, and want to import all their photos to us. Here is how I solve it.

Read more…

Categories: PowerShell Tags:

PowerShell; Calculated values in results

June 11th, 2010 2 comments

Whilst sitting here on a product release call, I was reading over my twitter feed, specifically looking at #powershell, and I saw a post by @tonjoh. Feeling I had a few minutes on my hands whilst QA bashed away at the servers, I decided to take a look…

Read more…

Categories: PowerShell Tags:

Cleaning up remote directories with PowerShell

May 24th, 2010 1 comment

Part of our application at work sends email notifications to various uses when certain criteria are met. This is great, except testing is difficult as you really don’t want to send out mails to valid customers, so we disable relaying outbound, and the local mail server just chomps on it a bit, then decides to not deliver it and drops it to the local file system. This is great, except in a the matter of days, we have 100k message files. This becomes entirely unmanageable with Windows, so PowerShell is here to help…

Read more…

PowerShell: Restart remote services

April 10th, 2010 No comments

In a follow up to @ScriptingGuys post about restarting services using VBScript, here is the same using PowerShell. I’ll probably be dragging the same onto some server monitoring stuff to get a service back up and running we’ve been having a weird issue with.

$names = Get-Content c:\temp\computers.txt
foreach($name in $names) {
    $svc = Get-WmiObject Win32_Service -ComputerName $name `
        -Filter "name='wuauserv'"
    if ($svc.started -eq $true) {
        $svc.StopService()
    }
    $svc.StartService()
}

This takes a file “computers.txt”, loops through the data, and uses WMI to connect to the remote machines to get the wuauserv service (or in plain English Windows Automatic Update). Easy as pie.

Categories: PowerShell Tags:

PowerShell: Top x Processes using CPU

April 6th, 2010 No comments

This is a quick and handy one for server monitoring, and tracking down that process that is using all your CPU. It makes use of WMI counters.

Read more…

PowerShell: Timing Commands

April 6th, 2010 2 comments

Curious about how long your script took to execute? How about just that cmdlet? Powershell has a built in function for you.

  • Open Powershell command prompt
  • Use the command Measure-Command like so:
Measure-Command {c:\scripts\yourscript.ps1}

Enjoy the output broken down by days, hours, minutes, seconds, milliseconds, and ticks.

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 34
Milliseconds      : 287
Ticks             : 342873445
TotalDays         : 0.000396844265046296
TotalHours        : 0.00952426236111111
TotalMinutes      : 0.571455741666667
TotalSeconds      : 34.2873445
TotalMilliseconds : 34287.3445

The same works for cmdlets too…

PS C:\scripts> Measure-Command {Get-Process}

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 6
Ticks             : 65919
TotalDays         : 7.62951388888889E-08
TotalHours        : 1.83108333333333E-06
TotalMinutes      : 0.000109865
TotalSeconds      : 0.0065919
TotalMilliseconds : 6.5919
Categories: General Ramblings, Microsoft, PowerShell Tags:

PowerShell: Cleaning up IIS Logs

April 1st, 2010 2 comments

This one is a quick and easy one. We have multiple IIS boxes, which each generate large usage logs on a daily basis. To save space, and for analytics off-server, we compress the log files. Having found Rar is one of the better compression algorithms, we simply shell out to rar, compress the logs, and cleanup older logs.

Read more…

PowerShell: Remote Registry, and fixing an Office 2007 install

March 29th, 2010 No comments

We’ve had a stubborn Windows 2003 server that we’ve been trying to get Office 2007 installed for a while. It has a couple of things wrong with it which have made the install substantially harder.

Read more…