0

I'm trying to create a script that connects to various servers, should attach a PSDrive and copy over files. The problem resides in that I an unable to pass the variable into the Invoke-Command script-block.

workflow kopijobb {
    param ([string[]]$serverList, $creds, $basePath)

    foreach -parallel ($server in $serverList){

        # Use the sequence keyword, to ensure everything inside of it runs in order on each computer.
        sequence {

            #Use the inlinescript keyword to allow PowerShell workflow to run regular PowerShell cmdlets
            inlineScript{

                $path = $using:basePath

                Write-Host "Starting $using:server using $path"
                #Create session for New-PSSession
                $session = New-PSSession -ComputerName $using:server -Credential $using:creds

                # Copy Java and recreate symlink
                Invoke-Command -Session $session -ScriptBlock {

                    # Make a PSDrive, since directly copying from UNC-path doesn't work due to credential-issues
                    New-PSDrive -Name N -PSProvider FileSystem -root $using:path -Credential $using:creds | out-null

I pass the network path to $basePath, and I'm able to read it inside the inlineScript block (where I have tried storing it in a new variable to test), but once I try accessing it in the New-PSDrive command, the variable is suddenly empty/unreachable, and the mounting of the drive fails with the error Cannot bind argument to parameter 'Root' because it is null.

I'm at a loss to why this fails, so I'm turning to the collective wisdom here instead.

4
  • Does it work using a hardcoded path? Commented May 8, 2018 at 11:27
  • Yes, problem is, then the $using:creds variable becomes the bottleneck in New-PSDrive (can't access the drive without logging in). :( Commented May 8, 2018 at 11:45
  • I am afraid that handing credentials over to the other machine using $creds does not work by design. So you are absolutely right to hand over user and pwd seperately. Dont use $using outside the invoke-block. It is meant to be used inside the script block to access the variables from the calling maching. Commented May 9, 2018 at 13:12
  • Passing credentials as an argument worked fine. As for the $using, I tried all kinds of variations, but my solution below was the only way I got it to work.once I finish the other projects I'm working on I will revisit this script to see if I can improve on it. Commented May 9, 2018 at 15:48

1 Answer 1

2

If feels embarrassing to answer my own question, especially on the same day, but I bumped into a PowerShell guru at work and he took one glance at the script and saw the problem:

I had to add -Args to the Invoke-Command

            Invoke-Command -Session $session -ScriptBlock {
                param($srv,$login,$path,$...)

                #Make a PSDrive, since directly copying from UNC-path doesn't work due to credential-issues
                New-PSDrive -Name N -PSProvider FileSystem -root $path -Credential $login | out-null
            } -Args $using:server,$using:creds,$using:basePath,$using:...

This does of course mean that I had to import all the needed arguments from the top level into the workflow, and then into the Invoke-Command.

Sign up to request clarification or add additional context in comments.

3 Comments

You should be able to utilize the $using: scope directly in your scriptblock without the need for an arglist.
Should? Maybe. Could? Not a chance! I tried all kinds of variations, nothing else worked. It's like the scriptBlock was isolated from everything before it.
@MaximilianBurszley I have completely restructured my code into Print Driven Development mode and I can assure you that variables pass from Workflow to InlineScript but they don't pass from InlineScript into Invoke-Command even if I create a new variable in the InlineScript scope using $Using a variable from the outer (Workflow) scope, and then I try to use that InlineScript scope variable with $Using in the Invoke-Command scope. (Also, that is -ArgumentList for me, not -Args)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.