Multiple foreach commands [SOLVED]

by Tadziz at 2013-02-12 07:51:46

Hello,
how can i run foreach in foreach

foreach ($file in $files) {



foreach ($path in $dirsD {

# here i need to run a commd to copy files to multiple directories from
# [string]$path = "C:", "\network\share", etc

}

}



How can i do this ? Thanks, Tadas.
by poshoholic at 2013-02-12 08:03:18
Hello,

Can you share more details on what you’re specifically trying to do? You can use nested foreach statements in PowerShell. But without the specific details of what you are trying to do I can’t help you.
by Tadziz at 2013-02-12 23:58:26
hi,
i will change my question. So i have the script which generates output to different files, like file1, file2, file3, file4, file5. Now i want to copy each this file to multiple directories: dir1, dir2 , dir3.


$pathwherearefiles = "C:"
#paths where files need to copied
$path = "\share01\directory", "\share02", "C:\Files"

#so im tring to run for each to copy files to these dirs
foreach ($files in $file )

#to copy files i use eseutil.exe but it does not work
eseutil.exe $pathwherearefiles$files $path$files

}



Question.
how can i make this work or how to copy to multiple directories at once.

Thanks, Tadas
by mjolinor at 2013-02-13 05:31:29
Generally, what you are describing would look something like this:

$pathwherearefiles = "C:"
#paths where files need to copied
$paths = "\share01\directory", "\share02", "C:\Files"

$files = Get-ChildItem -Path $pathwherearefiles |
where {-not($_.psiscontainer)}

foreach ($file in $files) {

foreach ($path in $paths){

copy-item -Path $file.fullname -Destination $path

}
}




I find the statement that you’re using exeutil.exe to copy files somewhat disturbing. Eseutil is a utility for maintaining/repairing Exchange mailbox databases…
by Tadziz at 2013-02-13 05:54:27
[quote]I find the statement that you’re using exeutil.exe to copy files somewhat disturbing. Eseutil is a utility for maintaining/repairing Exchange mailbox databases…[/quote]
Yes, you are right, but also it can copy files and i read that it can copy very large files very well :slight_smile:
by mjolinor at 2013-02-13 06:42:37
What kind of files are you copying? I find this in the docs for eseutil:

"The ability of Eseutil to copy large files is a new capability introduced in Exchange Server 2003 that has been imported from ESEFile. The copy file mode is optimized to copy very large files efficiently. You can use the switch to copy a database, streaming file, or log file. However, the mode is not suited as a general purpose copy utility. "
by Tadziz at 2013-02-13 07:14:42
I will copy MS SQL Datbase “bak” files. The size is 57 GB and growing. I tried robocopy with key /z but it gets very slow. Maybe you have other suggestions how to copy large files from powershell over wan.

p.s.
whats wrong with this part of code


$s = New-Object (‘Microsoft.SqlServer.Management.Smo.Server’) “pvt-2012”

#path where to backup database
$path = ‘C:\Install’

$dbs = $s.Databases

foreach ($db in $dbs) {

if(($db.name -eq “model”) -and ($db.name -eq “master”)) {

#run backup just for model and master datbase
# but it does only model database backup
# whats wrong :slight_smile: ?

}


}


by Klaas at 2013-02-13 07:20:46
How can a database name be “model” and “master”? You can have both databases if you use -Or.
by Tadziz at 2013-02-13 07:24:48
It’s a two different databases, i only want to backup them.
by poshoholic at 2013-02-13 07:27:55
Instead of this:
if(($db.name -eq “model”) -and ($db.name -eq “master”)) {
do this:
if (@(‘model’,‘master’) -contains $db.name) {
by mjolinor at 2013-02-13 07:30:38
I’d use BITS for copying a large file across a WAN.
by Tadziz at 2013-02-13 07:32:10
[quote=“poshoholic”]Instead of this:
if(($db.name -eq “model”) -and ($db.name -eq “master”)) {
do this:
if (@(‘model’,‘master’) -contains $db.name) {[/quote]

it only backup up model database. if i change for testing master to msdb, only msdb database is being backuped.
by Klaas at 2013-02-13 07:50:16
Maybe we can see the problem if you post the entire backup part of your script.
by mjolinor at 2013-02-13 08:08:28
Now we’ve got two questions going at once.

"Maybe you have other suggestions how to copy large files from powershell over wan."

I would use BITS for that…
by Tadziz at 2013-02-13 08:14:58
Mark this SOLVED. Because i got the answer to my first question. I will ask in other post for other problems