Removing Null Values

How many times have you gotten a WMI-Object and had to scroll through screens of null values?  Here’s a quick way to generate a new PSCustomObject that has those values removed.  Note that this is NOT the same object type as the input object, and it does not have any of the methods the original object has.  It…

0

Another Way to Create NoteProperty Entries

We’ve seen the Select-Object synthetic property method and the Add-Member -name AddNoteProperty method, but here’s probably the most straightforward way to output object data. $object = $true | Select ComputerName, Path, ContentCount;$object.ComputerName =$env:COMPUTERNAME.ToLower();$object.Path =Get-Location;$object.ContentCount = (Get-ChildItem).Count;$object; This works because the first line creates an object with the NoteProperty properties we need.  When we Select-Object on…

0

Securely Storing a Password

Here’s the scenario: You have a script that needs to access a static password which has to be stored in a file, but you do not want the password readable.  Here is a way to encrypt the password so that only you can read it.  Anyone else running the same code from any other account won’t…

5

Why Semi-Colons?

Sorry, no code here.  Just a quick thought. Q: Why should I put semi-colons at the end of lines?  PowerShell is smart enough to parse EOL as command terminators. A: Because some blogs are stupid and strip linefeeds.  That means you get a single line of PowerShell code when you copy-paste something that onscreen is a…

0

Laziness and XML: New-XmlElement

I love structured data.  I love how orderly it is, how logical.  However, I do not like the repetition.  Get the OwnerDoc.  Create an Element.  Loop over a bunch of AddAttribute calls, AppendChild it someplace.  90% of the time, this is how I build my XML structure.  Why not have PowerShell do it? function New-XmlElement…

0

Out-Error, the User-friendlier Write-Error

Write-Error is a pretty daunting cmdlet.  It dumps some pretty useful information to screen.  Useful, that is, for the scripter.  For the non-PowerShell-guru end user, it can be pretty intimidating.    Write-Host -ForegroundColor Red might be a better option, but it doesn’t populate the $Error variable.  Lastly, and this is just my preference, I’d like…

0

One-Liner: Dotting in vs. Running a Script

PowerShell scripts are a collection of functions and procedural code, right?  So often, I find myself wishing I had a function defined in a script in my interactive session, but the script doesn’t lend itself to being dotted into the environment. if ($MyInvocation.invocationName -eq ‘.’) { break __outOfScript; } Of course, this means you have…

0

Getting OCS 2007 Certs through WMI

 We recently had an interesting adventure where we needed to do an audit of the certificates used by various OCS servers.  In this case, scanning the computer’s cert store wasn’t sufficient – we needed to see which cert was bound to what interface.  The OCS Snap-In under compmgmt.msc provides this information, but GUIs don’t scale, and…

0

Creating a Multi-Host Cert MMC

MMCs are a mixed blessing at best.  The saved .msc files aren’t human-editable (read: not XML) so I don’t have an easy way to create them.  In this case, I need to perform a cert audit on all the machine certs in a lab.  PowerShell eschews ‘parsing by prayer’, but this is pretty much what…

0

Dumping ILO (and Other DHCP Addresses)

Here’s a quick-n-dirty script that dumps ILO address from the DHCP server. function Get-Ilo {     param ( [string]$computer = $env:COMPUTERNAME );     $scope = (netsh.exe dhcp server \\$computer show scope | Select-String ‘ilo’) -replace “\s+-.*” -replace “\s”;    netsh.exe dhcp server \\$computer scope $scope show clients 1 | Select-String “^\d” | % {        $data…

0