Using computername within Invoke-command

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Stig Sörnsen Stig Sörnsen 1 year, 3 months ago.

  • Author
  • #36289
    Profile photo of Cory Cameron
    Cory Cameron
    Invoke-Command -Command {robocopy C:\Users \\Server\share\Backup\%computername%\Users /E /ZB} -ComputerName (Get-Content C:\Scripts\Computers.txt) -AsJob -Credential $cred -Authentication CredSSP

    This creates a folder called %computername% instead of a folder for each computer. How do I correct this and where should I be looking to find this answer? I'm usually pretty good at finding the answer, usually in the PS help, but not sure where to start on this one.



  • #36290
    Profile photo of Dave Wyatt
    Dave Wyatt

    %computername% is the old DOS way of getting at environment variables. In PowerShell, it's $env:computername

    Invoke-Command -Command {robocopy C:\Users \\Server\share\Backup\$env:computername\Users /E /ZB} -ComputerName (Get-Content C:\Scripts\Computers.txt) -AsJob -Credential $cred -Authentication CredSSP
  • #36291
    Profile photo of Stig Sörnsen
    Stig Sörnsen

    Powershell doesn't understand every command that Cmd does.
    Try to exchange your %computername% with $env:COMPUTERNAME

    Best regards

    Edit: Dave beat me to it 🙂

You must be logged in to reply to this topic.