📜 ⬆️ ⬇️

Server administration Resource Management with PowerShell

Good day.

Today, our administrator needed to track on network resources how many GBs were used by employees in their personal directories (disk space was allocated by connecting a network drive). We assume that the maximum allowable size of a personal resource is 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 an employee’s personal directory increases by 200 MB per week, you need to notify the administrator.


So, the Powershell code that implemented all the above requirements:
')
The function of sending mail. The function is not universal, tailored to the task. Those. We take for the fact that the subject of the message, the recipient does not change. Only the body changes, we then set it as an input parameter for the function:

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. A directory in which a list of directories for each employee;
- maximum allowable size in bytes;
- the path to save the log of work;

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


Determine the day of the week in the year. At the time of writing the script was 47 weeks.
We are also interested in performing a check - once a week on Monday. The logic of the check is the following: in the even week the log of “0.log” is saved. In odd weeks we save the file “1.log”. If the date of the change “0.log” is greater than the date of the change “1.log”, then we logically determine that the last week was even. and check in which directory the size has increased by more than 200 MB.

 [Int32]$Monday = (Get-Date -UFormat "%w") $Monday [Int32]$Week = (Get-Date -UFormat "%W") $Week if ($week%2 -eq 0 -and $Monday -eq 1){ $LogPathFile = $LogPath + "\" + ($week%2).ToString() + ".log" If (!(Test-Path -path $LogPathFile)){ Write-Host " " New-Item -Path $LogPathFile -ItemType File } $ToFile = "" | Out-File $LogPathFile Write-Host " " Get-ChildItem -path $dir | %{ $dir_property = dir $_.FullName -recurse | where {-Not $_.PSIsContainer}| Measure-Object -Property length -Sum 


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

  if ($dir_property.Sum -gt $GB){ $Mail_body+= "  " + $_.FullName + "    1 .`n" $Mail_body+= "  " + $_.FullName + "  " + (($dir_property.Sum)/1024/1024) + " .`n" $Mail_body+= "------------------------------------------------------------------------------------------`n" <b>$ToFile = $_.DirectoryName + " " + (($dir_property.Sum)/1024/1024) | Out-File $LogPathFile -Append</b> } else { Write-Host "   : " $_.FullName } } EmailNotification -Mail_body $Mail_body 


We save the directory name and its size through the separator "|", after which we will parse the contents of the file.

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


We determine which file is considered new in order to make a comparison with the file a week ago.

 $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 += "   : " + $xc1 + "`n     : " + $y[1] + " ." + "`n     : " + $xc2 + " .`n" } } } } EmailNotification -Mail_body $Mail_body } } 


Checking the evenness / oddness of weeks is checked by dividing by module the week received in a year:

 <b>$week%2</b> 


Similar checks for odd week:

  if ($week%2 -eq 1 -and $Monday -eq 1){ Write-Host " " $LogPathFile = $LogPath + "\" + ($week%2).ToString() + ".log" If (!(Test-Path -path $LogPathFile)){ Write-Host " " 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+= "  " + $_.FullName + "    1 .`n" $Mail_body+= "  " + $_.FullName + "  " + (($dir_property.Sum)/1024/1024) + " .`n" $Mail_body+= "------------------------------------------------------------------------------------------`n" $ToFile = $_.Name + "|" + (($dir_property.Sum)/1024/1024) | Out-File $LogPathFile -Append } else { Write-Host "   : " $_.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 += "   : " + $xc1 + "`n     : " + $y[1] + " ." + "`n     : " + $xc2 + " .`n" $Mail_body } } } } EmailNotification -Mail_body $Mail_body } } } 


To run, we determine the incoming parameters and run the check.

 # :       1  $GB = 1073741824 $dir = "D:\Program Files" Check-Size-Directory -dir $dir -GB 107374 -Logpath "D:" 


The result of checking the message on the mail:

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

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

Size of the F: \ Shared \ PrivateData \ Dan directory exceeds the size of 1 GB.
The size of the F: \ Shared \ PrivateData \ Dan catalog is 1670.62088680267 MB.
-

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

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

Content of the 0.log file:

 ActiveX|2.8662109375 AvPinTool|0.5712890625 BDE|10.4070873260498 <b>drivers|6.512216567993</b> Drv for Secureoken 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 |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 


Contents of the 1.log file:

 ActiveX|2.8662109375 AvPinTool|0.5712890625 BDE|10.4070873260498 <b>drivers|276.512216567993</b> Drv for Secureoken 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 |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 comparing two files:

A sharp increase in the size of the directory: D: \ Program Files \ drivers
The size of the last week was: 6.512216567993Mb.
The size this week was: 276.512216567993 MB.

The result is displayed informational. At the request of the administrator with the resource, you can perform any actions, for example, archiving and transferring, or selecting files by parameters and sending them to the repository.

Thanks for attention.

Source: https://habr.com/ru/post/272107/


All Articles