Solved

Powershell variables and CMD /C

Posted on 2014-10-14
7
212 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
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 68

Expert Comment

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

Author Comment

by:Dead_Eyes
Comment Utility
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
Comment Utility
Probably a better way than my final conclusion
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
A procedure for exporting installed hotfix details of remote computers using powershell
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now