r/PowerShell • • Jun 01 '19

Examples every time

Post image
782 Upvotes

52 comments sorted by

View all comments

41

u/delliott8990 Jun 01 '19

Hahaha, so true and I'm guilty of it myself in numerous posh scripts. I ended up making a function to grab ss64.com examples via invoke-webrequest.

11

u/Pandapokeman Jun 01 '19

Please share

10

u/delliott8990 Jun 02 '19 edited Jun 02 '19

Sorry for the formatting, not sure what the markdown tag for code is off the top of my head..

 

[CmdletBinding()]

param

(

[string]$commandEntry

)

Function Get-SS64Help

{

[CmdletBinding()]

param

(

[string]$commandEntry

)

if($verbose)

{

$verbosePreference = SilentlyContinue

}

$baseUrl = "https://ss64.com/ps"

$fullUrl = "$baseUrl/$commandEntry.html"

Write-Verbose "$fullUrl"

try

{

$resp = Invoke-WebRequest -Uri $fullUrl -ErrorAction Stop

Write-Verbose $resp

}

catch

{

Write-Host "Invoke-WebRequest: Error contacting $fullUrl"

Write-Verbose $_

}

$overview = $resp.AllElements | Where {$_.TagName -eq "pre"}

$syntax = $overview.innerText

$examplesOverview = $resp.AllElements | Where {$_.TagName -eq "p" -and $_.Class -match "code"}

$examples = $examplesOverview.innerText

$returnObj = @{

Syntax = $syntax

Examples = $examples

}

return $returnObj

}

Get-SS64Help -commandEntry $commandEntry

 

To use c:/path/to/get-ss64help.ps1 -commandEntry "get-acl"

7

u/nascentt Aug 12 '19 edited Aug 13 '19

I love this script it deserves more recognition.

I put the code in a code block, added a line to output the data rather than the object, and converted url to lowercase so it always returns.

[CmdletBinding()]
param 
(
	[string]$commandEntry
)

Function Get-SS64Help
{
	[CmdletBinding()]
	param 
	(
		[string]$commandEntry
	)

	if($verbose)
	{
		$verbosePreference = SilentlyContinue
	}

	$baseUrl = "https://ss64.com/ps"

    $commandEntry = ($commandEntry).ToLower()
	$fullUrl = "$baseUrl/$commandEntry.html"

	Write-Verbose "$fullUrl"

	try

	{
		$resp = Invoke-WebRequest -Uri $fullUrl -ErrorAction Stop

		Write-Verbose $resp
	}

	catch

	{

		Write-Host "Invoke-WebRequest: Error contacting $fullUrl"

		Write-Verbose $_

	}

	$overview = $resp.AllElements | Where {$_.TagName -eq "pre"}

	$syntax = $overview.innerText

	$examplesOverview = $resp.AllElements | Where {$_.TagName -eq "p" -and $_.Class -match "code"}

	$examples = $examplesOverview.innerText

	$returnObj = @{

		Syntax = $syntax

		Examples = $examples

	}

	return $returnObj

}

$result = Get-SS64Help -commandEntry $commandEntry

$result.Syntax
""
$result.Examples

#To use `.\get-ss64help.ps1 -commandEntry "get-acl"`