Invoke-Command,Copy-Item, and 'Path is null'

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Tech Gismo Tech Gismo 1 year, 5 months ago.

  • Author
  • #24802
    Profile photo of Tech Gismo
    Tech Gismo

    When I just run the command on this server 2012 R2 named: MemberServer:

    $f1 = "D:\Scripts\folder1"
    $f2 = "D:\Scripts\folder2"
    Copy-Item $f1 -Recurse -Destination $f2 -Verbose -Force

    Then PowerShell is happy, but putting in Invoke-Command, and so on, PowerShell is not happy, and I can not figure out what it wants I found in another post in here that it was expecting a file extension.
    So this confuse me a Little.

    $f1 = "D:\Scripts\folder1"
    $f2 = "D:\Scripts\folder2"
    Invoke-Command -ComputerName MemberServer -ScriptBlock {Copy-Item $f1 -Recurse -Destination $f2 -Verbose -Force}
    Cannot bind argument to parameter 'Path' because it is null.
        + CategoryInfo          : InvalidData: [:] [Copy-Item], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CopyItemCommand
        + PSComputerName        : MemberServer

    How can I avoid this in the future?

    Thanks for all your help.

    Kind regards

  • #24803
    Profile photo of Don Jones
    Don Jones

    Keep in mind that the Script Block in Invoke-Command [i]is passed without modification to the remote computer[/i]. So the remote computer is looking at $f1 and $f2 and it has no idea what those are, because those were defined back on your computer.

    What you can do inside the script block (v3 and later) is $using:f1 and $using:f2. That will 'copy' $f1 and $f2 from your local scope into the remote scope, so that the remote server knows what they are.

  • #24810
    Profile photo of Tech Gismo
    Tech Gismo

    Thank you for the help.

    I think I better understand how the invoke-command Works now.

You must be logged in to reply to this topic.