import-csv scripting issue

Hi all,

I have an issue with using a csv as input for a script to add multiple workergroups to a xenapp published app. I have created a csv with a custom delimiter ";" because the paramater value needed to add multiple workergroups need to be seperated by a comma.

In my csv the exported data looks like this after running this command.

Get-XAApplicationReport * |where {($_.ApplicationType -ne"Content") -and($_.ApplicationType -ne"ServerDesktop") -and($_.Enabled -eq"True")} |selectBrowserName,@{n="WorkerGroups";e={[string]::join(",",$_.WorkerGroupNames)}},Folderpath | export-csv c:\workergroup.csv-Delimiter";"-NoTypeInformation  

CSV content:

"Test_APP_NAME";"CTX-WG-NewBuild-10.6,CTX-WG-NewBuild-10.6-Silo";"Applications/TEST"

When running the following script

$vars=Import-Csvc:\workergroups.csv-Delimiter";"-Header "BrowserName","WorkerGroups","FolderPath"

Foreach ($line in $Vars)

{

Set-XAApplication-BrowserName$line.BrowserName -WorkerGroupNames $line.WorkerGroups -Enabled$True

  } 

It wil interpret the "CTX-WG-NewBuild-10.6,CTX-WG-NewBuild-10.6-Silo" (without quotes) as a single name instead of 2 names seperated by the comma. Resulting in an error because that combined name ofcourse does not exsist. Running the command  manually wil work fine.

If i run trace-command "Trace-Command parameterbinding -Expression"  

Trace-Command parameterbinding -Expression {Set-XAApplication -BrowserName $line.BrowserName -WorkerGroupNames $line.WorkerGroups -Enabled $True} -PSHost

with the script i see the following :

DEBUG: ParameterBinding Information: 0 :                 Parameter and arg types the same, no coercion is needed.

DEBUG: ParameterBinding Information: 0 :             Adding scalar element of type String to array position 0

DEBUG: ParameterBinding Information: 0 :         Executing VALIDATION metadata: [System.Management.Automation.ValidateNotNullOrEmptyAttribute]

DEBUG: ParameterBinding Information: 0 :         BIND arg [System.String[]] to param [BrowserName] SUCCESSFUL

DEBUG: ParameterBinding Information: 0 :     BIND arg [CTX-WG-NewBuild-10.6,CTX-WG-NewBuild-10.6-Silo] to parameter [WorkerGroupNames]

DEBUG: ParameterBinding Information: 0 :         COERCE arg to [System.String[]]

DEBUG: ParameterBinding Information: 0 :             Trying to convert argument value from System.String to System.String[]

DEBUG: ParameterBinding Information: 0 :             ENCODING arg into collection

DEBUG: ParameterBinding Information: 0 :             Binding collection parameter WorkerGroupNames: argument type [String], parameter type [System.String[]],

Which results in:

