Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Clean_Deckman{
- $UserDesk = $env:USERPROFILE
- $Username = "######"
- $Password = "######"
- $Pass = convertTo-SecureString -AsPlainText $Password -Force
- $SecureString = $Pass
- $MySecureCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username,$SecureString
- $BK = "PRBACKEND01"
- $TL = "PRTOS0N4TL1"
- $GetFolder = Invoke-Command -ComputerName $BK -ScriptBlock {get-childitem -path "C:\TBA" | Sort-Object LastWriteTime -Descending | Select-Object -First 1} -Credential $MySecureCreds
- If($RadioButton1.checked){
- Invoke-Command -ComputerName $BK -ScriptBlock {
- $GetFolder = get-childitem -path "C:\TBA" | Sort-Object LastWriteTime -Descending | Select-Object -First 1
- $Location = "C:\TBA\$($GetFolder)\log"
- #get the list of files in the original folder
- $rootFolder = $Location
- $tempVariable = $rootFolder
- $files = Get-ChildItem -Path $rootFolder
- #create a temporary folder using today's date
- $tempFolderRoot = "C:\Users\sa_toslink\Desktop\Logs_"
- $date = Get-Date
- $date = $date.ToString("yyyy-MM-dd-hhmm")
- $tempFinalFolder = "$tempFolderRoot$date"
- New-Item -ItemType directory -Path $tempFinalFolder -Force
- #decide how long back to go
- $timespan = new-timespan -days 4
- #move the files to a temporary location
- foreach($file in $files)
- {
- $fileLastModifieddate = $file.LastWriteTime
- if(((Get-Date) - $fileLastModifiedDate) -gt $timespan)
- {
- Move-Item "$rootFolder\$file" -destination $tempFinalFolder
- }
- }
- $CompressionToUse = [System.IO.Compression.CompressionLevel]::Optimal
- $IncludeBaseFolder = $false
- $zipTo = "{0}\Backend_{1}.zip" -f $rootFolder,$date
- #add the files in the temporary location to a zip folder
- [Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
- [System.IO.Compression.ZipFile]::CreateFromDirectory($tempFinalFolder, $ZipTo, $CompressionToUse, $IncludeBaseFolder)
- Remove-Item $tempFinalFolder -RECURSE
- #Move-Item $zipTo -Destination $UserDesk\desktop
- } -Credential $MySecureCreds
- Move-Item "\\PRBACKEND01\C$\TBA\$($GetFolder)\log\Backend_*.zip" -Destination \\prnas01\N4_logs
- $MessageBox1.Text = "$($BK) has been cleaned, logs can be found at \\prnas01\n4_logs\Backend_YYYY-MM-dd-hhmm.zip"
- }
- Elseif($RadioButton2.Checked){}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement