powershell passing dictionary object

Posted on 2012-09-19
Last Modified: 2012-09-24
I am writing a powershell script that will be packaged using Sapian powershell studio.  According to their documentation to pass arguments to the packaged script you use the following command line "scriptname.exe -parameter1 value -parameter2 value"  I found an example of a function to put the parameters and associated values into a dictionary object.  It works great in the function but when I try to pass the arguments back to the main script I am unable to access the dictionary object.  Any ideas on how I would access the dictionary from my main script.  

Thanks for any help

# Created with: SAPIEN Technologies, Inc., PowerShell Studio 2012 v3.0.8
# Created on:   9/18/2012 3:15 PM
# Created by:   dan-w
# Organization: 
# Filename:     

function Parse-Commandline 
    $Arguments = New-Object System.Collections.Specialized.StringCollection 
    #Find First Quote 
    $index = $CommandLine.IndexOf('"')

    while ( $index -ne -1) 
    {#Continue as along as we find a quote 
        #Find Closing Quote 
        $closeIndex = $CommandLine.IndexOf('"',$index + 1) 
    #$closeIndex = $CommandLine.IndexOf(‘"’,$index + 1) 
        if($closeIndex -eq -1) 
            break #Can’t find a match 
        $value = $CommandLine.Substring($index + 1,$closeIndex – ($index + 2)) 
        $index = $closeIndex

        #Find First Quote 
        $index = $CommandLine.IndexOf('"',$index + 1) 
    return $Arguments 

function Convert-ArgumentsToDictionary 
    Param([System.Collections.Specialized.StringCollection] $Params, [char] $ParamIndicator) 
    for($index = 0; $index -lt $Params.Count; $index++) 
        [string]$param = $Params[$index] 
        #Clear the values 
        $key = "" 
        $value = ""

            #Remove the indicator 
            $key = $param.Remove(0,1) 
            if($index  + 1 -lt $Params.Count) 
                #Check if the next Argument is a parameter 
                [string]$param = $Params[$index + 1] 
                if($param.StartsWith($ParamIndicator) -ne $true ) 
                    #If it isn’t a parameter then set it as the value 
                    $value = $param 
        }#else skip 
    # returns the expected value
    write-host $arg_Dictionary["fid1"]    
    return $arg_Dictionary
$logfile = "c:\c\tstlog.txt"
New-Item $logfile -Type file -Force |Out-Null
$arg_Dictionary = New-Object System.Collections.Specialized.StringDictionary 
#Verify that the $CommandLine variable exists 

#Verify that the $CommandLine variable exists 
if($CommandLine -ne $null -and $CommandLine -ne "") 
    $Arguments = Parse-Commandline $CommandLine 
    #Convert the Arguments. Use – as the Argument Indicator 
    $arg_Dictionary= Convert-ArgumentsToDictionary $Arguments ‘-’ 
    #gets an error Unable to index into an object of type system.collections.dictionaryentry.
    write-host $arg_Dictionary["fid1"]
    #Not running in a packager or no command line arguments passed 
    Write-Output "There are no command line arguments to parse." 

Open in new window

Question by:DanDWolff
    LVL 67

    Expert Comment

    Any reason you do not just define the arguments in your script, or use $arg?

    Author Comment

    $arg doesn't work with the packaged executable, worked great when I was running the .ps1 but I have to package it as an executable and run it with alternate credentials for the application I am writing the script for.  And $args isn't getting populated when the executable is called.
    LVL 67

    Accepted Solution

    Got you. But where is your ps1 code located then? The code above consists of the function definitions and some executable code - I guess for demonstrating the usage?
    Since I don't know how the Sapien stuff works, I can only make general suggestions.

    You can define $arg_Dictionary globally instead of script-locally:
    $global:arg_Dictionary = New-Object System.Collections.Specialized.StringDictionary

    Open in new window

    which allows access from everywhere, including inside your own code.
    Or call your packaged script with the generated $arg_Dictionary as parameter, and process the dictionary there.

    Author Comment

    Sapien got back to me on the problem and for some reason powershell was converting the dictionary object to an array.  To get around that they had me pass the dictionary object to the function and now it is working.  I can set my variables and use them with the rest of the script.

    Thanks for your help

    Author Comment

    I've requested that this question be closed as follows:

    Accepted answer: 0 points for DanDWolff's comment #a38414686

    for the following reason:

    Sapien got back to me on the problem and for some reason powershell was converting the dictionary object to an array.

    Author Closing Comment

    this answer was close but rather than declare it as a global variable, I just needed to pass the dictionary object to the function.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Suggested Solutions

    This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
    This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now