Server administration. Resource management tools PowerShell

Hello.

Today our administrator had to monitor on network shares, how many GB was used by the staff in their personal directories (disk space is allocated by mapping a network drive). Assume that the maximum allowable size of a personal resource is equal to 1 GB. If this size exceeds the limit, you must inform the administrator. It is also necessary to control the sharp increase in the size of personal resources. For example, if the personal directory of an employee increases with the size of 200 MB per week, you must notify the administrator.


So, Powershell'a code that has implemented all the above requirements:

The function of sending mail. The function Universalna, tailored to the task. Ie, accept for the fact that the subject of the message, the recipient does not change. The only difference is the body, it is what we set as incoming parameter for the function:

the
function EmailNotification($Mail_body)
{
$Sender = "audit@..."
$Receipt = "levitskaks@gmail.com"
$Server = "gmail.com.ua"
$Object = "DirSize:" + (Get-Date)
$SMTPclient = new-object System.Net.Mail.SmtpClient $Server
#Specify SMTP port if needed
$SMTPClient.port = 25
#Activate SSL if needed
#$SMTPclient.EnableSsl = $true
#Specify email account credentials if needed
$SMTPAuthUsername = "levitskaks@gmail.com"
$SMTPAuthPassword = "pass"
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($SMTPAuthUsername, $SMTPAuthPassword)
$Message = new-object System.Net.Mail.MailMessage $Sender, $Receipt, $Object, $Mail_body
#-$Message.IsBodyHtml = $true;
$SMTPclient.Send($Message)
}


Go to the main function. Incoming parameters:
— source directory. The directory in which the list of directories under each employee;
— the maximum allowable size in bytes;
— the path for saving the log work;

