Archive

Posts Tagged ‘powershell’

PowerShell 2.0 – Binary Runtime measuring

24/06/2011 Leave a comment

How to measure runtimes for applications not in the program itself?

Therefore the PowerShell is a nice utility under windows. Sometimes there are reasons why it’s not possible to measure the runtime of a specific algorithm or program in itself. Means not in the native code of the program. Or you just want to measure different programs automated. Get the job done by the Windows PowerShell!

To be concrete:

#!msh
function Pause ($Message="Press any key to continue...")
{
Write-Host -NoNewLine $Message
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Write-Host ""
}

function Benchmark($path)
{
write-host "Benchmark for "$path
$startTime = New-TimeSpan "01 January 1970 00:00:00" $(Get-Date)
$startTimeLong = [LONG] $startTime.TotalMilliseconds
write-host "Started @" $startTimeLong
Start-Process -wait $path
$endTime = New-TimeSpan "01 January 1970 00:00:00" $(Get-Date)
$endTimeLong = [LONG] $endTime.TotalMilliseconds
write-host "Ended @" $endTimeLong
$result = $endTimeLong - $startTimeLong
write-host "Time(Milliseconds):" $result
}

Benchmark("a.exe")
Benchmark("b.exe")
Benchmark("c.exe")
Benchmark("../pki3/d.exe")
Benchmark("../pki3/e.exe")
Pause

The function “Pause” I’ve taken from Windows Powershell Blog. The function “Benchmark” handles the measurments for the given program (program path).

For measuring the time while the program is running it first gets the date and saves it as a long (milliseconds since 01.01.1970) in $startTimeLong. Afterwards the external program is started, the parameter “-wait” lets the script pause until it terminates. After termination it the gets the date again and saves it as $endTimeLong. The result in milliseconds is the execution time.

Various usage of this function is thinkable.

Cheers Mavi

Categories: General Tags: , ,

The Shell with Power! PowerShell 2.0 [how to activate script execution]

23/06/2011 Leave a comment

Sick of *.bat – shell scripts? – Powershell (2.0)!

It’s not really something new, but I just did need it until now. So I’m surprised by the mighty of it. Additional it comes with a small IDE which makes developing and debugging very handy.

Before you enjoy the power of the shell you have to activate the script execution within your system. Therefore you have to set the execution policies:

Start the PowerShell as administrator

Get-ExecutionPolicy

Displays the current policy

Set-ExecutionPolicy RemoteSigned

This sets a policy. Valid values are:

  • Restricted -> Impossible to run any PS script (standart)
  • AllSigned -> Each script needs to be signed (check PS Blog for details). Possible choice but to unhandy for my taste.
  • RemoteSigned -> Each local script can run unsigned, scripts from the web need to be signed. My choice!
  • Unrestricted -> Each script runs! Way to insecure!!!

After changing the policy it’s possible to run your scripts. You can simply work with a text editor (eg. Notepad++) and save the script as *.ps1. If you’re doing so, you can start the script by double click in your file browser.

Even more comfortable is the PowerShell ISE. It comes with syntax highligthing, part execution and real debugging.

Finally I have an other scope: Using it as a calculator! Nice if you want to see your calculation and not just the final result. More…

Cheers Mavi

Follow

Get every new post delivered to your Inbox.