Skip to content

Instantly share code, notes, and snippets.

@kvprasoon
Last active February 27, 2018 18:50
Show Gist options
  • Save kvprasoon/834f398fc494f860ee76224013ccedb7 to your computer and use it in GitHub Desktop.
Save kvprasoon/834f398fc494f860ee76224013ccedb7 to your computer and use it in GitHub Desktop.
Script to create expressions to print parameter values in a PowerShell script
<#
.SYNOPSIS
This script will help to create expressions for logging the input values of a PowerShell script.
.DESCRIPTION
This script will help to create expressions for logging the input values of a PowerShell script.
.EXAMPLE
PS C:\> .\Build-ParamLog.ps1 -Script C:\Test.PS1 -Prefix Log
Above execution will create expressions with prefix Log, see below.
Log "[string]:UserName : $UserName"
Log "[string]:DomainName : $DomainName"
Log "[int]:OtherParameter : $OtherParameter"
.EXAMPLE
PS C:\> .\Build-ParamLog.ps1 -Script C:\Test.PS1 -Prefix Log -icm
Above execution will create expressions with prefix Log including CommonParameters, see below.
Log "[string]:UserName : $UserName"
Log "[string]:DomainName : $DomainName"
Log "[int]:OtherParameter : $OtherParameter"
Log "[System.Management.Automation.ActionPreference]:ErrorAction : $ErrorAction"
Log "[System.Management.Automation.ActionPreference]:WarningAction : $WarningAction"
Log "[System.Management.Automation.ActionPreference]:InformationAction : $InformationAction"
Log "[switch]:Verbose : $Verbose"
Log "[switch]:Debug : $Debug"
Log "[string]:ErrorVariable : $ErrorVariable"
Log "[string]:WarningVariable : $WarningVariable"
Log "[string]:InformationVariable : $InformationVariable"
Log "[string]:OutVariable : $OutVariable"
Log "[int]:OutBuffer : $OutBuffer"
Log "[string]:PipelineVariable : $PipelineVariable"
.EXAMPLE
PS C:\> .\Build-ParamLog.ps1 -Script C:\Test.PS1
Above execution will create expressions with default prefix Write-Output, see below.
Write-Output "[string]:UserName : $UserName"
Write-Output "[string]:DomainName : $DomainName"
Write-Output "[int]:OtherParameter : $OtherParameter"
.NOTES
Supports PowerShell 3.0,4.0,5.0,5.1 and 6.x versions
#>
#requires -version 3.0
[alias('gpl')]
param(
#Script path to build parameter log expression
[Parameter(Mandatory = $true)]
[String]$Script,
#Prefix for each parameter expression output
[String]$Prefix = 'Write-Output',
#If mentioned, CommonParameters will be included in the output
[Alias('icm')]
[Switch]$IncludeCommonParameters
)
If([IO.Path]::GetExtension($Script) -ne '.ps1' ){
Throw "$Script is not a PowerShell script, please provide a PowerShel script."
}
If( -not (Test-Path -Path $Script)){
Throw "Unable to locate ,make sure $Script is available"
}
$Command = Get-Command -Name $Script
if( $IncludeCommonParameters.IsPresent ){
$FilteredList = $Command.Parameters.Values
}
else{
$FilteredList = $Command.Parameters.Values | Where-Object -FilterScript {$_.Name -notin [System.Management.Automation.Cmdlet]::CommonParameters}
}
$FilteredList | ForEach-Object -Process { "$Prefix `"[$($_.ParameterType)]:$($_.Name) : $('$' + $($_.Name))`"" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment