PowerShell: weird stuff when returning ArrayList

… unless you understand that a) ArrayList.Add() returns an integer, being the index of the item in the array, b) that goes onto the pipeline (unless you do something to eat it before it returns to the caller).

Here are the MSDN docs on ArrayList, where you can find the point about the Add method return value.

Here is a snippet where you can test the point in PowerShell

Advertisements

Pester: New-Fixture

While I might not always use it, when you’ve been away for a while and forgotten syntax, New-Fixture scaffolds up a PowerShell script (the script under test – SUT), and a PowerShell pester script to test the SUT. The wiki is very good and is here.

I don’t like the idea of having the test and production script in the same folder, but that is easy enough to adjust (see posts passim).

 

 

PowerShell: convert a column of data to a delimited string

Given a column of data (e.g. in Excel, Sheets), convert it to a delimited string (using PowerShell)
If I have data like this in a spreadsheet:

I want to create a single delimited string from that:

487,4443,576,224

This is one way:

Gist.


However, those were numbers/integers, so nice and easy.

If I now make one of them an alphanumeric…



… not so smooth this time:

Neither Sheets nor Excel will surround alphas with the necessary quotes. If we do that by hand…

, then this time no objections, and the quotes have been removed, which for my purposes is what I want anyway:

PowerShell: given an owner, find services

I’ll wrap this up another time, but this is the essence:

 

 

Gist.

You could in fact then use Out-GridView to display and filter, or just use the filter in the Grid, and remove the [where-Object] from the original query. However, that would be less manageable if you had a farm of VMs to query. But it’s there, if needed.

SqlServer/PowerShell: simple test of backup and restore

The SQLServer package has taken over from SQLPS. Basic backup and restore examples on my GitHub, and the data I used to create the tables (just create the databases by hand for now).

And the screenshots as I got the hang of it…

 

PowerShell: performance counters

Using Workflow…
(Gist)

And er not…
(Gist)

Pester: pretty summaries

Short examples of a mix of passing and failing Pester tests being rendered in a browser, using ReportUnit, available on NuGet, and ingesting NUnit output format. Check out the browser screenshots below first for the end result.

Code

# Runs all test suites, passes the output to the NUnit format, renders it, displays in a browser

# see my article here for installing nuget via Choco:

# https://dennisaa.wordpress.com/2017/04/26/powershell-updating-jpg-metadata/

# https://www.nuget.org/packages/ReportUnit/

nuget.exe install ReportUnit

#Then, for example...

# Execute all the tests

$outputName = Get-Random

$outputFile = "$PSScriptRoot/$outputName.xml"

$htmlFile = "$PSScriptRoot/$outputName.html"

Invoke-Pester -PassThru -Strict -OutputFile $outputFile -OutputFormat NUnitXml

.\ReportUnit.1.2.1\tools\ReportUnit.exe $outputFile

Start-Process chrome $htmlFile