Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
January 3, 2024

Extending RESTful Objects

Welcome to 2024!

I want to thank all of my subscribers once again for making it possible for me to do this newsletter. I have a full slate of articles planned for this month, but before we do, let’s wrap up our exploration of building custom objects from REST data using PowerShell.

I left off with this function that queries the OMBD API.

Function Get-OMDBMovie {
    [cmdletbinding()]
    [OutputType('psMovieInfo')]
    [alias('omdb')]
    Param(
        [Parameter(
            Position = 0,
            Mandatory,
            ValueFromPipeline,
            HelpMessage = 'Specify the movie title'
        )]
        [ValidateNotNullOrEmpty()]
        [String]$Title,
        [Parameter(ValueFromPipelineByPropertyName, HelpMessage = 'Specify the year the movie was released.')]
        [int]$Year,
        [Parameter(Mandatory)]
        [SecureString]$APIKey
    )

    Begin {
        Write-Verbose "[$((Get-Date).TimeOfDay) BEGIN  ] Starting $($MyInvocation.MyCommand)"
        Write-Verbose "[$((Get-Date).TimeOfDay) BEGIN  ] Running under PowerShell version $($PSVersionTable.PSVersion)"

    } #begin

    Process {
        Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Getting movie data for $Title [$Year]"
        $uri = 'https://www.omdbapi.com/'
        #convert the secure string to plain text
        $key = [System.Runtime.InteropServices.marshal]::PtrToStringAuto([System.Runtime.InteropServices.marshal]::SecureStringToBSTR($APIKey))
        $uri += "?apikey=$key"

        $uri += "&t=$($title -replace '\s','+')"
        if ($year) {
            $uri += "&y=$year"
        }

        $m = Invoke-RestMethod $uri -DisableKeepAlive
        $m.PSObject.properties | ForEach-Object -Begin {
            $tmpHash = [ordered]@{
                PSTypeName = 'omdbMovieInfo'
            }
        } -Process {
            $TestValue = $_.Value
            Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Processing $($_.Name) [$($TestValue)]"
            #Set value type based on regex pattern
            Switch -regex ($TestValue) {
                '^\$\d+' { $v = ($TestValue -replace ('\$|,')) -as [int]; Break }
                '\d+(?=\smin)' { $v = New-TimeSpan -Minutes $matches[0] ; Break }
                '\d+\s\w+\s\d{4}' { $v = $TestValue -as [DateTime]; Break }
                '^((\d+)|(\d+,\d+))$' { $v = $TestValue -as [int] ; Break }
                '\d+\.\d+' { $v = $TestValue -as [Double] }
                '\,' { $v = ($TestValue -split ',').Trim() }
                Default { $v = $TestValue }
            }
            $tmpHash.Add($_.Name, $v)
        } -End {
            [PSCustomObject]$TmpHash
        }
    } #process

    End {
        Write-Verbose "[$((Get-Date).TimeOfDay) END    ] Ending $($MyInvocation.MyCommand)"
    } #end

} #close Get-OMDBMovie
Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.