Link to home
Start Free TrialLog in
Avatar of ITguy565
ITguy565Flag for United States of America

asked on

Powershell Scripting Assistance : File Copy and Transfer Speed

Experts,

I want to do the following and am not sure how to present this in powershell:

I want to take a test file "Test1.vhdx" and transfer it to multiple endpoints that I specify.

My best solution would be to present this in the form of a function or commandlets that returns the following:

Name, Server, origination, Destination, and Transfer Rate in MB/s or GB/s


Here is the base script I have come up with.. It is incomplete but contains comments in sections that I need assistance in.

$Results = @()

Function Test-FileTransferSpeed($TargetServer, $TestFileName, $origination, $destination){
    
    write-host "Testing File Transfer $($origination) to $($Destination)"

    Try {enter-pssession -ComputerName $TargetServer;
    
        #Check if File Exists in Destination if there Delete the file
        if (!(get-childitem $Destination)){write-host "Destination File Does not Exist : Starting Test"}else {
            try {if (get-childitem -path $destination){get-childitem -path $destination |Remove-Item -whatif}else{write-host "Something went Wrong File Does not Exist or can't be found"}}catch{"Error Detected with File"}
        }

        #Check Originating Location for the file
        if (!(get-childitem $Origination)) { write-host "Origination File Does not Exist : Test Abort" }else {write-host "File Found in Origination location"}


#Parse this Data and Present stats in `$Copystats 
        robocopy *.tst $origination $Destination

        #Create Custom Object to store data
        $Copystats = ""|select Name,Server,Origination,Destination,TransferRate
        $Results += $Copystats
        $Results



    
    
    }catch {Throw "Could Not Connect to $($TargetServer)"}
}

. Test-FileTransferSpeed -TargetServer Server1 -TestFileName "Test1.vhdx" -origination c:\test\testlocation1 -destination c:\testdestination

Open in new window

Avatar of ITguy565
ITguy565
Flag of United States of America image

ASKER

if Robocopy isn't the best method, I am open to others as well.. Robocopy would be preferred as it allows multithreading fairly easily with just a commandline switch.

Update to the code:

#$TestFileName = "Test1.tst"
$Results = @()

Function Test-FileTransferSpeed($TargetServer, $TestFileName, $origination, $destination){
    
    write-host "Testing File Transfer $($origination) to $($Destination)"

    Try {enter-pssession -ComputerName $TargetServer;
    
        #Check if File Exists in Destination if there Delete the file
        if (!(get-childitem $Destination)){write-host "Destination File Does not Exist : Starting Test"}else {
            try {if (get-childitem -path $destination){get-childitem -path $destination |Remove-Item -whatif}else{write-host "Something went Wrong File Does not Exist or can't be found"}}catch{"Error Detected with File"}
        }

        #Check Originating Location for the file
        if (!(get-childitem $Origination)) { write-host "Origination File Does not Exist : Test Abort" }else {write-host "File Found in Origination location"}


#Parse this Data and Present stats in `$Copystats 
        robocopy *.tst $origination $Destination

        #Create Custom Object to store data
        $Copystats = ""|select Server,Origination,Destination,TransferRate
        $copystats.Server = "$($TargetServer)"
        $copystats.Origination = $($origination)
        $copystats.Destination = $($Destination)
        $copystats.TransferRate = "Transfer Rate Here"
        $Results += $Copystats
        $Results



    
    
    }catch {Throw "Could Not Connect to $($TargetServer)"}
}

. Test-FileTransferSpeed -TargetServer Server1 -TestFileName "Test1.tst" -origination c:\test\testlocation1 -destination c:\testdestination

Open in new window

Your robocopy statement is using *.tst rather than the "Test1.vhdx" value of the $TestFileName parameter
@Aikimark,

Thanks for catching that... I have changed the name to reflect the needed change.
I'm not sure how you're capturing the report output from robocopy.  I would have expected that you would have assigned that statement to some variable and then parsed the variable for your report.
This is as far as I have gotten:

$Results = @()

Function Test-FileTransferSpeed($TargetServer, $TestFileName, $origination, $destination){
    
    write-host "Testing File Transfer $($origination) to $($Destination)"

    Try {enter-pssession -ComputerName $TargetServer;
    
        #Check if File Exists in Destination if there Delete the file
        if (!(get-childitem $Destination)){write-host "Destination File Does not Exist : Starting Test"}else {
            try {if (get-childitem -path "$destination\$TestFileName"{set-location $destination;get-childitem -path $($destination\$TestFileName) |Remove-Item -whatif}else{write-host "Something went Wrong File Does not Exist or can't be found"}}catch{"Error Detected with File"}
        }

        #Check Originating Location for the file
        if (!(get-childitem $Origination)) { write-host "Origination File Does not Exist : Test Abort" }else {write-host "File Found in Origination location"}


#Parse this Data and Present stats in `$Copystats 
        $robocopy = robocopy *.tst $origination $Destination

        #Create Custom Object to store data
        $Copystats = $null
        $Copystats = "" | select Server, TestFileName, Origination, Destination, completionTime, TransferRate
        $copystats.Server = "$($TargetServer)"
        $Copystats.TestFileName = $($TestFileName)
        $copystats.Origination = $($origination)
        $copystats.Destination = $($Destination)
        $copystats.completionTime = (($robocopy | select-string -pattern "Times\s:\s+(\d:\d+:\d+)").matches.groups[1]).value
        $copystats.TransferRate = "Transfer Rate Here"
        $Results += $Copystats
        $Results



    
    
    }catch {Throw "Could Not Connect to $($TargetServer)"}
}

. Test-FileTransferSpeed -TargetServer Server1 -TestFileName "Test1.tst" -origination c:\test\testlocation1 -destination c:\testdestination

Open in new window

I meant something like this:
$robocopy = robocopy $TestFileName $origination $Destination

Open in new window

#Parse this Data and Present stats in `$Copystats 
        $robocopy = robocopy *.tst $origination $Destination

        #Create Custom Object to store data
        $Copystats = $null
        $Copystats = "" | select Server, TestFileName, Origination, Destination, completionTime, TransferRate
        $copystats.Server = "$($TargetServer)"
        $Copystats.TestFileName = $($TestFileName)
        $copystats.Origination = $($origination)
        $copystats.Destination = $($Destination)
        $copystats.completionTime = (($robocopy | select-string -pattern "Times\s:\s+(\d:\d+:\d+)").matches.groups[1]).value
        $copystats.TransferRate = "Transfer Rate Here"
        $Results += $Copystats
        $Results

Open in new window


That is how I ended up populating everything but the Transfer rate.. Just not sure how to get that.
ASKER CERTIFIED SOLUTION
Avatar of aikimark
aikimark
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Aikimark,


There is a Bytes line in the report.  You divide the bytes by the elapsed time to get the transfer rate.

I never was able to get the proper information utilizing this Bytes Line..

I am however going to give the question to you given the fact that no other experts chimed in and I have no further time to spend on this.
Thanks for your attempt at this..
What does your PS code currently look like?

Also, please post the text from your robocopy invocation.