Solved

Powershell variables and CMD /C

Posted on 2014-10-14
7
215 Views
Last Modified: 2014-10-14
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
Comment
Question by:Dead_Eyes
  • 5
  • 2
7 Comments
 

Author Comment

by:Dead_Eyes
ID: 40379585
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
 

Author Comment

by:Dead_Eyes
ID: 40379592
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
 

Author Comment

by:Dead_Eyes
ID: 40379642
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40379648
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
 
LVL 69

Expert Comment

by:Qlemo
ID: 40379651
http:#a40379642  creates a string with the command, there is no executing of that string.
0
 

Author Comment

by:Dead_Eyes
ID: 40379653
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
 

Author Closing Comment

by:Dead_Eyes
ID: 40379656
Probably a better way than my final conclusion
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question