Home > other >  Store a session in memory to be used with later commands (Powershell)
Store a session in memory to be used with later commands (Powershell)

Time:02-24

I'm writing quite some Powershell modules to be used with different kind of (rest) api's. The first thing that you need to do when connecting to an API is creating a session with authentication credentials. Now my question is: is it possible to store this session variable somewhere in memory, so it can be used with later commands?

Here's an example of a script with the Gitlab API. I create this function to create a session pscustomobject:

function New-GitlabSession {
    [CmdletBinding()]
    [OutputType([pscustomobject])]
    param
    (
        [Parameter()]
        [string]$Url = "https://gitlab.com/api/v4",
        [Parameter(Mandatory = $true)]
        [string]$GitlabToken
    )
    $Header = @{
        Authorization = "Bearer $GitLabToken"
    }
    return [pscustomobject]@{
        Url    = $Url
        Header = $Header
    }
}

So I create a new session variable with this function:

$Session = New-GitlabSession -GitlabToken 'your token'

When I want to check the Gitlab projects, I can write a function like this:

function Get-Project {
    [CmdletBinding()]
    [OutputType([pscustomobject])]
    param
    (
        [Parameter(Mandatory = $true)]
        [pscustomobject]$Session
    )

    $AllProjectGroups = Invoke-RestMethod -Headers $($Session.Header) -Method Get -Uri "$($Session.Url)/projects/?simple=true" -FollowRelLink
    $AllProjects = foreach ($AllProjectGroup in $AllProjectGroups) {
        $AllProjectGroup | Select-Object id, name, path_with_namespace, http_url_to_repo, permissions
    }
    return $AllProjects
}

So as you see, when calling this function, I need to provide the $session variable:

$Projects = Get-Project -Session $Session

Now my question is: how can I write a function, that doesn't need to explicitly ask for the session variable?

So just something like this:

New-GitlabSession -GitlabToken 'your token'
$Projects = Get-Project

Anyone an idea?

CodePudding user response:

You could assign the result of the call to New-GitlabSession to the $PSDefaultParameterValues automatic variable:

$PSDefaultParameterValues['Get-Project:Session'] = New-GitlabSession -GitlabToken 'your token'

Now PowerShell will automatically provide the session object as the argument to -Session unless you specify another value:

# no -Session parameter argument passed, default will be used
Get-Project    
# explicit argument passed to -Session, default will be ignored
Get-Project -Session (New-GitlabSession -GitlabToken 'someothertoken')

CodePudding user response:

Thanks to the feedback of Mathias R. Jessen, I have transformed my code into this working example:

I have the following Functions:

function Set-DefaultParameters {
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [pscustomobject]$Session
    )
    $PSDefaultParameterValues['Get-Project:Session'] = $Session
    # Put here more functions that you want to use with the session variable
}

function New-GitlabSession {
    [CmdletBinding()]
    [OutputType([pscustomobject])]
    param
    (
        [Parameter()]
        [string]$Url = "https://git.arxus.eu/api/v4",
        [Parameter(Mandatory = $true)]
        [string]$GitlabToken
    )
    $Header = @{
        Authorization = "Bearer $GitLabToken"
    }
    $NewSession =  [pscustomobject]@{
        Url    = $Url
        Header = $Header
    }
    Set-DefaultParameters -Session $NewSession
    return $NewSession
}


function Get-Project {
    [CmdletBinding()]
    [OutputType([pscustomobject])]
    param
    (
        [Parameter(Mandatory = $true)]
        [pscustomobject]$Session
    )
    $AllProjectGroups = Invoke-RestMethod -Headers $($Session.Header) -Method Get -Uri "$($Session.Url)/projects/?simple=true" -FollowRelLink
    $AllProjects = foreach ($AllProjectGroup in $AllProjectGroups) {
        $AllProjectGroup | Select-Object id, name, path_with_namespace, http_url_to_repo, permissions
    }
    return $AllProjects
}

With these functions, I can run the following commands in my shell:

New-GitlabSession -GitlabToken 'yourtoken'
$Projects = Get-Project
  • Related