ITguy565
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.
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
Your robocopy statement is using *.tst rather than the "Test1.vhdx" value of the $TestFileName parameter
ASKER
@Aikimark,
Thanks for catching that... I have changed the name to reflect the needed change.
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.
ASKER
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
I meant something like this:
$robocopy = robocopy $TestFileName $origination $Destination
ASKER
#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
That is how I ended up populating everything but the Transfer rate.. Just not sure how to get that.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@Aikimark,
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.
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.
ASKER
Thanks for your attempt at this..
What does your PS code currently look like?
Also, please post the text from your robocopy invocation.
Also, please post the text from your robocopy invocation.
ASKER
Update to the code:
Open in new window