packaged script and commandline

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 11 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
bn2hunt
Posts: 101
Last visit: Mon Aug 02, 2021 12:04 pm

packaged script and commandline

Post by bn2hunt »

I am trying to figure out how to pass parameters to a packaged executable. I found the function(s) that were writen for primalforms 2009. It seems to work until I try to access the dictionary outside of the function Convert-ArgumentsToDictionary. When I try to access the dictionary object in the main script I get the error " Unable to index into an object of type system.collections.dictionaryentry." Any ideas?

Thanks
Dan


#===================================================================
# 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:ctstlog.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."
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

packaged script and commandline

Post by davidc »

Ok I see what is happening. PowerShell is converting the Dictionary to an array. Why it does this? I don't know.

The way to get around is to update the function so you pass the Dictionary as a parameter instead of returning it through the pipeline:

function Convert-ArgumentsToDictionary
{
Param( [ValidateNotNull()]
[System.Collections.Specialized.StringDictionary]$Dictionary,
[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++
}
}
$Dictionary[$key] = $value
}#else skip
}
}


$Arguments = Parse-Commandline $CommandLine
$Dictionary = New-Object System.Collections.Specialized.StringDictionary
Convert-ArgumentsToDictionary $Dictionary $Arguments '-'

David
David
SAPIEN Technologies, Inc.
This topic is 11 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.