New-PSSession in script keep spitting out 'conversion' Errors

When I run next command in powershell:

$test = New-PSSession -ComputerName localhost,localhost

it runs fine and create two new sessions.

Here under, you can find the script code I have that keeps sending me errors.

function New-TempDirectory {
    [CmdletBinding(DefaultParameterSetName = 'ComputerName')]
    param (
        [Parameter( ValueFromPipeline,
                    ParameterSetName = "ComputerName",
                    Position = 0
        )]
        [string[]]
        $ComputerName = 'localhost',

        [Parameter( Mandatory,
                    ValueFromPipeline,
                    ParameterSetName = 'PSSession',
                    Position = 0
        )]
        [System.Management.Automation.Runspaces.PSSession]
        $PSSession
    )
    
    begin {
        Write-Verbose "[BEGIN  ] Starting: $($MyInvocation.Mycommand)"
    }
    
    process {
        if ($PSCmdlet.ParameterSetName -eq 'ComputerName') {
            Write-Verbose "ComputerName = $ComputerName"
            $PSSession = New-PSSession -ComputerName $ComputerName
        } # if ParameterSetName

       # Execution logic to write

    } # process
    
    end {
        Write-Verbose "[END    ] Ending: $($MyInvocation.Mycommand)"
    }
}

New-TempDirectory -ComputerName localhost, localhost -Verbose

And here is the output I keep getting (with said error message):

VERBOSE: [BEGIN  ] Starting: New-PBPTempDirectory
VERBOSE: ComputerName = localhost localhost
MetadataError: P:\PowerShell\Modules\PBPInstallSoftware\PBPInstallTestScript.ps1:27:13
Line |
  27 |              $PSSession = New-PSSession $ComputerName
     |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot convert the "System.Object[]" value of type "System.Object[]" to type      
     | "System.Management.Automation.Runspaces.PSSession".
VERBOSE: [END    ] Ending: New-PBPTempDirectory

Any help would be much appreciated.
Karsten

Karsten,
Welcome to the forum. :wave:t3:

I’m not completely sure if I got what you’re trying to do. Could it be an issue with your parameter declaration. You try to provide 2 elements for a parameter expecting only a single element? Have you tried to provide only one PSSession at a time?

Hi Olaf,

I’m trying to write a module with that function in it. The idea is to create a temp directory on 1 or more computers. Those computers can be given using the -ComputerName parameter which is typed as a string array, piped in, or can be given directly through by a PSSession array. Inside the function, if I get some ComputerNames, I use them to create PSSessions that are attibuted to a variable $PSSession. And that is where it goes wrong.

Hope this helps to clarify,
Karsten

Did you read more than just the first sentence of my reply? :wink:

When I use 1 computerName it does work.
As far as I can tell the New-PSSession accepts an array of type String with the -ComputerName parameter

-ComputerName <System.String>

That is why I don’t get it.

It’s because you hard typed the variable $PSSession

        [System.Management.Automation.Runspaces.PSSession]
        $PSSession

Use a different variable or remove the type

1 Like
        [System.Management.Automation.Runspaces.PSSession]
        $PSSession

Thank you krzydoug.
That was indeed the problem. I removed the type and it works like a charm.

Greetz,
Karsten

I’ve ran into the exact same thing some time ago. I assumed the variable being in a different parameter set that wasn’t in use would mean that typing wouldn’t be applied. Glad your function is working as expected now!