Created
January 12, 2021 14:12
-
-
Save Paebbels/8765f0e21ef739507b8810f6cc246ba8 to your computer and use it in GitHub Desktop.
PowerShell Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# GitHub user: https://github.com/mkropat | |
# Gist account at GitHub: https://gist.github.com/mkropat | |
# Gist snippet URL: https://gist.github.com/mkropat/c1226e0cc2ca941b23a9 | |
function Add-EnvPath | |
{ param( | |
[Parameter(Mandatory=$true)] | |
[string] $Path, | |
[ValidateSet("Machine", "User", "Session")] | |
[string] $Container = "Session" | |
) | |
if ($Container -ne "Session") | |
{ $containerType = $EnvPath_ContainerMapping[$Container] | |
$persistedPaths = [Environment]::GetEnvironmentVariable("Path", $containerType) -split ";" | |
if ($persistedPaths -notcontains $Path) | |
{ $persistedPaths = $persistedPaths + $Path | where { $_ } | |
[Environment]::SetEnvironmentVariable("Path", $persistedPaths -join ";", $containerType) | |
} | |
} | |
$envPaths = $env:Path -split ";" | |
if ($envPaths -notcontains $Path) | |
{ $envPaths = $envPaths + $Path | where { $_ } | |
$env:Path = $envPaths -join ";" | |
} | |
} | |
# GitHub user: https://github.com/mkropat | |
# Gist account at GitHub: https://gist.github.com/mkropat | |
# Gist snippet URL: https://gist.github.com/mkropat/c1226e0cc2ca941b23a9 | |
function Remove-EnvPath | |
{ param ( | |
[Parameter(Mandatory=$true)] | |
[string] $Path, | |
[ValidateSet("Machine", "User", "Session")] | |
[string] $Container = "Session" | |
) | |
if ($Container -ne "Session") | |
{ $containerType = $EnvPath_ContainerMapping[$Container] | |
$persistedPaths = [Environment]::GetEnvironmentVariable("Path", $containerType) -split ";" | |
if ($persistedPaths -contains $Path) | |
{ $persistedPaths = $persistedPaths | where { $_ -and $_ -ne $Path } | |
[Environment]::SetEnvironmentVariable("Path", $persistedPaths -join ";", $containerType) | |
} | |
} | |
$envPaths = $env:Path -split ";" | |
if ($envPaths -contains $Path) | |
{ $envPaths = $envPaths | where { $_ -and $_ -ne $Path } | |
$env:Path = $envPaths -join ";" | |
} | |
} | |
# GitHub user: https://github.com/mkropat | |
# Gist account at GitHub: https://gist.github.com/mkropat | |
# Gist snippet URL: https://gist.github.com/mkropat/c1226e0cc2ca941b23a9 | |
function Get-EnvPath | |
{ param ( | |
[Parameter(Mandatory=$true)] | |
[ValidateSet("Machine", "User")] | |
[string] $Container | |
) | |
$containerType = $EnvPath_ContainerMapping[$Container] | |
[Environment]::GetEnvironmentVariable('Path', $containerType) -split ";" | where { $_ } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Exit-CompileScript | |
{ <# | |
.SYNOPSIS | |
Undocumented | |
.DESCRIPTION | |
Undocumented | |
.PARAMETER ExitCode | |
ExitCode of this script run | |
#> | |
[CmdletBinding()] | |
param( | |
[int]$ExitCode = 0 | |
) | |
cd $Module_WorkingDir | |
# unload modules | |
if (-not $Module_Hosted) | |
{ Remove-Module shared -Verbose:$false -Debug:$false | |
Remove-Module targets -Verbose:$false -Debug:$false | |
} | |
exit $ExitCode | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Restore-NativeCommandStream | |
{ <# | |
.SYNOPSIS | |
This CmdLet gathers multiple ErrorRecord objects and reconstructs outputs | |
as a single line. | |
.DESCRIPTION | |
This CmdLet collects multiple ErrorRecord objects and emits one String | |
object per line. | |
.PARAMETER InputObject | |
A object stream is required as an input. | |
.PARAMETER Indent | |
Indentation string. | |
#> | |
[CmdletBinding()] | |
param( | |
[Parameter(ValueFromPipeline=$true)] | |
$InputObject | |
) | |
begin | |
{ $LineRemainer = "" } | |
process | |
{ if ($InputObject -is [System.Management.Automation.ErrorRecord]) | |
{ if ($InputObject.FullyQualifiedErrorId -eq "NativeCommandError") | |
{ Write-Output $InputObject.ToString() } | |
elseif ($InputObject.FullyQualifiedErrorId -eq "NativeCommandErrorMessage") | |
{ $NewLine = $LineRemainer + $InputObject.ToString() | |
while (($NewLinePos = $NewLine.IndexOf("`n")) -ne -1) | |
{ Write-Output $NewLine.Substring(0, $NewLinePos) | |
$NewLine = $NewLine.Substring($NewLinePos + 1) | |
} | |
$LineRemainer = $NewLine | |
} | |
} | |
elseif ($InputObject -is [String]) | |
{ Write-Output $InputObject } | |
else | |
{ Write-Host "Unsupported object in pipeline stream" } | |
} | |
end | |
{ if ($LineRemainer -ne "") | |
{ Write-Output $LineRemainer } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Test-GitRepository | |
{ <# | |
.SYNOPSIS | |
Returns true, if the current working directy is under git control. | |
#> | |
git rev-parse 2>&1 | Out-Null | |
return $LastExitCode -eq 0 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Write-ColoredGCCLine | |
{ <# | |
.SYNOPSIS | |
This CmdLet colors GHDL output lines. | |
.DESCRIPTION | |
This CmdLet colors GHDL output lines. Warnings are prefixed with 'WARNING: ' | |
in yellow and errors are prefixed with 'ERROR: ' in red. | |
.PARAMETER InputObject | |
A object stream is required as an input. | |
.PARAMETER SuppressWarnings | |
Skip warning messages. (Show errors only.) | |
.PARAMETER Indent | |
Indentation string. | |
#> | |
[CmdletBinding()] | |
param( | |
[Parameter(ValueFromPipeline=$true)] | |
$InputObject, | |
[Parameter(Position=1)] | |
[switch]$SuppressWarnings = $false, | |
[Parameter(Position=2)] | |
[string]$Indent = "" | |
) | |
begin | |
{ $ErrorRecordFound = $false } | |
process | |
{ if ($InputObject -is [String]) | |
{ if ($InputObject -match ":\d+:\d+:\swarning:\s") | |
{ if (-not $SuppressWarnings) | |
{ Write-Host "${Indent}WARNING: " -NoNewline -ForegroundColor Yellow | |
Write-Host $InputObject | |
} | |
} | |
elseif ($InputObject -match ":\d+:\d+:\s") | |
{ $ErrorRecordFound = $true | |
Write-Host "${Indent}ERROR: " -NoNewline -ForegroundColor Red | |
Write-Host $InputObject | |
} | |
else | |
{ Write-Host "${Indent}$InputObject" } | |
} | |
else | |
{ Write-Host "Unsupported object in pipeline stream" } | |
} | |
end | |
{ $ErrorRecordFound } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment