pipe variable to copy-item destination

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Wilfredo Perez Wilfredo Perez 1 year, 4 months ago.

  • Author
    Posts
  • #31753
    Profile photo of Derek Shirley
    Derek Shirley
    Participant

    I want to declare a variable with a list of computer names. For each one i want to stop a service copy a file to c:\windows\ and then start the service.

    One example i tried was:

    $target = get-content C:\comp2.txt
    foreach ($t in $target){
    stop-service -Name 'CDCA Multi Client';
    Copy-Item C:\multi.ini -Destination \\$T\c$\windows\;
    start-service -Name 'CDCA Multi Client'
    }

    Another way i tried was:
    $target = get-content C:\comp2.txt
    $target | ForEach-Object(Invoke-Command {stop-service 'CDCA Multi Client'}, Copy-Item C:\multi.ini -Destination \\$_.T\c$\windows\;

    what i cant figure out is how to use the data from the pipeline to set the destination path

  • #31757
    Profile photo of Wilfredo Perez
    Wilfredo Perez
    Participant

    Why do you have the semicolon at the end

    This worked:

    $target = get-content C:\comp2.txt
    foreach ($t in $target){
    Stop-Service -Name wuauserv
    Copy-Item C:\Test\ActOffice.txt "\\$target\c$\Test\"
    Get-Service
    Start-Service -Name wuauserv
    Get-Service
    }

    Using the parentheses in "\\$target\c$\Test\" will replace $target with the current computer name from the text file, that's how you can get the current object in the pipeline to set the destination path. No need for semicolon.

You must be logged in to reply to this topic.