the
function Check-Size-Directory ($dir, $GB, $Logpath) {


The designated day of the week of the year. At the time of writing the script was week 47.
Also we are interested in the verification run once a week on Monday. Validation logic is the following: even in a week remains to log "0.log". In the odd week that the saved file "1.log". If the date changes to "0.log" now the date of the change "1.log", then logically determine that the last week was even. and we verify in which directory size increased by more than 200 MB.

the
[Int32]$Monday = (Get-Date-UFormat "%w")
$Monday
[Int32]$Week = (Get-Date-UFormat "%W")
$Week
if ($week%2 -eq 0-and $Saturday-eq 1){

$LogPathFile = $LogPath + "\" + ($week%2).ToString() + ".log" 
If (!(Test-Path -path $LogPathFile)){
Write-Host "Created element"
New-Item-Path $LogPathFile -ItemType File
}
$ToFile = "" | Out-File $LogPathFile
Write-Host "Odd week"
Get-ChildItem -path $dir | %{
$dir_property = dir $_.FullName -recurse | where {-Not $_.PSIsContainer}| Measure-Object-Property length-Sum 


The first required inspection: check the directory size larger than 1GB.

the
 if ($dir_property.Sum-gt $GB){
$Mail_body+= "Size of directory" + $_.FullName + "exceeds the size of 1 GB.`n"
$Mail_body+= "Size of directory" + $_.FullName + " is " + (($dir_property.Sum)/1024/1024) + "MB.`n"
$Mail_body+= "------------------------------------------------------------------------------------------`n"
<b>$ToFile = $_.DirectoryName + "" + (($dir_property.Sum)/1024/1024) | Out-File $LogPathFile -Append</b>
}
else {
Write-Host "All is well with the directory:" $_.FullName
}
}
EmailNotification -Mail_body $Mail_body


Save the directory name and size using the separator "|", subsequently through which will parse the contents of the file.

the
<b>$ToFile = $_.DirectoryName + "" + (($dir_property.Sum)/1024/1024) | Out-File $LogPathFile -Append</b>


Define which file you consider new to be compared with the file a week ago.

the
$Mail_body = ""
If ((Get-Item-Path $LogPath\0.log).LastWriteTime -gt (Get-Item-Path $LogPath\1.log).LastWriteTime){
$log_content = Get-Content (Get-ChildItem -Path $LogPath\0.log)
foreach ($data in $log_content) 
{
$x = $data.split("|")
$xc1 = $x[0]
$xc2 = $x[1]
$log_content = Get-Content (Get-ChildItem -Path $LogPath\1.log)
foreach ($data in $log_content) 
{
$y = $data.split("|")
if ($xc1 -eq $y[0]){
if(([Int32]$xc2 - [Int32]$y[1]) -gt 200){
$Mail_body += "a Sharp increase in directory size:" + $xc1 + "`n the Size of last week amounted to: "+ $y[1] + " MB." + "`n Size for the current week amounted to: "+ $xc2 + "MB.`n"


}
}

} 
EmailNotification -Mail_body $Mail_body
} 
}


Parity / odd weeks is checked by the modulus of the received weeks of the year:

the
<b>$week%2</b>


Similar checks for odd weeks:

the
 if ($week%2 -eq 1-and $Saturday-eq 1){
Write-Host "Odd week"
$LogPathFile = $LogPath + "\" + ($week%2).ToString() + ".log"
If (!(Test-Path -path $LogPathFile)){
Write-Host "Created element"
New-Item-Path $LogPathFile -ItemType File
}
$ToFile = "" | Out-File $LogPathFile

Get-ChildItem -path $dir | %{
$dir_property = dir $_.FullName -recurse | where {-Not $_.PSIsContainer}| Measure-Object-Property length-Sum 

if ($dir_property.Sum-gt $GB){
$Mail_body+= "Size of directory" + $_.FullName + "exceeds the size of 1 GB.`n"
$Mail_body+= "Size of directory" + $_.FullName + " is " + (($dir_property.Sum)/1024/1024) + "MB.`n"
$Mail_body+= "------------------------------------------------------------------------------------------`n"
$ToFile = $_.Name + "|" + (($dir_property.Sum)/1024/1024) | Out-File $LogPathFile -Append
}
else {
Write-Host "All is well with the directory:" $_.FullName
}
}
EmailNotification -Mail_body $Mail_body
$Mail_body = ""
If ((Get-Item-Path $LogPath\1.log).LastWriteTime -gt (Get-Item-Path $LogPath\0.log).LastWriteTime){
$log_content = Get-Content (Get-ChildItem -Path $LogPath\1.log)
foreach ($data in $log_content) 
{
$x = $data.split("|")
$xc1 = $x[0]
$xc2 = $x[1]
$log_content = Get-Content (Get-ChildItem -Path $LogPath\0.log)
foreach ($data in $log_content) 
{
$y = $data.split("|")
if ($xc1 -eq $y[0]){
if(([Int32]$xc2 - [Int32]$y[1]) -gt 200){
$Mail_body += "a Sharp increase in directory size:" + $xc1 + "`n the Size of last week amounted to: "+ $y[1] + " MB." + "`n Size for the current week amounted to: "+ $xc2 + "MB.`n"
$Mail_body
}

}
}

} 
EmailNotification -Mail_body $Mail_body
} 


}

}


To start define incoming parameters and run the test.

the
#Proveril directory: if it does not exceed the size of 1 GB
$GB = 1073741824
$dir = "D:\Program Files"
Check-Size-and-Directory -dir $dir-107374 GB-Logpath "D:"


The result of the check by e-mail:

Subject: DirSize: 11/27/2015 11:32:05
Date: 27 Nov 2015 11:32:05 +0200
From: sizefoldres@gmail.com
To: levitskaks@gmail.com

The size of the directory F:\Shared\PrivateData\BRU exceeds the size of 1 GB.
The size of the directory F:\Shared\PrivateData\BRU is 1239.06250095367 MB.


The size of the directory F:\Shared\PrivateData\Danпревышает size 1GB.
The size of the directory F:\Shared\PrivateData\Dan is 1670.62088680267 MB.


The size of the directory F:\Shared\PrivateData\DYA exceeds the size of 1 GB.
The size of the directory F:\Shared\PrivateData\DYA is 7456.12028884888 MB.


The size of the directory F:\Shared\PrivateData\GLU exceeds the size of 1 GB.
The size of the directory F:\Shared\PrivateData\GLU is 2198.93785953522 MB.

...

The contents of the file 0.log:

the
ActiveX|2.8662109375
AvPinTool|0.5712890625
BDE|10.4070873260498
<b>drivers|6.512216567993</b>
Drv for Sigatoka 337/0.129350662231445
eclipse-standard-kepler-SR2-win32|545.861120223999
flash|122.166826248169
FTP_Drive|0.252327919006348
Install|431.435597419739
Jabber|55.9909982681274
LibreOffice_4_3_4|215.234375
Liga9|336.688585281372
Mail (address_book)|0.141551971435547
nkicntInit|0.166786193847656
powershell_3.0/14.0534420013428
PowerShell_4_0|46.8222227096558
single|630.298968315125
Total Commander|6.67717361450195
WinImage|1.12846660614014
zabbix|1.37527465820313
Documentation|0.584843635559
CSPKeyUtil.exe|0.89208984375
jdk-8u65-windows-i586.exe|181.22908782959
jre-8u65-windows-i586.exe|47.8077087402344
LimeActiveXCrypt.cab|5.14455604553223
npp.6.7.4.Installer.exe|7.59689044952393
SkypeSetupFull_6.21.exe|34.3624038696289
winapcupsd-3.14.12.exe|5.84123229980469


The contents of the file 1.log:

the
ActiveX|2.8662109375
AvPinTool|0.5712890625
BDE|10.4070873260498
<b>drivers|276.512216567993</b>
Drv for Sigatoka 337/0.129350662231445
eclipse-standard-kepler-SR2-win32|545.861120223999
flash|122.166826248169
FTP_Drive|0.252327919006348
Install|431.435597419739
Jabber|55.9909982681274
LibreOffice_4_3_4|215.234375
Liga9|336.688585281372
Mail (address_book)|0.141551971435547
nkicntInit|0.166786193847656
powershell_3.0/14.0534420013428
PowerShell_4_0|46.8222227096558
single|630.298968315125
Total Commander|6.67717361450195
WinImage|1.12846660614014
zabbix|1.37527465820313
Documentation|205.584843635559
CSPKeyUtil.exe|0.89208984375
jdk-8u65-windows-i586.exe|181.22908782959
jre-8u65-windows-i586.exe|47.8077087402344
LimeActiveXCrypt.cab|5.14455604553223
npp.6.7.4.Installer.exe|7.59689044952393
SkypeSetupFull_6.21.exe|34.3624038696289
winapcupsd-3.14.12.exe|5.84123229980469


The result of the comparison in two files:

The sharp increase in the size of the directory: D:\Program Files\drivers
The size of last week were: 6.512216567993 MB.
The size of this week were: 276.512216567993 MB.
The result is displayed information at the request of the admin resource can perform any action, such as archiving and transfer of, or selection of files in the parameters and sending them to storage.

Thank you for your attention.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

ODBC Firebird, Postgresql, executing queries in Powershell

garage48 for the first time in Kiev!

The Ministry of communications wants to ban phones without GLONASS