locked
How can i stop the quotes being added...? RRS feed

  • Question

  • Hi,
    I have written a powershell script which is importing data from a csv file, and setting the required settings for some calendar resources. However, I am encountering a problem with a field containing userid's seperated by commas when passed to a command as a variable is being enclosed automatically in quotes.

    I can replicate the problem with a three line powershell script containing the following;

    $ConfRoomAlias = "ConfRoomTest1"
    $ConfRoomDelegates = "user1,user2"
    set-calendarprocessing $ConfRoomAlias -ResourceDelegates $ConfRoomDelegates

    This returnes the following error;
    Couldn't find object "user1,user2". Please make sure that it was spelled correctly or specify a different object.
        + CategoryInfo          : NotSpecified: (0:Int32) [Set-CalendarProcessing], ManagementObjectNotFoundException
        + FullyQualifiedErrorId : 1E087ED5,Microsoft.Exchange.Management.StoreTasks.SetCalendarProcessing

    I can replicate this issue by entering manually into powershell the following line;
    set-calendarprocessing ConfRoomTest1 -ResourceDelegates "user1,user2"
    This will return the same error.

    This command is what i actually want (note: there are no quotes around the two userid's);
    set-calendarprocessing ConfRoomTest1 -ResourceDelegates user1,user2
    This will correctly set the resource delegates for this calendar.

    My script works perfectly if there is only 1 userid in the delegates list. But as soon as there are multiple seperated with the comma, then powershell adds the quotes to the command, and everything breaks.

    How can i get powershell to pass the string to the command line without adding the quotes?

    I have tried all sorts of string manipultations, adding extra quotes, adding escape characters, (\,), etc, etc, etc... but I can't get it to pass the string in a way the command will accept it.

    After two days scratching my head, I need some help before i have no hair left... ;-)

    Thanks

    Mark.

    Tuesday, January 24, 2012 3:32 PM

Answers

  • Hi,

    This line:

    $ConfRoomDelegates = "user1,user2"

    Creates a single string. That's not an array. If you want an array, you can write:

    $ConfRoomDelegates = "user1","user2"

    Bill

    • Proposed as answer by Bigteddy Tuesday, January 24, 2012 4:28 PM
    • Marked as answer by Mark Snell Wednesday, January 25, 2012 12:11 PM
    Tuesday, January 24, 2012 3:37 PM
  • Yes, you do want an array and Bill's answer is correct.  (Just to be sure, I just tested it.)  Remember, PowerShell, unlike a Unix shell script, is object-oriented so you do want to pass an array.

    You could construct a command as in:

    $ConfRoomAlias = "ConfRoomTest1"
    $ConfRoomDelegates = "user1,user2"
    $Command = "Set-CalendarProcessing -Identity " + $ConfRoomAlias + " -ResourceDelegates " + $ConfRoomDelegates

    Invoke-Expression -Command $Command

    but that seems like more work to me.


    Ed Crowley MVP "There are seldom good technological solutions to behavioral problems."
    • Marked as answer by Mark Snell Wednesday, January 25, 2012 12:11 PM
    Tuesday, January 24, 2012 4:13 PM
  • What about:
    $ConfRoomAlias = "ConfRoomTest1"
     $ConfRoomDelegates = "user1,user2" -split ','
     set-calendarprocessing $ConfRoomAlias -ResourceDelegates $ConfRoomDelegates
    


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Network Live Audit - Powershell script
    • Marked as answer by Mark Snell Wednesday, January 25, 2012 12:11 PM
    Tuesday, January 24, 2012 4:43 PM

