[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1992
  • Last Modified:

powershell passing dictionary object

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 
{ 
    Param([string]$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)) 
        [void]$Arguments.Add($value) 
        $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 = ""

        if($param.StartsWith($ParamIndicator)) 
        { 
            #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 
                    $index++ 
                } 
            } 
            $arg_Dictionary.add($key,$value)
        }#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"]
} 
else 
{ 
    #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

0
DanDWolff
Asked:
DanDWolff
  • 4
  • 2
1 Solution
 
QlemoC++ DeveloperCommented:
Any reason you do not just define the arguments in your script, or use $arg?
0
 
DanDWolffAuthor Commented:
$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.
0
 
QlemoC++ DeveloperCommented:
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.
0
A Cyber Security RX to Protect Your Organization

Join us on December 13th for a webinar to learn how medical providers can defend against malware with a cyber security "Rx" that supports a healthy technology adoption plan for every healthcare organization.

 
DanDWolffAuthor Commented:
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
0
 
DanDWolffAuthor Commented:
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.
0
 
DanDWolffAuthor Commented:
this answer was close but rather than declare it as a global variable, I just needed to pass the dictionary object to the function.

Thanks
0

Featured Post

 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now