Emil’s Blog

Programming Windows, .Net, EPiServer and whatnot…

[Powered by WordPress.]

September 30, 2009

Working with cultureinfo in PowerShell

by @ 13:17. Filed under .Net programming, PowerShell

As a simple example of working with culture information in PowerShell, here's how to list Swedish monthnames:

(new-object system.globalization.cultureinfo("sv-SE")).DateTimeFormat.MonthNames

The result is an array:

januari
februari
mars
april
maj
juni
juli
augusti
september
oktober
november
december

Try doing that in a BAT file... :-)

/Emil

September 29, 2009

Configuring a trace listener

by @ 14:54. Filed under .Net programming

Mostly for my own reference, here's an example of how to configure a trace listener that writes events to a text file:

<configuration>
  ...
  <system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <add name="TestTrace" type="System.Diagnostics.TextWriterTraceListener"
            initializeData="trace.txt" />
      </listeners>
    </trace>
   </system.diagnostics>
  ...
</configuration>

After that, all calls to System.Diagnostics.Trace.Write* functions are logged to the textfile. Example:

Trace.WriteLine("Something happened...");

September 14, 2009

Powershell script to kill long-running processes

by @ 10:02. Filed under PowerShell

Here's a small script to kill all Word or Excel-processes that have been running for more than 5 minutes. This can be useful on a server that launches those applications to generate documents. You probable shouldn't do it that way but if you do you're likely to discover that the processes don't always terminate as they should. In those situations, scripts like this come in handy.

$startTimeLimit = (get-date) - (new-timespan -minutes 5)
$processes_to_kill = get-process |
    where {$_.StartTime -lt $startTimeLimit -and ($_.path -like "*winword.exe" -or $_.path -like "*excel.exe") }
if ($processes_to_kill -ne $null)
{
    $processes_to_kill | foreach { $_.Kill() }
}

/Emil

[powered by WordPress.]

jour·nal n. A personal record of occurrences, experiences, and reflections kept on a regular basis; a diary.

Internal links:

Categories:

Search blog:

Archives:

September 2009
M T W T F S S
« Aug   Oct »
 123456
78910111213
14151617181920
21222324252627
282930  


View Emil Åström's profile on LinkedIn

General links:

I read:

Visitors

Recent Comments

Spam caught

Other:

Clicky Web Analytics

36 queries. 0.636 seconds