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

Welcome Forums General PowerShell Q&A Invoke-Command,Copy-Item, and 'Path is null'

This topic contains 2 replies, has 2 voices, and was last updated by

3 years, 9 months ago.

  • Author
  • #24802

    Points: 0
    Rank: Member

    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

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    Keep in mind that the Script Block in Invoke-Command is passed without modification to the remote computer. 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

    Points: 0
    Rank: Member

    Thank you for the help.

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

The topic ‘Invoke-Command,Copy-Item, and 'Path is null'’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort