Advertisement
Guest User

Untitled

a guest
Aug 25th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. Function Clean_Deckman{
  2.  
  3. $UserDesk = $env:USERPROFILE
  4. $Username = "######"
  5. $Password = "######"
  6. $Pass = convertTo-SecureString -AsPlainText $Password -Force
  7. $SecureString = $Pass
  8. $MySecureCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username,$SecureString
  9.  
  10. $BK = "PRBACKEND01"
  11. $TL = "PRTOS0N4TL1"
  12.  
  13. $GetFolder = Invoke-Command -ComputerName $BK -ScriptBlock {get-childitem -path "C:\TBA" | Sort-Object LastWriteTime -Descending | Select-Object -First 1} -Credential $MySecureCreds
  14.  
  15. If($RadioButton1.checked){
  16. Invoke-Command -ComputerName $BK -ScriptBlock {
  17. $GetFolder = get-childitem -path "C:\TBA" | Sort-Object LastWriteTime -Descending | Select-Object -First 1
  18. $Location = "C:\TBA\$($GetFolder)\log"
  19.  
  20. #get the list of files in the original folder
  21. $rootFolder = $Location
  22. $tempVariable = $rootFolder
  23. $files = Get-ChildItem -Path $rootFolder
  24.  
  25. #create a temporary folder using today's date
  26. $tempFolderRoot = "C:\Users\sa_toslink\Desktop\Logs_"
  27. $date = Get-Date
  28. $date = $date.ToString("yyyy-MM-dd-hhmm")
  29. $tempFinalFolder = "$tempFolderRoot$date"
  30. New-Item -ItemType directory -Path $tempFinalFolder -Force
  31.  
  32. #decide how long back to go
  33. $timespan = new-timespan -days 4
  34.  
  35. #move the files to a temporary location
  36. foreach($file in $files)
  37. {
  38. $fileLastModifieddate = $file.LastWriteTime
  39. if(((Get-Date) - $fileLastModifiedDate) -gt $timespan)
  40. {
  41. Move-Item "$rootFolder\$file" -destination $tempFinalFolder
  42. }
  43. }
  44.  
  45. $CompressionToUse = [System.IO.Compression.CompressionLevel]::Optimal
  46. $IncludeBaseFolder = $false
  47. $zipTo = "{0}\Backend_{1}.zip" -f $rootFolder,$date
  48.  
  49. #add the files in the temporary location to a zip folder
  50. [Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
  51. [System.IO.Compression.ZipFile]::CreateFromDirectory($tempFinalFolder, $ZipTo, $CompressionToUse, $IncludeBaseFolder)
  52.  
  53. Remove-Item $tempFinalFolder -RECURSE
  54. #Move-Item $zipTo -Destination $UserDesk\desktop
  55.  
  56. } -Credential $MySecureCreds
  57.  
  58. Move-Item "\\PRBACKEND01\C$\TBA\$($GetFolder)\log\Backend_*.zip" -Destination \\prnas01\N4_logs
  59.  
  60. $MessageBox1.Text = "$($BK) has been cleaned, logs can be found at \\prnas01\n4_logs\Backend_YYYY-MM-dd-hhmm.zip"
  61. }
  62.  
  63.  
  64. Elseif($RadioButton2.Checked){}
  65.  
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement