Matt Simmons posted a question today on Twitter…
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…
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…
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…
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…
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…
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.
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…
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
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…
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…