1

I'm validating a markdown using Powershell script and I use below command line

markdownlint.cmd "c:\index.md"

And it returns output like below

levels should only increment by one level at a time [Expected: h2; Actual: h3]

index.md: 4: MD003/heading-style/header-style Heading style 
[Expected: setext; Actual: atx]

I want to export the output log to a variable, in which I'd like to test like

if( $output -contains '*Expected*')
{

Write-Host "contains errors"
}

Unfortunately I'm unable to export to a variable within code, How can I do it?

Options I tried

$output = Invoke-Expression "markdownlint.cmd 'c:\index.md' "

    markdownlint.cmd 'c:\index.md' | Export-csv c:\test.csv

This did not work.

Note: markdownlint.cmd is CMD line Env.

2 Answers 2

3

Simply enclose your batch command and args in parentheses

$output = (markdownlint.cmd "c:\index.md")

if( $output -match 'Expected') { 
    Write-Host "contains errors" 
}

Single quotes have no special meaning in cmd.exe,

so markdownlint.cmd 'c:\index.md' fails because it looks for them literally.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Unfortunately nothings gets assigned to $output. This is my code . ``` npm install -g markdownlint-cli $output = (markdownlint.cmd "C:\index.md") Write-Host $output if( $output -contains 'Expected') { Write-Host "contains errors" } ```
You are using the wrong comparison operator, -contains checks if the array $output has an exact entry Expected, use RegEx based -match instead. Read online
0

Export-csv is a combination of two commands: convertTo-csv piped to out-file.

The markdownlint.cmd "c:\index.md" is probably not producing a powershell object as output. From Get-Help ConvertTo-Csv -Full I can see ConvertTo-csv requires as input a powershell object:

 SYNTAX
ConvertTo-Csv [-InputObject] <PSObject> [[-Delimiter] <Char>] [-NoTypeInformation] [<CommonParameters>]

ConvertTo-Csv [-InputObject] <PSObject> [-NoTypeInformation] [-UseCulture] [<CommonParameters>]

You can use the

markdownlint.cmd 'c:\index.md' | out-file 'c:\textfile.txt'

and then examine the file content and see how you can parse or convert it.

1 Comment

@lftimie - It wont anything export to file . i want to write the data to a variable within.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.