Forked from NakedPowerShell/gist:9711e57e18fa4c4cd0fed148497f900e
Created
October 8, 2018 07:12
-
-
Save Splaxi/6d746acc56f48c3c044ecdee03ab1f08 to your computer and use it in GitHub Desktop.
Script to generate all the exploration I normally do when I want to install a new PowerShell module
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 Show-ModExplore { | |
<# | |
.SYNOPSIS | |
Generate all the exploration I normally do when I want to install a new PowerShell module | |
.DESCRIPTION | |
Script to generate all the exploration I normally do when I want to install a new PowerShell module | |
and look at what each function does | |
.PARAMETER Module | |
The name of the PowerShell module to explore | |
.EXAMPLE | |
Show-ModExplore "dbachecks" | |
.NOTES | |
Script to generate all the exploration I normally do when I want to install a new PowerShell module | |
and look at what each function does | |
When I explore a new PowerShell module, I find I do the same steps over and over again | |
It was time to automate the exploration a little bit | |
For each parameter, the generated line for a splat will look something like this: | |
'PersonalAccessToken'='blank'; # String (None) [true] | |
PersonalAccessToken is the parameter name | |
'blank' is the default my code assigns it - based upon parameter data type | |
After the comment # is the data type; String, Int, DateTime,Bolean etc | |
In the () is the default of the parameter inside the function | |
In the [] is if the parameter is required i.e. mandatory, either true or false | |
Version History | |
v1.0 - [Twitter: @NakedPowerShell] [Blog: https://nakedpowershell.blogspot.com/ ] - Initial Release: 07/10/2018 | |
#> | |
[CmdletBinding()] | |
param( | |
[Parameter(ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $true, Mandatory = $true)] | |
[string]$Module | |
) | |
Clear-Host | |
$Today = Get-Date | |
if (Get-Module -ListAvailable -Name $Module) { | |
Write-Host "Module $Module is all ready installed" | |
} | |
else { | |
Write-Output "Run this command first to install the module $Module" | |
Write-Output "Install-Module $module -Verbose -Force -Scope CurrentUser" | |
Exit | |
} | |
$Version = Get-InstalledModule -Name $Module | Select-object Version | |
Write-Output "# Started Generated Exploration Code for $module : Version: $Version Created $Today By: $env:UserName" | |
$commands = Get-Command -Module $module | Where-Object -FilterScript {$_.Commandtype -eq 'function'} | |
$Outfile = "startup_" + $module + "_1.ps1" | |
#$eOutfile = "startup_" + $module + "_Error.txt" | |
#Write-Output "# Generated Exploration Code for $module : Version: $Version Created $Today By: $env:UserName" | Out-File $eoutFile | |
#Write-Output "" | Out-File $eoutFile -Append | |
Write-Output "# Generated Exploration Code for $module : Version: $Version Created $Today By: $env:UserName" | Out-File $outFile | |
Write-Output "" | Out-File $outFile -Append | |
Write-Output "Install-Module $module -Verbose -Force -Scope CurrentUser" | Out-File $outFile -Append | |
Write-Output "" | Out-File $outFile -Append | |
Write-Output "Import-Module $module" | Out-File $outFile -Append | |
Write-Output "" | Out-File $outFile -Append | |
Write-Output "Update-Module $module" | Out-File $outFile -Append | |
Write-Output "" | Out-File $outFile -Append | |
Write-Output "Get-Help $module -Examples" | Out-File $outFile -Append | |
Write-Output "" | Out-File $outFile -Append | |
Write-Output "Get-Help $module -Full" | Out-File $outFile -Append | |
Write-Output "" | Out-File $outFile -Append | |
Write-Output "Get-Help $module -Detailed" | Out-File $outFile -Append | |
Write-Output "" | Out-File $outFile -Append | |
foreach ($command in $commands) { | |
$commandName = $command.Name | |
Write-Output "# Function $commandname for $module" | |
Write-Output "# Function $commandname for $module" | Out-File $outFile -Append | |
Write-Output "" | Out-File $outFile -Append | |
Write-Output "Get-Help $commandname -Examples" | Out-File $outFile -Append | |
Write-Output "" | Out-File $outFile -Append | |
Write-Output "Get-Help $commandname -Full" | Out-File $outFile -Append | |
Write-Output "" | Out-File $outFile -Append | |
Write-Output "Get-Help $commandname -Detailed" | Out-File $outFile -Append | |
Write-Output "" | Out-File $outFile -Append | |
$Common = 'Debug', 'ErrorAction', 'ErrorVariable', 'InformationAction', 'InformationVariable', 'OutBuffer', 'OutVariable', | |
'PipelineVariable', 'Verbose', 'WarningAction', 'WarningVariable' | |
$Help = Get-Help $commandName -ErrorAction SilentlyContinue | |
$parameters = $command.ParameterSets.Parameters | Sort-Object -Property Name -Unique | Where-Object Name -notin $common | |
$O = "$" + "parms = @{" | |
Write-Output $O | Out-File $outFile -Append | |
foreach ($parameter in $parameters) { | |
$parameterName = $parameter.Name | |
$parameterHelp = $Help.parameters.parameter | Where-Object Name -EQ $parameterName | |
$p_PV = $parameterHelp.parameterValue | |
$p_DV = $parameterHelp.defaultValue | |
$p_MM = $parameterHelp.required | |
switch ( $p_PV ) { | |
"String" { | |
Write-Output "'$parametername'='blank'; # $p_PV ($p_DV) [$p_MM]" | Out-File $outFile -Append | |
} | |
"String[]" { | |
Write-Output "'$parametername'='blank'; # $p_PV ($p_DV) [$p_MM]" | Out-File $outFile -Append | |
} | |
"GUID" { | |
$G = [guid]::NewGuid() | |
Write-Output "'$parametername'='$G'; # $p_PV ($p_DV) [$p_MM]" | Out-File $outFile -Append | |
} | |
"GUID[]" { | |
$G = [guid]::NewGuid() | |
Write-Output "'$parametername'='$G'; # $p_PV ($p_DV) [$p_MM]" | Out-File $outFile -Append | |
} | |
"SwitchParameter" { | |
Write-Output "'$parametername'=`$false; # $p_PV ($p_DV) [$p_MM]" | Out-File $outFile -Append | |
} | |
"Int" { | |
Write-Output "'$parametername'=1; # $p_PV ($p_DV) [$p_MM]" | Out-File $outFile -Append | |
} | |
"Int[]" { | |
Write-Output "'$parametername'=1; # $p_PV ($p_DV) [$p_MM]" | Out-File $outFile -Append | |
} | |
"Int32" { | |
Write-Output "'$parametername'=1; # $p_PV ($p_DV) [$p_MM]" | Out-File $outFile -Append | |
} | |
"Int32[]" { | |
Write-Output "'$parametername'=1; # $p_PV ($p_DV) [$p_MM]" | Out-File $outFile -Append | |
} | |
"Bool" { | |
Write-Output "'$parametername'=`$false; # $p_PV ($p_DV) [$p_MM]" | Out-File $outFile -Append | |
} | |
"Boolean" { | |
Write-Output "'$parametername'=`$false; # $p_PV ($p_DV) [$p_MM]" | Out-File $outFile -Append | |
} | |
"Datetime" { | |
Write-Output "'$parametername'='$today'; # $p_PV ($p_DV) [$p_MM]" | Out-File $outFile -Append | |
} | |
default { | |
Write-Output "'$parametername'='blank'; # $p_PV ($p_DV) [$p_MM]" | Out-File $outFile -Append | |
#Write-Output "'$parametername'='blank'; # $p_PV ($p_DV) [$p_MM]" | Out-File $eoutFile -Append | |
} | |
} | |
} | |
Write-Output "}" | Out-File $outFile -Append | |
Write-Output "$commandName @parms -verbose -WhatIf" | Out-File $outFile -Append | |
Write-Output "" | Out-File $outFile -Append | |
} | |
Write-Output "# End Generated Exploration Code for $module : Created $Today By: $env:UserName" | |
Write-Output "" | |
Write-Output "# PowerShell script generated for module $module is : $outfile" | |
} # End of Show-ModExplore | |
Show-ModExplore "VSTeam" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment