gangstanthony

powershell-script-to-copy-files-if-drive-free-space-is-great

Apr 21st, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # http://stackoverflow.com/questions/36776415/powershell-script-to-copy-files-if-drive-free-space-is-greater-than-threshold-va
  2.  
  3. # get drives starting with C that have at least 5GB of free space, then select the first one on the list
  4. $drive = ([System.IO.DriveInfo]::GetDrives() | ? {$_.totalfreespace / 1gb -gt 5}).name | ? {[int][char]$_[0] -ge 67} | select -f 1
  5.  
  6. if (!$drive) {
  7.     Write-Warning 'No drives have more than 5GB of free space. Exiting script.'
  8.     return
  9. }
  10.  
  11. Write-Host "Selected drive: $drive"
  12.  
  13. # get list of servers
  14. $serversfile = 'c:\servers.txt'
  15. $servers = Get-Content $serversfile
  16.  
  17. foreach ($server in $servers) {
  18.     # specify folder to copy on current server
  19.     $source = "\\$server\share\Softwares"
  20.    
  21.     # specify destination folder
  22.     $destination = Join-Path $drive $server
  23.  
  24.     # if destination does not exist, create the folder
  25.     if (!(Test-Path $destination)) {
  26.         md $destination | Out-Null
  27.     }
  28.  
  29.     # copy shared Softwares folder to the root of the selected drive
  30.     Write-Host "Copying '$source' folder to '$destination'"
  31.     copy $source $destination -Recurse -WhatIf
  32. }
Add Comment
Please, Sign In to add comment