Set-XAApplication : Cannot find worker group with name CTX-WG-NewBuild-10.6,CTX-WG-NewBuild-10.6-Silo (0x80000001 Failure.)
At line:1 char:45
+ Trace-Command parameterbinding -Expression {Set-XAApplication -BrowserName $line ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidResult: (CTX-WG-NewBuild...Build-10.6-Silo:String) [Set-XAApplication], CitrixException
    + FullyQualifiedErrorId : FindWorkerGroupByName,Citrix.XenApp.Commands.SetAppCmdlet
 

When running the same trace-command but with the parameters filled in manually all works fine and i see this:

Trace-Command parameterbinding -Expression {Set-XAApplication -BrowserName "Test_APP_NAME" -WorkerGroupNames CTX-WG-NewBuild-10.6,CTX-WG-NewBuild-10.6-Silo} -PSHost

DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [Set-XAApplication]
DEBUG: ParameterBinding Information: 0 :     BIND arg [Test_APP_Name] to parameter [BrowserName]
DEBUG: ParameterBinding Information: 0 :         COERCE arg to [System.String[]]
DEBUG: ParameterBinding Information: 0 :             Trying to convert argument value from System.String to System.String[]
DEBUG: ParameterBinding Information: 0 :             ENCODING arg into collection
DEBUG: ParameterBinding Information: 0 :             Binding collection parameter BrowserName: argument type [String], parameter type [System.String[]], coll
ection type Array, element type [System.String], coerceElementType
DEBUG: ParameterBinding Information: 0 :             Creating array with element type [System.String] and 1 elements
DEBUG: ParameterBinding Information: 0 :             Argument type String is not IList, treating this as scalar
DEBUG: ParameterBinding Information: 0 :             COERCE arg to [System.String]
DEBUG: ParameterBinding Information: 0 :                 Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 :             Adding scalar element of type String to array position 0
DEBUG: ParameterBinding Information: 0 :         Executing VALIDATION metadata: [System.Management.Automation.ValidateNotNullOrEmptyAttribute]
DEBUG: ParameterBinding Information: 0 :         BIND arg [System.String[]] to param [BrowserName] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 :     BIND arg [System.Object[]] to parameter [WorkerGroupNames]
DEBUG: ParameterBinding Information: 0 :         COERCE arg to [System.String[]]
DEBUG: ParameterBinding Information: 0 :             Trying to convert argument value from System.Object[] to System.String[]
DEBUG: ParameterBinding Information: 0 :             ENCODING arg into collection
DEBUG: ParameterBinding Information: 0 :             Binding collection parameter WorkerGroupNames: argument type [Object[]], parameter type [System.String[]
], collection type Array, element type [System.String], coerceElementType
DEBUG: ParameterBinding Information: 0 :             Arg is IList with 2 elements
DEBUG: ParameterBinding Information: 0 :             Creating array with element type [System.String] and 2 elements
DEBUG: ParameterBinding Information: 0 :             Argument type System.Object[] is IList
DEBUG: ParameterBinding Information: 0 :             COERCE collection element from type String to type System.String
DEBUG: ParameterBinding Information: 0 :             COERCE arg to [System.String]
DEBUG: ParameterBinding Information: 0 :                 Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 :             Adding element of type String to array position 0

The difference i see is that the later one is converted from a System.Object[] to System.String[] and the line that is used from the CSV is converted from a System.String to System.String[].

Can anyone give me a hint to whats going wrong and what i might be able to do to fix it?

September 14th, 2015 2:33am

You cannot use both commas and semicolons to delimit a file.  One or the other.  If you have a field that is a list then you will need to split the field to get the elements.

This is what your CSV will look like:

BrowserName,WorkerGroups,FolderPath
Test_APP_NAME,"CTX-WG-NewBuild-10.6,CTX-WG-NewBuild-10.6-Silo",Applications/TEST
Test_APP_NAME,"CTX-WG-NewBuild-10.6,CTX-WG-NewBuild-10.6-Silo",Applications/TEST
Test_APP_NAME,"CTX-WG-NewBuild-10.6,CTX-WG-NewBuild-10.6-Silo",Applications/TEST
Test_APP_NAME,"CTX-WG-NewBuild-10.6,CTX-WG-NewBuild-10.6-Silo",Applications/TEST

We would run the code like this:

Import-Csv Myfile.csv |
    ForEach-Object{
        $groups=$_.WorkerGroups.Split(',')
        # rest of code here
    }

Free Windows Admin Tool Kit Click here and download it now
September 14th, 2015 2:42am

If i use the split wont this remove the comma?

This isn't what i want. I want it to pass the value including the comma in that field of the csv as input for the -workergroupnames parameter.

Running the command with the workergroupname one by one isn't an option as you must include all the names you want to add or only the last command run will be used.


@Edit

BTW the csv passes the info out of the variable as i would like it  as you can see in the error but for some reason it's interpreted as 1 single value and the comma is ignored

Set-XAApplication : Cannot find worker group with name CTX-WG-NewBuild-10.6,CTX-WG-NewBuild-10.6-Silo (0x80000001 Failure.)

Eventually the WorkerGroups column will also have single values. we have some apps with only 1 workergroup.
September 14th, 2015 4:47am

Hi Mark,

alas ... jrv's response was totally correct. What Set-XAApplication wants is a list of strings. The input it receives when using Import-Csv is a single string that happens to have a comma inside.

Commas don't make lists though. When typing into the console, it will interprete it as a list unless you wrap it in quotes. Try using this line when running the command interactively:

-WorkerGroupNames "CTX-WG-NewBuild-10.6,CTX-WG-NewBuild-10.6-Silo"

As you can see, the same error as when running the script.

Using split, you separate one string into multiple strings. You do not need a comma for that (it's only to make it easy for you to input a list when typing into the console window).

This should work just fine (try it with a csv with a single entry - it should work well):

$vars = Import-Csv "c:\workergroups.csv" -Delimiter ";" -Header "BrowserName", "WorkerGroups", "FolderPath"

Foreach ($line in $Vars)
{
	Set-XAApplication -BrowserName $line.BrowserName -WorkerGroupNames $line.WorkerGroups.Split(",") -Enabled $True
}

Cheers,
Fred

Ps.: Generally speaking, if a person with 100+k points gives a piece of advice, it's generally worth considering it and giving it a try, rather than straight out dismissing it. While jrv might not be always the most gentle person on this mudball, he earned his score the hard way.

Free Windows Admin Tool Kit Click here and download it now
September 14th, 2015 9:45am

By "list" we mean array.  Om a command line this is an array one,two,three as well as  this 'one','two','three'.

Look at the help and you will see that the parameter is defined as [string[]] which means an array of strings.

September 14th, 2015 11:29am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics