big-data-for-humans / azure-automation-tools

Set of tools for Azure Automation
MIT License
0 stars 0 forks source link

Add check to make sure runbook can be parsed before uploading #34

Open lfshr opened 7 years ago

lfshr commented 7 years ago

Right now runbooks will get uploaded regardless of whether or not they contain code that can be parsed. A call should be added to make sure this doesn't happen. Finding out that the code cannot be parsed while in the portal means waiting for the upload, setting of variables, and runbook queue before an error occurs.

This can be achieved with the following:

$ParseErrors = @()
$FilePath = 'C:\Test\cantparse.ps1'
[System.Management.Automation.Language.Parser]::ParseFile($FilePath, [ref]$null, [ref]$ParseErrors) | Out-Null
if ($ParseErrors) {
    $ParseErrors | ForEach-Object -Process {
        Write-Error -Message "Failed to parse file '$FilePath': $($_.Message) at line $($_.Extent.StartLineNumber) column $($_.Extent.StartColumnNumber)" -ErrorId $_.ErrorId -ErrorAction Continue
    }

    return
}
lfshr commented 7 years ago

@stvnrs you good with this? If so I'll work it. 😄