Lösungen
Märkte
Referenzen
Services
Unternehmen
Easily enter a Container through PowerShell

Easily enter a Container through PowerShell

4. August 2017

Easily enter a Container through PowerShell

As I wrote here you can conveniently enter a running Container through PowerShell. As I am lazy (and hate doing repeating stuff) I created an additional function which makes it even easier. You could probably have done the same in five minutes but maybe it saves you at least those five… 🙂

function Enter-Container {
    Param(
    [parameter(Mandatory=$true,position=0)]
    [String]
    $Container 
    )
    Enter-PSSession -ContainerId (Get-Container $Container).ID -RunAsAdministrator
}

With that in place you can then get a PowerShell session inside a running container as easily as

Enter-Container containername

Update

Inspired by Jan Hoek’s post about argument completion I had the idea to make that function a bit more meaningful by adding auto completion for the container names. The idea is that you only need to enter the command name and can then tab through the possible argument values, in my case the available containers. This leads to the following code1:

function Enter-Container {
    [CmdletBinding()]
    param
    ()

    DynamicParam {
        # Set the dynamic parameters name
        $ParameterName = 'Container'
             
        # Create the dictionary 
        $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
 
        # Create the collection of attributes
        $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
             
        # Create and set the parameters' attributes
        $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
        $ParameterAttribute.Mandatory = $true
        $ParameterAttribute.Position = 0
 
        # Add the attributes to the attributes collection
        $AttributeCollection.Add($ParameterAttribute)
 
        # Generate and set the ValidateSet 
        $arrSet = Get-Container| select -ExpandProperty Names | ForEach-Object { $_.Substring(1) }
        $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)
 
        # Add the ValidateSet to the attributes collection
        $AttributeCollection.Add($ValidateSetAttribute)
 
        # Create and return the dynamic parameter
        $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)
        $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
        return $RuntimeParameterDictionary
    }

    begin {
        # Bind the parameter to a friendly variable
        $Container = $PsBoundParameters[$ParameterName]
    }
 
    Process {
        Enter-PSSession -ContainerId (Get-Container $Container).ID -RunAsAdministrator
    }
}

With that in place usage of the function looks like this (I type „enter-con“ followed by pressing tab to complete the Cmdlet / function name and then I press tab twice to get to the container I want)

Pretty quick and easy, right?

  1. I think that example code how to create a dynamic param can be found in multiple places and I couldn’t track down the origin, but the place where I found it is here