0

I have a powershell script so basically I need to transfer files from multiple source to one local. I don't want to use pslurp in this case.

Basically, i get more than one remote source...error if i run my array in for loop, however it works in for each why?

Now the problem is I can't change my destination path as I have no idea where to let my Destination Array run in foreach loop. If I specify one source ip, it can loop to send to diff destination. So my array and loop technically is working fine.

Overall code (not working, got more thqn one remote source not supported error)

$ArrayIP=@("root@10.0.0.1","root@10.0.0.2")
$ArrayDestination=@("C:/Users/me/save01","C:/Users/me/save01"}

for (i=0; i -le2; i++){
pscp -pw testing -r $ArrayIP[i]":"/cf/conf/backup/* $ArrayDestination[i]
}

So I changed to FOREACH... but now I dont know how to let it save to different destination? Either way I specified one single destination for the sake of testing and it works. I am not getting the more than one remote source error anymore.

foreach ($IP in $ArrayIP){
pscp -pw testing -r $IP":"/cf/conf/backup/* <insert destination? dk how to make it run an arrayDestination>
}

Now I am thinking if i should do a 2D array...will that help me to run different variables in foreach loop? Or if anyone can guide me using the object command...I have read through forums about it but still not sure on how to use it

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • The end of line 2 has a syntax error. Line 4 has syntax errors, and line 5. – js2010 Mar 03 '20 at 17:35
  • sorry this isnt my actual code, and did not pay attention when retyping the overall coe out !! Thanks for pointing out! – Anonymous01 Mar 04 '20 at 03:43
  • I've rolled back your edit. It is not appropriate to add SOLVED to the title or add a solution to the question. You've indicated that your issue was solved by accepting an answer to it. You can find more information about how the site works in the [help]. – Ken White Mar 04 '20 at 03:52

3 Answers3

0

There are two things preventing your first sample from working:

First up, your current for loop isn't valid - variable names in PowerShell are prefixed with $:

for ($i=0; $i -le 2; $i++){
   ...
}

Second, PowerShell treats command line arguments as expandable strings, and an array index operation won't expand correctly in a string - enclose in a subexpression $() to correctly expand the referenced array index:

for ($i=0; $i -le 2; $i++){
    pscp -pw testing -r "$($ArrayIP[$i]):/cf/conf/backup/*" "$($ArrayDestination[$i])"
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • tq!!! this works! However since this is a testing, we actually need to extract a huge amount of servers to a local... So to make it more organised? Which method should i approach? Since i will be hving tons of array (for ip, dest, pw, username etc) it's kinda hard to keep track and i need to make sure this arrayDest[1] has to be for arrayIP[1] with arrayUser[1] if you get what i mean ... cuz if the info is not aligned...we will kinda ruin everything – Anonymous01 Mar 04 '20 at 01:55
  • @Anonymous01 I'd use a hashtable: `@{'root@10.0.0.1'="C:/Users/me/save01"}` then iterate over that – Mathias R. Jessen Mar 04 '20 at 11:26
0

You could try this, join the destination path and IP with a string value that won't conflict with the file name and split by that value inside the loop.

$Array=@("root@10.0.0.1--C:/Users/me/save01","root@10.0.0.2--C:/Users/me/save01")

foreach ($Entry in $ArrayIP){
    $Split = $Entry -split '--'
    pscp -pw testing -r ($Split[0] + ':/cf/conf/backup/*') $Split[1]
}
Dave
  • 63
  • 3
0

First time using Powershell? This comes close to the first example. Using @() to make arrays is a myth a lot of people fall for. I like using single quotes unless there's a variable inside.

$ArrayIP = 'root@10.0.0.1', 'root@10.0.0.2'
$ArrayDestination = 'C:/Users/me/save01', 'C:/Users/me/save01'

for ($i=0; $i -lt 2; $i++){
  pscp -pw testing -r ($ArrayIP[$i] + ':/cf/conf/backup/*') $ArrayDestination[$i]
}

I tested out the line with the pscp like this to make sure I got the strings right. Using other methods I ended up with extra spaces in between.

for ($i=0; $i -lt 2; $i++){
  cmd /c echo pscp -pw testing -r ($ArrayIP[$i] + ':/cf/conf/backup/*') $ArrayDestination[$i]
}

pscp -pw testing -r root@10.0.0.1:/cf/conf/backup/* C:/Users/me/save01
pscp -pw testing -r root@10.0.0.2:/cf/conf/backup/* C:/Users/me/save01

Here's something a little fancier with an array of hashes. Or you could import a csv.

$hashes = @{source='root@10.0.0.1:/cf/conf/backup/*'; destination='C:/Users/me/save01'},
          @{source='root@10.0.0.2:/cf/conf/backup/*'; destination='C:/Users/me/save01'}

foreach ($item in $hashes) {
  pscp -pw testing -r $item.source $item.destination
}

js2010
  • 23,033
  • 6
  • 64
  • 66