DEV Community

Alex Sarafian
Alex Sarafian

Posted on • Originally published at sarafian.github.io

Render a computer badge with name and state in markdown with PowerShell using MarkdownPS

Originally posted here

Using MarkdownPS PowerShell module this is a simple cmdlet that

  1. Tests the state of the computer using PowerShell's Test-Connection cmdlet
  2. Depending on the outcome it generates a badge in markdown with red or green state.
function New-ComputerBadge {
    param (
        [Parameter(Mandatory=$true)]
        [string]
        $Computer
    )
    try
    {
        if(Test-Connection $Computer -Quiet)
        {
            $color="green"
            $status="Live"
        }
        else
        {
            $color="red"
            $status="Not Live"
        }
        New-MDImage -Subject $Computer -Status $status -Color $color
    }
    catch
    {
        Write-Error $_
        New-MDImage -Subject "Badge" -Status "Error" -Color red
    }
}

Example

New-ComputerBadge -Computer "EXAMPLE"

renders the following markdown

![](https://img.shields.io/badge/EXAMPLE-Live-green.svg)
![](https://img.shields.io/badge/EXAMPLE-Not%20Live-red.svg)


Discussion (4)

Collapse
mburszley profile image
Maximilian Burszley

Is there any reason to use MarkdownPS over the *-Markdown cmdlets included in PSv6+? I like the badge feature, but you could generate SVGs without including external references.

Collapse
sarafian profile image
Alex Sarafian Author

Hmm, I wasn't aware about those cmdlets but from the quick investigation I did the MarkdownPS offers a completely different functionality. The module offers the ability to render/produce markdown and now with these nice cmdlets you can get a quick conversion or preview in html. So completely different functionality around the same topic that is markdown.

With regards to the SVGs, the shields.io is very simple alternative I think without getting into SVG territory. It also doesn't require the creation of any resource, so I think having the ability to generate a text that shows a nice picture without the need to create any additional files anywhere is the best and simplest way. It is the same as the shields and badges that everyone uses in the github repositories. The module was created in a manner that other services like shields.io can be used but I've not looked further.

Collapse
mburszley profile image
Maximilian Burszley

Thanks for the response. My only concern with using external resources is corporate firewalls and the like breaking what would otherwise be nice reporting functionality.

Thread Thread
sarafian profile image
Alex Sarafian Author

In that case you have a problem indeed. You would need to produce the picture first, store it and then use MarkdownPS normal image cmdlets to reference the just produced picture.

For the purpose of this badge, you can download the pictures upfront and just reference them.