[PowerShell] Get-Pi(e) Function

Happy Holidays! Treat yourself to a piece of Pi(e) with this custom PowerShell Function. It’s as easy as Get-Pi or Get-Pie, depending on your mood. Whether you need one piece or a thousand digits, this custom function has you covered.

Note

As pointed out by Temptis on Reddit, this function is more for entertainment than practicality. In real applications, try [math]::pi instead.

1, 100, and 1000 digits of Pi
Get-Help Get-Pi
Get-Help Get-Pi -Examples

For queries over 100 digits of Pi, the function uses the pi.delivery API to retrieve the requested number of digits. Check out the link below for other interesting Pi related projects. This my attempt at producing adaptive outputs depending on which command name was executed. If you call Get-Pi, you’ll get digits of Pi. Alternatively, if you call the functions alias, Get-Pie, you get pies and slices of pie.

Link to Happy Pi Day!

Happy Pi Day!

Celebrate Pi Day with Pi delivered via an API

The Code

<#
.Synopsis
   This cmdlet retrieves a piece of pi.

.DESCRIPTION
   This cmdlet retrieves a piece of pi. (Default is 3 digits)

.PARAMETER Digits
    The digits parameter will determine how many pieces of pi are retrieved.
 
.INPUTS
System.Integer
Get-Pi accepts an integer array on the pipeline.
 
Get-Pi accepts an integer in the digits property objects in the pipeline.
 
.OUTPUTS
Suprise!

.EXAMPLE
   Get-Pi

.EXAMPLE
   Get-Pi  5

.EXAMPLE
   Get-Pi -Digits 10
.NOTES
   
Disclaimer:
  This code is provided "as is" and with no expressed guarantees. The code provider makes no representations or warranties of any kind concerning 
the safety, suitability, inaccuracies, typographical errors, or other harmful components of this code. There are inherent dangers in the use of 
any code, and you are solely responsible for determining whether this code is compatible with your equipment and other software installed on your 
equipment. You are also solely responsible for the protection of your equipment and backup of your data, and the provider will not be liable for 
any damages you may suffer in connection with using, modifying, or distributing this code.

 
AUTHOR(S):
Justin C.
#>

Function Get-Pi {
#region Function Parameters
    [Alias("Get-Pie")]
    Param
    (
        #Number of digits to retrieve
        [Alias("Pieces")]
        [Parameter(Position=0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [Int[]] $Digits = 3

    )
#endregion Function Parameters
    Begin {
        $API_Url = "https://api.pi.delivery/v1/pi"
        #100 digits of Pi
        $Pi = "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873115956286388235378759375195778185778053217122680661300192787661119590921642019"
        $Results = ""
    }
    Process {
        ForEach ($Digit in $Digits) {
            If ((Get-PSCallStack | Select-Object -Property Position -ExpandProperty Position) -like "Get-Pie*"){
                $Pies =  [Int]"$($Digit / 6)".Split(".")[0]
                $Pieces = [Int]($Digit % 6)
                $Line = @{}
                For ($i=1;$i -le $Pies; $i++) {                   
                    $Line[0] += "          (            "
                    $Line[1] += "           )           "
                    $Line[2] += "      __..---..__      "
                    $Line[3] += "  ,-='  /  |  \  '=-.  "
                    $Line[4] += " :--..___________..--; "
                    $Line[5] += " \.,______________,./  "
                    If (($i % 4) -eq 0){
                        For ($i2 = 0; $i2 -lt 6; $i2++){
                            $Results += "$($Line[$i2])`n"
                        }
                        $Line = @{}
                    }
                }
                $Results += "$($Line[0])`n$($Line[1])`n$($Line[2])`n$($Line[3])`n$($Line[4])`n$($Line[5])`n"
                $Line = @{}
                For ($i=0;$i -lt $Pieces; $i++) {
                        $Line[0] += "()()()()()()"
                        $Line[1] += "|\         |"
                        $Line[2] += "|.\. . . . |"
                        $Line[3] += "\'.\       |"
                        $Line[4] += " \.:\ . . .|"
                        $Line[5] += "  \'o\     |"
                        $Line[6] += "   \.'\. . |"
                        $Line[7] += "    \'.\   |"
                        $Line[8] += "     \'.\ .|"
                        $Line[9] += "      \.'\ |"
                        $Line[10] += "       \__\|"
                }
                $Results += "$($Line[0])`n$($Line[1])`n$($Line[2])`n$($Line[3])`n$($Line[4])`n$($Line[5])`n$($Line[6])`n$($Line[7])`n$($Line[8])`n$($Line[9])`n$($Line[10])"
            } Else {
                #[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
                #Invoke-RestMethod -Method Get -Uri 'https://api.pi.delivery/v1/pi?start=0&numberOfDigits=100'

                If ($Digit -le 0){
                    $Results = ""
                    Write-Error "Invalid number of digits, must be greater than 0."
                }
                ElseIf ($Digit -eq 1){
                    $Results = "3"
                }
                ElseIf ($Digit -le 100) {
                    $Results = $Pi.Substring(0,$Digit+1)
                } ElseIf ($Digit -le 1000){
                        $Results = $(Invoke-RestMethod -Method Get -Uri "$($API_Url)?start=0&numberOfDigits=$($Digit)").content
                        $Results = $Results.substring(0, 1) + "." + $Results.substring(1, $Results.length - 1)
                } Else {
                    $Results = $(Invoke-RestMethod -Method Get -Uri "$($API_Url)?start=0&numberOfDigits=1000").content
                    $Results = $Results.substring(0, 1) + "." + $Results.substring(1, $Results.length - 1)
                    $Digit -= 1000
                    $Offset = 1000
                    While ($Digit -gt 0){
                        If ($Digit -ge 1000){
                            $Results += $(Invoke-RestMethod -Method Get -Uri "$($API_Url)?start=$($Offset)&numberOfDigits=1000").content
                            $Digit -= 1000
                            $Offset += 1000
                        } Else {
                            $Results += $(Invoke-RestMethod -Method Get -Uri "$($API_Url)?start=$($Offset)&numberOfDigits=$($Digit)").content
                            $Digit -= $Digit
                        }  
                    }
                }
            }
        }
    }
	End {Return $Results}
}
Justin C. (Author Profile)

Justin C.

Technology Enthusiast

I’m a Soldier by day and coder by night. A life long learner of cyber security, penetration testing, machine learning, circuit design, art, and photography.

Liked it? Take a second to support Justin on Patreon!
%d bloggers like this: