Powershell possibilities?

XantchaXantcha Member Posts: 64 ■■□□□□□□□□
Hi all. Just wanted to get your opinions on something. I thought that seeing as I'll be starting a new job in november, I'll learn powershell to help me out. I read a thread here earlier about a book on learning powershell 'learn powershell in a month of lunches' and how good it was (and now I have it but only on chapter 2).

Today I got told that I should copy all the data on our fileserver using robocopy to our new fileserver that I'm setting up. I started looking at setting this up with robocopy but then I thought 'is there a way to do it via powershell'? Seeing as I want to learn it, I thought I could do it this way and learn at the same time.

The question is, is it possible to do it and then do a daily synchronisation too? The more I read about PS the more I see how powerful it really is and I think it's possible but I just wanted to make sure.

Thanks

Comments

  • EveryoneEveryone Member Posts: 1,661
    Yes it is possible. Obviously it will take you longer to figure out how to write a powershell script to do this, than it would to just use robocopy, but it can be done.

    Something like this...


    $destination = <where you want the files to get copied to>
    $data = Get-ChildItem <where you are copying files from>
    ForEach ($object in $data)
    {
    Copy-Item $object.Name -destination $destination
    }

    Now if you want to copy folders, you can use the -recurse option. Synchronization will require checking the difference on the LastWriteTime, which will require use of the Get-Date command and some "If" statements.

    Once you get it all figured out, you can set your powershell script up as a scheduled task to run once a day.

    That should be enough to get you started, good luck. ;)
  • RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    Everyone wrote: »
    Yes it is possible. Obviously it will take you longer to figure out how to write a powershell script to do this, than it would to just use robocopy, but it can be done.

    Something like this...


    $destination = <where you want the files to get copied to>
    $data = Get-ChildItem <where you are copying files from>
    ForEach ($object in $data)
    {
    Copy-Item $object.Name -destination $destination
    }

    Now if you want to copy folders, you can use the -recurse option. Synchronization will require checking the difference on the LastWriteTime, which will require use of the Get-Date command and some "If" statements.

    Once you get it all figured out, you can set your powershell script up as a scheduled task to run once a day.

    That should be enough to get you started, good luck. ;)
    While your cde is very readable that can be shortened.
    [B][FONT=Courier New][SIZE=2][COLOR=#5f9ea0][FONT=Courier New][SIZE=2][COLOR=#5f9ea0][FONT=Courier New][SIZE=2][COLOR=#5f9ea0]Get-ChildItem [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/B][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]C:\Users\rkaucher\Documents[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] | [/SIZE][/FONT][/SIZE][/FONT][B][FONT=Courier New][SIZE=2][COLOR=#5f9ea0][FONT=Courier New][SIZE=2][COLOR=#5f9ea0][FONT=Courier New][SIZE=2][COLOR=#5f9ea0]Copy-Item [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/B][I][FONT=Courier New][SIZE=2][COLOR=#5f9ea0][FONT=Courier New][SIZE=2][COLOR=#5f9ea0][FONT=Courier New][SIZE=2][COLOR=#5f9ea0]-Destination [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/I][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]C:\backup\[/COLOR][/SIZE][/FONT]
    [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
    
    which can also be shortened using aliases
    [B][FONT=Courier New][SIZE=2][COLOR=#5f9ea0][FONT=Courier New][SIZE=2][COLOR=#5f9ea0][FONT=Courier New][SIZE=2][COLOR=#5f9ea0]gci [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/B][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]C:\Users\rkaucher\Documents[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] | [/SIZE][/FONT][/SIZE][/FONT][B][FONT=Courier New][SIZE=2][COLOR=#5f9ea0][FONT=Courier New][SIZE=2][COLOR=#5f9ea0][FONT=Courier New][SIZE=2][COLOR=#5f9ea0]cp [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/B][I][FONT=Courier New][SIZE=2][COLOR=#5f9ea0][FONT=Courier New][SIZE=2][COLOR=#5f9ea0][FONT=Courier New][SIZE=2][COLOR=#5f9ea0]-Destination [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/I][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]C:\backup\[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
    

  • RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    Oh, and don't forget Where-Object!

    gci C:\Users\rkaucher\Documents | Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-1) } | cp -Destination C:\backup\
  • EveryoneEveryone Member Posts: 1,661
    Oh, and don't forget Where-Object!

    gci C:\Users\rkaucher\Documents | Where {$_.LastWriteTime -gt (Get-Date).AddDays(-1) } | cp -Destination C:\backup\

    Fixed that for ya. icon_thumright.gif If you're going to shorten things with aliases, go all the way! icon_cool.gif

    Yes I tend to write my scripts in an easy to read format. When I'm just issuing commands, I'd do it like that.
  • ClaymooreClaymoore Member Posts: 1,637
    Copy-Item does not copy the NTFS file permissions, which I assume you want. You could write a crazy PoSh script that included cacls, but scripting is supposed to make your life easier so Robocopy is the right tool in this case. Try:
    Robocopy d:\data\folder1 \\server\d$\data\folder1 /e /z /copyall /dcopy:T /r:2 /w:5 /log:d:\log\folder1.txt /v /tee
    

    Robocopy

    You will want the log file to satisfy the annoying middle manager who will ask if all the files were copied, and how do you know for sure. Tell him where the log is and invite him to review the 25MB text file. That will usually end the inquisition right there. The /r and /w are important so that Robocopy won't get stuck trying to copy the same bad file 1 million times.
  • EveryoneEveryone Member Posts: 1,661
    Claymoore wrote: »
    Copy-Item does not copy the NTFS file permissions, which I assume you want. You could write a crazy PoSh script that included cacls, but scripting is supposed to make your life easier so Robocopy is the right tool in this case. Try:

    Good point... yes Robocopy is the easy way to do it, but the question was, could it be done with PowerShell? Yes it can.

    The "Get-ACL" and "Set-ACL" commands can be used to take care of the permissions.
  • RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    Everyone wrote: »
    Fixed that for ya. icon_thumright.gif If you're going to shorten things with aliases, go all the way! icon_cool.gif

    Yes I tend to write my scripts in an easy to read format. When I'm just issuing commands, I'd do it like that.

    I understand completely. I didn't want people to think I was correcting you.

    Now some people never use the proper PoSh style syntax, though, because they learned C# or Perl or some similar language first and it acts as a handicap to them - and they never get to see how powerful it can be right at the shell. They only think of it as "scripting". I had to educate a Unix guy at work regaring that. He basically said it was too wordy to ever be useful as a proper shell language.
  • DevilsbaneDevilsbane Member Posts: 4,214 ■■■■■■■■□□
    At the request of Robert I picked up Amazon.com: Windows Powershell Cookbook: for Windows, Exchange 2007, and MOM V3 (9780596528492): Lee Holmes: Books (I went the used route and it was a whole $11). I've only paged through it but it looks to be a more practical application of what you can do rather than a how to. Lots of example scripts that it will teach you how to manipulate.
    Decide what to be and go be it.
  • EveryoneEveryone Member Posts: 1,661
    I understand completely. I didn't want people to think I was correcting you.

    Now some people never use the proper PoSh style syntax, though, because they learned C# or Perl or some similar language first and it acts as a handicap to them - and they never get to see how powerful it can be right at the shell. They only think of it as "scripting". I had to educate a Unix guy at work regaring that. He basically said it was too wordy to ever be useful as a proper shell language.

    There are some commands, especially if you get into working with Exchange 2007 or 2010, that do not have aliases, that are just stupidly long.

    I still want to get around to doing a 2008 R2 Core install. I think it'd help improve my PowerShell skills by forcing me to use it more.

    I still find myself looking up command syntax on TechNet more often than I'd like to.

    Right now aside from Exchange administration, I only use PowerShell for scripting.

    I did recently have some fun writing some scripts that used both PowerShell and diskpart to partition, format, label, create folders, assign drive letters, and assign volume mount points, for a very large number of disks on a 2008 R2 server.
  • XantchaXantcha Member Posts: 64 ■■□□□□□□□□
    Thanks guys. It's a bit (okay a lot) more than I was looking for but now I can pick the code apart and learn how it works. icon_thumright.gif
Sign In or Register to comment.