none
Pwershell script to Copy Complex Data - Remote Computer RRS feed

  • Question

  • Hello Folks,

    I hava large number of log copy tasks for many remote virtual machines.

    Logs are splitted  in 2 locations

    c:\Data\Folder1\Folder2\logs (this I manage to integrate in my script without issuse via copy option)

    and

    c:\Users\<Username>\AppData\Roaming\The SDK\log

    For this second ones I need to ONLY copy the files for users logged on the 16/03/2015 and only the Log files from 16/03/2015

    Does anyone have a sample that could do this work?

    Thanks in advance

    Viliam

    Thursday, March 19, 2015 3:35 PM

Answers

All replies

  • Hi Viliam,

    Here's a starter:

    Get-ChildItem 'C:\Users\*\AppData\Roaming\The SDK\log' | 
        Where { $_.CreationTime.Date -eq (Get-Date '16/03/2015').Date } |
            Copy-Item ...


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    Thursday, March 19, 2015 3:45 PM
  • I have tried this:

    Get-ChildItem '\\server\c$\Users\*\AppData\Roaming\The SDK\Log' |
        Where { $_.CreationTime.Date -eq (Get-Date '17/03/2015').Date } |
    Copy-Item \\Server\c$\Users\*\AppData\Roaming\SIGNificant Kiosk SDK\Log -Destination \\Server\c$\Logs\ -Recurse

    But it does not copy anything

    if I put a username (but not the purpose of the script) in the copy line it copies the content, what's wrong with it?

    Thursday, March 19, 2015 4:47 PM
  • I have tried this:

    Get-ChildItem '\\server\c$\Users\*\AppData\Roaming\The SDK\Log' |
        Where { $_.CreationTime.Date -eq (Get-Date '17/03/2015').Date } |
    Copy-Item \\Server\c$\Users\*\AppData\Roaming\SIGNificant Kiosk SDK\Log -Destination \\Server\c$\Logs\ -Recurse

    But it does not copy anything

    if I put a username (but not the purpose of the script) in the copy line it copies the content, what's wrong with it?

    I was missing ForEach-Object in my starter:

    Get-ChildItem 'C:\Users\*\AppData\Roaming\The SDK\log' | 
        Where { $_.CreationTime.Date -eq (Get-Date '16/03/2015').Date } | ForEach-Object {
    
            Copy-Item -Path $_.FullName -Destination wherever -WhatIf
    
    }

    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    Thursday, March 19, 2015 5:29 PM
  • this is a lot better, but it does not list me the names of the users from where it is copyng the data from; so I can't tell which log is which.

    Is it possible to copy also the Usernames located under Users ?

    thank you very much for your help.

    Friday, March 20, 2015 10:24 AM
  • this is a lot better, but it does not list me the names of the users from where it is copyng the data from; so I can't tell which log is which.

    Is it possible to copy also the Usernames located under Users ?

    Sure, you can do that. Here's an example of how to extract that information from the path:

    Get-ChildItem 'C:\Users\*\AppData\Roaming\The SDK\log' | ForEach {
    
        $userDir = ((Split-Path $_.FullName -NoQualifier) -split '\\')[2]
    
        $userDir
    
    }

    You can then use $userDir in your destination file name.

    thank you very much for your help.

    You're very welcome.


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    Friday, March 20, 2015 1:26 PM
  • when I enter that , I get the following:

    PS C:\Windows\system32> Get-ChildItem '\\Computer1\c$\Users\*\AppData\Roaming\The SDK'|
        Where { $_.LastWriteTime.Date -eq (Get-Date '19/03/2015' ).Date } | ForEach-Object {
          $userDir = ((Split-Path $_.FullName -NoQualifier) -split '\\Computer1\c$\Users\*')[2]
           Copy-Item -Path $_.FullName -Destination \\Computer2\c$\Downloads\Log -Recurse
           Copy-Item \\Computer1\c$\Data -Destination \\Computer2\c$\Downloads\Log\AGU -Recurse
           }     
    Bad argument to operator '-split': parsing "\\Computer1\c$\Users\*" - Unrecognized control character..
    At line:3 char:63
    +       $userDir = ((Split-Path $_.FullName -NoQualifier) -split <<<<  '\\Computer1\c$\Users\*')[2]
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : BadOperatorArgument

    Sorry in advance but, I guess I still have a long way to learn powershell :)

    Friday, March 20, 2015 2:47 PM
  • Ah, forgot you're using UNC paths.

    Change the -split back to '\\' but use index 5 instead of 2. You can also drop the -NoQualifier switch.


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    • Marked as answer by vipask Friday, March 20, 2015 5:01 PM
    • Unmarked as answer by vipask Monday, March 23, 2015 8:41 AM
    Friday, March 20, 2015 2:54 PM
  • Hello again,

    so now basically the script does what it is supposed to do and lists me in the console, which user has the data I need. But it puts everything in the same destination folder all in one. So I don't know which Log file corresponds to which user.

    I'm searching for a functionality ,that the script will generate the User directory and copy the required content based on the :

    Get-ChildItem '\\Computer1\c$\Users\*\AppData\Roaming\The SDK'|
        Where { $_.LastWriteTime.Date -eq (Get-Date '19/03/2015' ).Date } | ForEach-Object {

    Am I looking for science fiction ?

    Monday, March 23, 2015 8:25 AM
  • Hi,

    No, that can be done. Here's an example:

    $destinationPath = '\\ServerName\ShareFolder\LogFiles'
    
    Get-ChildItem '\\PC\c$\Users\*\AppData\Roaming' | ForEach {
    
        $userDir = ((Split-Path $_.FullName -NoQualifier) -split '\\')[5]
    
        $userFolder = Join-Path -Path $destinationPath -ChildPath $userDir
    
        New-Item -Path $userFolder -ItemType Directory
    
    }


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    • Marked as answer by vipask Tuesday, March 24, 2015 9:12 AM
    Monday, March 23, 2015 1:57 PM
  • Thanks for your support,

    this isuse has been solved

    Tuesday, March 24, 2015 9:12 AM
  • Cheers, you're very welcome. I'm glad everything is working properly.

    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    Tuesday, March 24, 2015 12:15 PM