All replies

  • Hi,

    This line:

    $ConfRoomDelegates = "user1,user2"

    Creates a single string. That's not an array. If you want an array, you can write:

    $ConfRoomDelegates = "user1","user2"

    Bill

    • Proposed as answer by Bigteddy Tuesday, January 24, 2012 4:28 PM
    • Marked as answer by Mark Snell Wednesday, January 25, 2012 12:11 PM
    Tuesday, January 24, 2012 3:37 PM
  • Hi,

    I don't want an array - i want the data as a single string being passed to the command line, without the quotes...

    I did try what you are suggesting - but the command doesn't accept this syntax...

    Regards

    Mark.

     

    Tuesday, January 24, 2012 3:42 PM
  • I think Bill is right.  It is unusual for a cmdlet to accept multiple parameters in a string.  They are normally passed as an array.  Consider:

    "test1.txt", "test2.txt" | Get-ChildItem


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Network Live Audit - Powershell script
    Tuesday, January 24, 2012 3:54 PM
  • Yes, you do want an array and Bill's answer is correct.  (Just to be sure, I just tested it.)  Remember, PowerShell, unlike a Unix shell script, is object-oriented so you do want to pass an array.

    You could construct a command as in:

    $ConfRoomAlias = "ConfRoomTest1"
    $ConfRoomDelegates = "user1,user2"
    $Command = "Set-CalendarProcessing -Identity " + $ConfRoomAlias + " -ResourceDelegates " + $ConfRoomDelegates

    Invoke-Expression -Command $Command

    but that seems like more work to me.


    Ed Crowley MVP "There are seldom good technological solutions to behavioral problems."
    • Marked as answer by Mark Snell Wednesday, January 25, 2012 12:11 PM
    Tuesday, January 24, 2012 4:13 PM
  • Indeed - when runnign this command in powershell;
    set-calendarprocessing ConfRoomTest1 -ResourceDelegates "user1", "user2"
    It does accept it.

    So i will have to split the string into an array, and then try to present each item on the command line.

    It is actually not as easy as it sounds - because i cannot combine the entries into a string, as then it will put quotes around everything again... so i need to somehow do something like;

    set-calendarprocessing ConfRoomTest1 -ResourceDelegates $ConfDelegate[0], $ConfDelegate[1], etc, etc...

    However, i have the problem that i don't know how many entries there will be, so will end up with a load of , , , , at the end, which powershell will not allow...

    Mark.

    Tuesday, January 24, 2012 4:40 PM
  • What about:
    $ConfRoomAlias = "ConfRoomTest1"
     $ConfRoomDelegates = "user1,user2" -split ','
     set-calendarprocessing $ConfRoomAlias -ResourceDelegates $ConfRoomDelegates
    


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Network Live Audit - Powershell script
    • Marked as answer by Mark Snell Wednesday, January 25, 2012 12:11 PM
    Tuesday, January 24, 2012 4:43 PM
  • Tried that too... ;-)

    The split removes the comma and creates the array.

    The command doesn't like it, as there must be a comma between the multiple entries...

    I even tried recombining the entries with all sort of syntax, but to no avail... powershell pops those quotes around the whole thing and then the command cannot find the userid...

    Any other ideas?

    Thanks

    Mark.

    Tuesday, January 24, 2012 4:48 PM
  • My post of a little more than an hour ago shows how to construct the command and execute it.  That probably will work for your situation if you don't want to break up the string into an array.

    Bigteddy's method works as well; I just tested it in my lab. 

    If you're seeing it not work, post your complete syntax and the error message.


    Ed Crowley MVP "There are seldom good technological solutions to behavioral problems."

     



    Tuesday, January 24, 2012 5:25 PM
  • The command doesn't like it, as there must be a comma between the multiple entries...

    You need to be more specific about what this means.

    I don't have Exchange, but use this command:

    help set-calendarprocessing

    You should see a parameter like this:

    -CalendarProcessing <string[]>

    The square braces [] indicate the parameter accepts an array.

    Bill

    Tuesday, January 24, 2012 5:37 PM
  • Thanks all for your help - indeed, i was doing something wrong.

    The answers about passing as an array, etc, were correct - i did even try the split command in my script, but clearly had something else not good at the time, as it wasn't working.

    A fresh day, and your guys help, the problem is indeed resolved.

    I have the following "solution" incorporated into the main script. Thanks.

     

    $ConfRoomAlias = "ConfRoomTest1"
    $ConfRoomModerators = "user1,user2,user3"

    $ConfRoomModerators = $ConfRoomModerators.Split(",")

    set-calendarprocessing $ConfRoomAlias -ResourceDelegates $ConfRoomModerators

    Regards,

    Mark.


    • Edited by Mark Snell Wednesday, January 25, 2012 12:17 PM
    Wednesday, January 25, 2012 12:17 PM