在经历了大量的头痛之后,我能够使这几乎成功运作。
问题:在错误/输出中,Robocopy似乎将$ args [4](ref:$ sourcePath)视为范围内的单个IP而不是仅仅是一个对象。
我假设其余语法正确,因为如果我将$ip = 101..119 | foreach { "192.168.1.$_" }
切换为$ip = "192.168.1.101"
,一切正常。
Robocopy作为$ ip范围内的所有IP地址转储到控制台-source中。我在这做错了什么?
#####################################################
#Purpose: to ping an IP range of network locations in a loop until successful. When successful, copy new files from source onto storage.
#Each ping and transfer needs to be ran individually and simultaneously due to time constraints.
#####################################################
#define the IP range, source path & credentials, and storage path
$ip = 101..119 | foreach { "192.168.1.$_" }
#$ip = "192.168.1.101" #everything works if I comment above and uncomment here
$source = "\\$ip"
$sourcePath = "$source\admin\"
$dest = "C:\Storage\"
$destFull = "$dest$ip\"
$username = "user"
$password = "password"
#This is how to test connection. Once returns TRUE, then copy new files only from source to destination.
#copy all subdirectories & files in restartable mode
foreach ($src in $ip){
Start-Job -ScriptBlock {
DO {$ping = Test-Connection $args[0] -BufferSize 16 -Count 4 -quiet}
until ($ping)
net use \\$args[1] $args[2] /USER:$args[3]
robocopy $args[4] $args[5] /E /Z
} -ArgumentList $src, $source, $password, $username, $sourcePath, $destFull -Name "$src" #pass arguments to Start-Job's scriptblock
}
#get all jobs in session, supress command prompt until all jobs are complete. Then once complete, get the results.
Get-Job | Wait-Job
Get-Job | Receive-Job
答案 0 :(得分:1)
在你创建$ source的时候,$ ip是一个数组,所以$ source最终是一个连接所有项目的很长的字符串:
\\192.168.1.101 192.168.1.102 192.168.1.103 192.168.1.104 ...
您可以通过仅运行这两行,然后检查$ source:
的内容来自行查看$ip = 101..119 | foreach { "192.168.1.$_" }
$source = "\\$ip"
这对$ sourcePath有一个连锁效应,在你对RoboCopy的调用中用作$ args [4]。你应该在你的foreach循环中构建你的路径,你可以从$ ip集合访问每个IP地址($ src)。
答案 1 :(得分:0)
某些消息来源/等。是不同的,但这只是由于测试环境。我决定使用[io.path]作为路径,因为我在定义变量时遇到$args
的问题。
感谢boxdog提供上述帮助。我完全忽略了这个事实。
$ScriptBlock = {
$source = [io.path]::Combine('\\',$args[0])
$sourcePath = [io.path]::Combine('\\',$args[0],'c$','admin\')
$dest = "C:\Storage\"
$destFull = [io.path]::Combine($dest,$args[0])
DO {$ping = Test-Connection $args[0] -BufferSize 16 -Count 1 -quiet}
until ($ping)
net use $source password /USER:user
robocopy $sourcePath $destFull /E /Z
}
$ip = 101..119 | foreach { "192.168.1.$_" }
foreach ($dvr in $ip){
Start-Job $ScriptBlock -ArgumentList $dvr
}
Get-Job | Wait-Job
Get-Job | Receive-Job