ninja-one/ninja-boilerplate.ps1
|
[CmdletBinding()] param ( [Parameter()] [string]$Token ) begin { # Check for required PowerShell version (7+) if (!($PSVersionTable.PSVersion.Major -ge 7)) { try { # Install PowerShell 7 if missing if (!(Test-Path "$env:SystemDrive\Program Files\PowerShell\7")) { Write-Output '[INFO] Installing PowerShell version 7...' Invoke-Expression "& { $( Invoke-RestMethod https://aka.ms/install-powershell.ps1 ) } -UseMSI -Quiet" } # Refresh PATH $env:Path = [System.Environment]::GetEnvironmentVariable('Path', 'Machine') + ';' + [System.Environment]::GetEnvironmentVariable('Path', 'User') # Restart script in PowerShell 7 pwsh -File "`"$PSCommandPath`"" @PSBoundParameters } catch { Write-Output '[ERROR] PowerShell 7 was not installed. Update PowerShell and try again.' throw $Error } finally { exit $LASTEXITCODE } } else { $PSStyle.OutputRendering = 'PlainText' } if ($env:token -and $env:token -notlike "null") { $Token = Ninja-Property-Get $env:token } if (-not $Token) { Write-Host "[ERROR] Please specify a Token." exit 1 } } process { try { } catch { # output the error and the line it came from Write-Host "[ERROR] Error: $_" Write-Host "[ERROR] Line: $($_.InvocationInfo.ScriptLineNumber)" exit 1 } } end { } |