Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

Powershell variables and CMD /C

In an effort to improve my scripting I am trying to move more towards PowerShell but failing horribly in places (this is one of them). I had an old .bat script I used to distribute and replace files to users folders:
Robocopy C:\Policies\Current D:\Users\Technical\mike\Documents\NewPolicies /COPY:DAT /E /Z /B /R:5 /W:1 /PURGE
Robocopy C:\Policies\Current D:\Users\Technical\Ben\Documents\NewPolicies /COPY:DAT /E /Z /B /R:5 /W:1 /PURGE
Robocopy C:\Policies\Current D:\Users\Sales\Sam\Documents\NewPolicies /COPY:DAT /E /Z /B /R:5 /W:1 /PURGE
Downside is every time their is a new user I have to add a line to the script
So I thought a powershell script and ForEach-Object would be a better way to do things but I am struggling
So far I have:
Get-ChildItem -Path D:\Users\Sales | ForEach-Object -Process { $_.Name }
Which gives me the name of each user and I know to use cmd /c "Robocopy ....." to run robocopy in a cmd environment but getting the D:\Users\Sales\ bit in before and the \Documents\NewPolicies and getting a loop out of it is evading me. Any suggestions welcome (plus will using cmd /C make any variable I create null and void?) and let me know if you need more information
0
Dead_Eyes
Asked:
Dead_Eyes
  • 5
  • 2
1 Solution
 
Dead_EyesAuthor Commented:
Ok making a little progress:
$source = "D:\Source"
$Destinations = "D:\test\folder 1","D:\test\folder 2","D:\test\folder 3","D:\test\SimpleFolder1","D:\test\SimpleFolder2","D:\test\SimpleFolder3"
foreach($target in $Destinations){
robocopy $source $target /COPY:DAT /E /Z /B /R:5 /W:1 /PURGE
}
Now my only issue is $destinations. Any suggestions?
0
 
Dead_EyesAuthor Commented:
Simplified how to I turn this:
$var1 = Get-ChildItem -Path D:\Test | ForEach-Object -Process { $_.Name }
$var1

which outputs like this:

PS C:\WINDOWS\system32> $var1
Folder 1
Folder 2
Folder 3
SimpleFolder1
SimpleFolder2
SimpleFolder3

into a variable looking like this:

"D:\test\folder 1\New","D:\test\folder 2\New","D:\test\folder 3\New","D:\test\SimpleFolder1\New","D:\test\SimpleFolder2\New","D:\test\SimpleFolder3\New"
0
 
Dead_EyesAuthor Commented:
Ok getting there, it now runs but no action....help!

$source = "D:\Source"
$Destinations = Get-ChildItem -Path D:\Test | ForEach-Object -Process { $_.Name }
foreach($target in $Destinations){
" robocopy $source 'D:\test\$target\new' /COPY:DAT /E /Z /B /R:5 /W:1 /PURGE "
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
QlemoC++ DeveloperCommented:
As you have correctly concluded, you do not need to use cmd /c.

Regarding the folder structure, you should make sure to get only folders (? is the same as where-object and % as foreach-object):
Get-ChildItem D:\Users\Technical | ? { $_.PsIsContainer }

Open in new window

Using that to generate the target folder:
Get-ChildItem D:\Test | ? { $_.PsIsContainer } | % {
   Robocopy C:\Policies\Current "$($_.FullName)\Documents\NewPolicies" /COPY:DAT /E /Z /B /R:5 /W:1 /PURGE
}

Open in new window

Note that in double quotes, variables as $X and expressions like $(...) will be evaluated and replaced with the result. But "$x.y" will expand "$x", and append ".y" as a string to it, instead of getting the property y of x.
0
 
QlemoC++ DeveloperCommented:
http:#a40379642  creates a string with the command, there is no executing of that string.
0
 
Dead_EyesAuthor Commented:
Done :) hope this helps someone out:
$source = "D:\Source"
$Destinations = Get-ChildItem -Path D:\Test | ForEach-Object -Process { $_.Name }
ForEach ($target in $Destinations) {
cmd /c "" robocopy $source "D:\test\$target\new" /COPY:DAT /E /Z /B /R:5 /W:1 /PURGE ""
}
0
 
Dead_EyesAuthor Commented:
Probably a better way than my final conclusion
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now