Categories
PowerShell Web

Generate a self-signed certificate for IIS with a custom common name

Using the IIS Manager you can generate self-signed certificates for HTTPS bindings. The certificates generated by the IIS Manager have the limitation that the common name (CN) of the certificate is always the computer name.

If you wish to set an own common name for the certificate, you can use following command in PowerShell (open as administrator) to generate a new self-signed certificate.

New-SelfSignedCertificate -DnsName "my.customdomain.com" -FriendlyName "MyServiceName" -NotAfter (Get-Date).AddYears(10)
Categories
PowerShell

Generate a GUID

Here’s a small but very useful PowerShell script to generate GUIDs in different formats.

Categories
PowerShell

Monitor text files with PowerShell

You can use this method with PowerShell as alternative to the tail command at Linux.

Categories
PowerShell

Visualize data in PowerShell as GridView.

When using PowerShell, did you ever wish to have a function to pipe structured data to visualize it.

PowerShell has a great built-in function for that: Out-GridView.
You can even filter the rows in the grid.

Categories
PowerShell

Waiting in PowerShell for an action to complete

If you want PowerShell to wait for an action to complete, you can display a loading spinner using these few lines of code.

The script will print a new dot every three seconds until the condition in the until-clause is fulfilled.

The result looks like this:

Categories
PowerShell

Pipe output to clipboard

You can use the command line program clip to pipe the output of a command to your clipboard.

Write-Output "Hello World!" | clip
Categories
PowerShell

Grep on Windows – Searching in all files recursively

There’s no command line tool on Windows like Grep on Linux to search for a specific text in all files recursively. But there’s a simple alternative.

Bash

grep -r "peter" *

PowerShell

ls * -r | sls "peter"
Categories
PowerShell

Alternative for Invoke-SqlCmd

Invoke-SqlCmd ist a great cmdlet for PowerShell for querying SQL databases. But sometimes you don’t want to install the extra requirement SQL Server Management Objects on the server.

Invoke-SqlCmd -ServerInstance $Server -Database $Database -Query "SELECT * FROM Staff"

There’s an simple alternative. Use the .NET classes in the System.Data.SqlClient namespace which are available on every computer which has the .NET framework installed.

function SqlQuery($server, $database, $query)
{
 $connection = New-Object System.Data.SqlClient.SqlConnection
 $connection.ConnectionString = "Server=$server;Database=$database;Integrated Security=True;"
 $connection.Open()
 $command = $connection.CreateCommand()
 $command.CommandText = $query
 $result = $command.ExecuteReader()
 $table = new-object “System.Data.DataTable”
 $table.Load($result)
 $connection.Close()
 return $table
}

SqlQuery $Server $Database "SELECT * FROM Staff"