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 6 months, 3 weeks ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • 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.



    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
    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 🙂

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.