Import-Module and the “-Prefix” parameter

Lesson learned from the presentation “How to Position Yourself for the Future” with Jeffrey Snover and Jason Helmick (from the Q&A session at 1:01:17): you can add a prefix to the commands of a module when you import it!

In the Q&A session a guy from audience mentions that a PowerShell module (presumably PowerCLI from VMWware) breaks other PowerShell commands as there are Cmdlets/functions named like core or other modules Cmdlets.

So if you import the PowerCLI module it will provide you with the command Get-VM, which is a good idea if you want to manage virtual machines on vSphere, but nowadays you might also have a Hyper-V server you want to manage from that very same computer (maybe using RSAT) which also has the Cmdlet Get-VM. So now it is no longer possible to manage virtual machines on the Hyper-V servers (at least to run the Cmldet Get-VM against Hyper-V) unless you remove the PowerCLI module from that PowerShell instance (or specify explicitly that the command should be the one from the Hyper-V module).

It is not very likely that VMWare will change their commands anytime soon to not overwrite/conflict with other Cmdlets/functions. So what could you possible do manage both hypervisors within one script?

You can import the module which breaks your code with the parameter -Prefix.

Let’s see how this works. For demo purpose I will use the BitsTransfer module.

So importing the module “as usual” and getting the available commands gives you just what you expect.

Import-Module and the "-Prefix" parameter

But importing it with a prefix will add this very prefix to every command (after the verb-) of that module.

Import-Module and the "-Prefix" parameter

Very cool feature I did not yet know about.

ConvertTo-Influxdb.ps1

As I was about to merge two influx databases into one, I was surprised that one could not use the CLI influx to both import and export data.

export influx measurement

To export data from a influx database I used the following command:
influx -database home -format csv -execute "select * from temperature" > /tmp/temperature.csv
The measurement temperature from database home is exported and saved into a CSV file.

import fails

My attempt to import the data as follows failed:
influx -database private -format csv -path /tmp/temperature.csv -import
2018/02/17 12:33:03 error writing batch: {"error":"unable to parse '
...
ERROR: 11397 points were not inserted

I did not RTFM but instead guessed the command/parameter combination which I thought would be reasonable. How naive I was!

formatting for the import

Turns out, the data has to be formatted like a normal insert; e.g.:
temperature,sensor=livingroom temperature=21
One has to convert the data from the CSV export to be able to import it again …

alternative scripts

I found several scripts on github which all convert CSV files to influx. But those where all written in programming languagues which I neither had installed nor knew (as in “created at least 10 scripts in that language”). I was looking for a Bash script but couldn’t find any. And writing Bash scripts is not fun for me. Thus I wrote the PowerShell script ConvertTo-Influxdb.

running ConvertTo-Influx.ps1

Converting a CSV file into one which can be imported using the influx CLI goes like this:
ConvertTo-Influx.ps1 -Path /tmp/temperature.csv -Database private > /tmp/temperature.influx
The script takes two parameters (both mandatory):
Path: the path to the CSV file
Database: the database name you want the data to be imported to

import data into influx database

Once converted the data can be imported using the influx CLI:
influx -path /tmp/temperature.influx -import

MediaWiki update script “mediawiki-updater”

Because repetative tasks bore me (my principle is: “If you have to do something twice, automate it. Every time you have to do it again increases the likeliness of doing it again.”), I have written a two scripts to automate the task “update MediaWiki”.

As I manage installations on both Linux and Windows I created a Bash and a PowerShell script.

The files (and their requirements) can be found in the following repo:

https://github.com/gpunktschmitz/mediawiki-updater

Here are the steps the scripts do:
1. check if a new release is available
2. backup the MySQL database
3. backup (copy) the current installation
4. download new MediaWiki release
5. extract new MediaWiki release into a temporary directory
6. overwrite current installation (files) with the new release
7. delete the directory “vendor”
8. clone the external libraries into the directory “vendor” using git
9. delete old backups (but keep the last 3)
10. run the script “maintenance/update.php”

While creating these I thought of running them by cron/scheduled task. They are not yet perfect for that – but I was able to successfully update my installations from version 1.28.2 to 1.29.0 by manually running them.

Hope it helps you automate that task also.

powershell – download file from server via https which has a self signed certificate

the problem:

when I was trying to “wget” a file using $webClient.DownloadFile from a server with a self signed certificate it failed when using the following code:

$webClient = new-object System.Net.WebClient
$webClient.DownloadFile( $url, $path )

 

powershell - download file from server via https which has a self signed certificate

the resource:

searching for a solution i stumpled upon http://blogs.technet.com/b/heyscriptingguy/archive/2010/07/25/windows-powershell-invalid-certificates-and-automated-downloading.aspx

there you will read that all it takes is to set the following just before you download the file …

[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

the solution:

the complete script then would be something like:

$url = "https://server/file.ext"
$path = "c:\downloads\file.ext"
[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$webClient = new-object System.Net.WebClient
$webClient.DownloadFile( $url, $path )