Useful DateTime functions – LibraryTime.ps1

I was going to post a blog entries and realized that the example I wanted to give used some of my utility functions so I’ll share them first.  I hope you find them interesting.  I put them in a file called LibraryTime.ps1 and dot source that during my login.  BTW  – everyone should adopt that… Read more

The Wonders of Date Math using Windows PowerShell

Larry Hayden posted a query about dates at:http://techtasks.com/bookmarks/powershell/2006/09/interview-with-a-scripter-jeffrey-snover/ He has a script which gets all the Application Events that happened TODAY: ————————————————————$today = [DateTime]::Today$Events = Get-Eventlog -New 1024 Application | Where {$Today -le $_.TimeWritten}$Events |Group EntryType,Source |Format-Table Count,Name -Auto———————————————————— What Larry really wants is the Application Events that happend in the last 24 hours.  We’ll… Read more

Writing and Reading info from Serial Ports

PSMDTAG:FAQ: How do I read/write data from a Serial Port? Writing to a Serial Port PS> [System.IO.Ports.SerialPort]::getportnames()COM3PS> $port= new-Object System.IO.Ports.SerialPort COM3,9600,None,8,onePS> $port.open()PS> $port.WriteLine(“Hello world”)PS> $port.Close()Reading from a Serial Port PS> $port= new-Object System.IO.Ports.SerialPort COM3,9600,None,8,onePS> $port.Open()PS> $port.ReadLine() For information about how to read from a serial port in an asynchronous manner in PowerShell V2, use the DataReceived… Read more

Casting a scriptblock to an arbitrary delegate signature

Arild asked the following in http://arildf.spaces.msn.com/blog/cns!E99F8B43533149B0!140.entry?_c%3dBlogPart : PSMDTAG:FAQ: Delegates – how to cast a scriptblock to an arbitrary delegate signature? ANSWER – You can’t in V1.0. In version 1.0 Windows PowerShell only supports casting scriptblocks to delegates with the signature: void Method(Object Sender, object e) For instance, here is a WinForms script which uses a scriptblock delegate when a… Read more

Cascading Type Casts

A user on the newsgroup wondered whether the example below highlighted a bug or a feature.  They applied multiple type declarations on a single parameter and Windows PowerShell did not complain. function check {   param([string] [int] $y = 0, $x = 0)###^^^^^^^^^^^^  $x.GetType().ToString();  $y.GetType().ToString();  $x + $y;} That’s no bug, that’s a feature.  🙂… Read more

Type Shortcuts

PSMDTAG:FAQ: What is a TYPE SHORTCUT?PSMDTAG:FAQ: What are the TYPE SHORTCUTS?   Type shortcuts are a Windows PowerShell feature that allows you to reduce the amount of typing you have to do when specifying a type.    For example, the following 2 statements produce the same result: [System.Text.RegularExpressions.Regex]”^c.*” [Regex]”^c.*”   Jim Truher produced the following… Read more

Using Format Control Strings.

PSMDTAG:FAQ: How do I display large numbers with commas?PSMDTAG:FAQ: How do I use .NET formatting strings? As scripters, we often spend tons of times trying to get our data formated just the right way.  Often this is an error-prone, frustrating experience.  Windows PowerShell leverages .NET objects to make this task a ton simpler.  In .NET,… Read more

Exposing the Power of .NET in a Admin-friendly way

One of our primary goals for Monad was to:  “Expose the power of .NET in an Admin-friendly way”.  The challenges of systems administration are large and growing at the same time organizations are under ever more pressure to reduce costs and do more with less.  That was true 10 years ago, it is true now,… Read more

Base64 Encode/Decode a string

Today I came across http://scripts.readify.net/  . This sight is focused on Monad and MSH and is starting a collection of scripts at: http://scripts.readify.net/Scripts.aspx .  You should visit their site and let them know what type of scripts would be useful to you.  I particularly liked their entry on how to Base64 encode a file.  This is… Read more

Comparative Examples in MSH and KSH

  Most shells (such as Windows CMD.EXE and the UNIX shells SH, KSH, CSH, and BASH) operate by executing a command or utility in a new process, and presenting the results (or errors) to the user as text. Text-based processing is the way in which system interaction is done with these shells. Over the years,… Read more