Powershell Robocopy 从服务器到多台主机
Powershell Robocopy from server to mulitple hosts
我在服务器上有一个文件,我想将其复制到域中的主机列表中。我使用以下脚本来填充列出已连接主机的 .txt 文件:
@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
然后我在将 IP 用于其他目的后删除 IP,留下一个逐行列出主机的 .txt 文件,如下所示:
\Host1
\Host2
\Host3
...
我尝试了以下方法:
Get-Content 'path\computers.txt' | Foreach-Object start /B {robocopy 'C:\Users\User\Desktop\copyfile' '\$_\users\user\desktop' }
出现以下错误:
ERROR 53 (0x00000035) Accessing Destination Directory \$_\Users\user\Desktop\
The network path was not found.
我搞砸了什么?
start /b
不是 Foreach-object
的 poper 参数。变量也没有用单引号展开。
Get-Content 'path\computers.txt' | Foreach-Object { robocopy C:\Users\User\Desktop\copyfile \$_\users\user\desktop }
我不知道为什么,但它抱怨目标路径上的尾部反斜杠。
我在服务器上有一个文件,我想将其复制到域中的主机列表中。我使用以下脚本来填充列出已连接主机的 .txt 文件:
@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
然后我在将 IP 用于其他目的后删除 IP,留下一个逐行列出主机的 .txt 文件,如下所示:
\Host1
\Host2
\Host3
...
我尝试了以下方法:
Get-Content 'path\computers.txt' | Foreach-Object start /B {robocopy 'C:\Users\User\Desktop\copyfile' '\$_\users\user\desktop' }
出现以下错误:
ERROR 53 (0x00000035) Accessing Destination Directory \$_\Users\user\Desktop\
The network path was not found.
我搞砸了什么?
start /b
不是 Foreach-object
的 poper 参数。变量也没有用单引号展开。
Get-Content 'path\computers.txt' | Foreach-Object { robocopy C:\Users\User\Desktop\copyfile \$_\users\user\desktop }
我不知道为什么,但它抱怨目标路径上的尾部反斜杠。