I have a file on a server that I want to copy to a list of hosts on the domain. I use the following script to populate a .txt file that lists connected hosts:
@echo off
setlocal EnableDelayedExpansion
set "xNext="
set "xComputer="
for /f %%A in ('net view /all') do (
set "xComputer=%%~A"
if "!xComputer:~0,2!"=="\\" for /f "tokens=2,* delims=. " %%X in ('nslookup %%A') do (
if "!xNext!"=="1" (
echo.!xComputer! = %%X.%%Y
set "xNext=0"
)
if "!xComputer:~2!"=="%%~X" set "xNext=1"
)
)
endlocal
pause
I then strip the IP's after using them for other purposes to leave a .txt file that lists the hosts, line by line, like this:
\\Host1
\\Host2
\\Host3
...
I have tried the following:
Get-Content 'path\computers.txt' | Foreach-Object start /B {robocopy 'C:\Users\User\Desktop\copyfile' '\\$_\users\user\desktop' }
With the following error:
ERROR 53 (0x00000035) Accessing Destination Directory \\$_\Users\user\Desktop\
The network path was not found.
What have I messed up?
start /b
is not a poper parameter for Foreach-object
. Also variables are not expanded in single quotes.
Get-Content 'path\computers.txt' | Foreach-Object { robocopy C:\Users\User\Desktop\copyfile \\$_\users\user\desktop }
I don’t know why, but it’s complaining about the trailing backslash on the destination path.
User contributions licensed under CC BY-SA 3.0