Passing arrays or hashes into -File

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Max Kozlov Max Kozlov 2 months ago.

  • Author
    Posts
  • #70558
    Profile photo of Codeweavers
    Codeweavers
    Participant

    Hello,

    I'm trying to pass either an array or hash into Powershell from CruiseControl.net; I'm constrained on the way it is called based on how it's hardcoded into the PowershellTask. I need to basically make this work when executed from cmd or ccnet.

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -nologo -NoProfile -NonInteractive -file "C:\Test\Input.ps1" -Environment test -Overrides @{'asd'='net';'arguments'='broken'}

    In the script I've got:

    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true)]
        [String]$Environment,
        [Parameter(Mandatory=$false)]
        [Hashtable]$Overrides = @{}
    )
    

    When I try that command (through CMD or cc.net) I get:

     : Cannot process argument transformation on parameter 'Overrides'. Cannot convert the "@{'asd'='net';'arguments'='broken'}" value of type "System.String" to type "System.Collections.Hashtable".

    If I switch from -File to -Command it works. Unfortunately the -File is hardcoded in CCNet so I can't change it.

    I don't understand why it doesn't work, and why the behaviour is different between -File and -Command.

    Is what I want even possible?

    Thanks,
    Rob

  • #70574
    Profile photo of Daniel Krebs
    Daniel Krebs
    Moderator

    Thanks for your post. Please try the following:

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NoProfile -NonInteractive -Command "& C:\Test\Input.ps1 -Environment test -Overrides @{'asd'='net';'arguments'='broken'}"
    

    I hope that helps.

    – Daniel

  • #70577
    Profile photo of Codeweavers
    Codeweavers
    Participant

    Hi Daniel,

    Thank you for your reply. However as I said in my question:

    Unfortunately the -File is hardcoded in CCNet so I can't change it.

    So, I need it to work with -File not -Command

    Thanks,
    Rob

  • #71146
    Profile photo of Codeweavers
    Codeweavers
    Participant

    Hello,

    I've been trying more and more things to do this.

    I have a feeling it's impossible to pass anything but simple strings into a

    powershell -File

    execution, can someone confirm? Unfortunately I can't change the execution from -File to -Command as it's hard-coded in cc.net 🙁

    Thanks,
    Rob

  • #71156
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    Afaik this is true
    but you can write a "proxy"

    param(
        [Parameter(Mandatory=$true)]
        [String]$Environment,
        [Parameter(Mandatory=$false)]
        #[Hashtable]$Overrides = @{}
    	[string]$Overrides = ''
    )
    
    "Environment: $Environment"
    "Overrides:"
    $Overrides
    $Overrides1 = & ([ScriptBlock]::Create($Overrides))
    $Overrides1
    #here you can call your needed file with transformed parameters
    

You must be logged in to reply to this topic.