You are on page 1of 3

function New-Zip

{
param([string]$zipfilename)
set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
(dir $zipfilename).IsReadOnly = $false
}

function Add-Zip{
param([string]$zipfilename)

Process {

if(-not (test-path($zipfilename)))
{
set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" *
18))
(dir $zipfilename).IsReadOnly = $false
}

$shellApplication = new-object -com shell.application


$zipPackage = $shellApplication.NameSpace($zipfilename)

$zipPackage.CopyHere($_.FullName)

}
}

$date = (Get-date).Tostring('MM-dd-yyyy-HH-mm')
$server = $ENV:Computername

#Logging
$CheckLogPath = Test-Path E:\logs\archiving-logs\
if (-not($checklogpath))
{
New-item -type directory -path E:\logs\archiving-logs

}
$now = get-date

$testpath = Test-Path "E:\logs\Source_logs"


if (-not ($testpath))
{
New-item -type directory -path E:\logs\Source_logs

$checkNaspath = Test-Path "\\Iceopsnas\iceopsnas.grp\ICE_LOGS\R5\Server_logs\


$server"
If (-not ($checkNaspath)) {
md "\\Iceopsnas\iceopsnas.grp\ICE_LOGS\R5\Server_logs\$server"
}

$checkSPfolder = Test-Path "\\Iceopsnas\iceopsnas.grp\ICE_LOGS\R5\Server_logs\


$server\Sharepoint"
If (-not ($checkSPfolder)) {
md "\\Iceopsnas\iceopsnas.grp\ICE_LOGS\R5\Server_logs\
$server\Sharepoint"
}

Get-ChildItem E:\logs\SharePoint\ -filter *.log | sort -property lastwritetime


-descending | select -skip 2 | move-item -destination "E:\logs\Source_logs"
$ListToBeMoved = dir E:\logs\source_logs\ | % {$_.name.Trim() -replace ".log", ""}

$LogOutput = @()

$LogOutput += "Following logs will be moved to NAS at $now. `n"


$LogOutput += "`n"
$LogOutput += dir E:\logs\source_logs\ | % {$_.name}

$ToBeMoved = (dir E:\logs\source_logs\ | % {$_.name} | select measure).count

New-Zip \\iceopsnas\iceopsnas.grp\ice_logs\r5\Server_logs\$server\Sharepoint\
$date.zip
start-sleep -s 3

Get-Item E:\logs\Source_logs | Add-Zip


\\iceopsnas\iceopsnas.grp\ice_logs\r5\Server_logs\$server\Sharepoint\$date.zip

$filesize = (get-item \\iceopsnas\iceopsnas.grp\ice_logs\r5\Server_logs\


$server\Sharepoint\$date.zip).length
while ($filesize -lt 100) {
$filesize = (get-item \\iceopsnas\iceopsnas.grp\ice_logs\r5\Server_logs\
$server\Sharepoint\$date.zip).length

start-sleep -s 60

$shell = new-object -com Shell.Application


$zipFile = $shell.NameSpace("\\iceopsnas\iceopsnas.grp\ice_logs\r5\Server_logs\
$server\Sharepoint\$date.zip")
$moved = ($zipFile.Items() | % {$_.getfolder.items()| %{$_.name}} | measure).count
$ListMoved = $zipFile.Items() | % {$_.getfolder.items()| %{$_.name.trim()}}

$LogOutput +=
"------------------------------------------------------------------------"

$LogOutput += "Following logs have been zipped and moved to NAS and located at
\\iceopsnas\iceopsnas.grp\ice_logs\r5\Server_logs\$server\Sharepoint\$date.zip"
$LogOutput += "`n"

$LogOutput += $zipFile.Items() | % {$_.getfolder.items()| %{$_.name}}


"$ToBeMoved is to be moved"
"$moved is moved"

If ($ToBeMoved -eq $Moved) {


$LogOutput += "NO ERRORS OCCURED DURING LOGS ARCHIVING. TOTAL NO OF FILES TO
BE ARCHIVED WERE $ToBeMoved AND TOTAL NO OF FILES THAT HAS BEEN ARCHIVED IS $Moved"

}Else {

$LogOutput += "`n"
$LogOutput += "SOME ISSUE OCCURRED DURING LOGS ARCHIVING AS TOTAL NO OF FILES
TO BE ARCHIVED WERE $TOBEMOVED AND TOTAL NO OF FILES THAT HAS BEEN ARCHIVED IS
$MOVED. Below are the files that were not moved.`n"
$NotMoved = Compare -ReferenceObject $ListToBeMoved -DifferenceObject
$ListMoved | % {$_.InputObject}
$LogOutput += $NotMoved
$LogOutput += "`n"
$LogOutput += "Files that were not moved have not been deleted and is present
at E:\logs\source_logs and will moved at the next run."
}

$NotMoved | % {
$file = $_
dir E:\logs\source_logs\ | % {
$logfile = $_.name -replace ".log", ""
$logf = $_.name
If ($file -notlike "$logfile") {
remove-item E:\logs\source_logs\$logf
}
}

$LogOutput | out-file E:\logs\archiving-logs\$date.log

You might also like