locked
ParameterSetName does not workwith Invoke-Command and ArgumentList? RRS feed

  • Question

  • I am writing scripts to monitor disk usage based on scripts from msdn code gallery:

    https://social.technet.microsoft.com/Forums/scriptcenter/en-US/1a1e47c2-635f-4572-a556-e0d6cc964af8/how-to-analyze-the-powershell-console-output-with-tools-such-as-excel-and-access-etc?forum=ITCG#c8d40235-deae-4a15-9d51-9d0f89d7f0a5

    I need to execute the script on multiple servers with PowerShell remote session. One of the problem I got is that it looks like ParameterSetName does not work with ArgumentList.

    What follows is the simple test script I used. it's based on the parameter definition of Get-DirStats.ps1, I just added positions for Switch parameters because I don't know how to pass named parameter with ArgumentList.

    [CmdletBinding(DefaultParameterSetName="Path")]
    param(
      [parameter(Position=0,Mandatory=$false,ParameterSetName="Path",ValueFromPipeline=$true)]
        $Path=(get-location).Path,
      [parameter(Position=0,Mandatory=$true,ParameterSetName="LiteralPath")]
        [String[]] $LiteralPath,
      [parameter(Position=1,Mandatory=$false)]
        [Switch] $Only,
      [parameter(Position=2,Mandatory=$false)]
        [Switch] $Every,
      [parameter(Position=3,Mandatory=$false)]
        [Switch] $FormatNumbers,
      [parameter(Position=4,Mandatory=$false)]
        [Switch] $Total
    )
    
    Write-host "ParameterSetName:" $PSCmdlet.ParameterSetName
    Write-host "Path:" $Path
    Write-host "LiteralPaht:" $LiteralPath
    Write-host "Only:" $Only

    What follows in the screenshot for test result:


    Another related issue is I use Get-ChildItem2 to overcome the 260 character limit in path. It does not work in powershell remote session for me. I asked the question https://gallery.technet.microsoft.com/scriptcenter/Get-ChildItemV2-to-list-29291aae/view/Discussions
    • Edited by GuYuming Monday, October 15, 2018 7:50 AM
    • Moved by Bill_Stewart Thursday, December 20, 2018 9:14 PM This is not "debug/fix/rewrite my script for me" forum
    Monday, October 15, 2018 6:35 AM

All replies

  • Hey,

    I copied your test script and ran it against a few servers and it correctly returned the ParameterSetName each time.

    What happens if you run that directly on the target machine you are testing against rather than through invoke?

    Tuesday, October 16, 2018 2:29 AM
  • it works without PowerShell remote session of cause. But if it cannot work with remote session, I have to configure schedule to run it on every machine.
    Tuesday, October 16, 2018 7:48 AM
  • Hmmm, something else strange is happening though. I just ran that code remotely (*exactly* as you are) against 50 servers and they all returned Parameter set "Path". In your screen shot above your invoke is returning a value for both path and literalpath. That should not be possible.
    Tuesday, October 16, 2018 8:51 PM