Tuesday, February 12, 2019

Get Remote Machine Folder Size From Power Shell..


In some of the  administrator task scenario admin need to know the particular folder size from user machine’s and sometime remote sever storage folder admin’s has to monitor without login into the server or machine’s
.Here power shell help you get folder size in GB OR MB from remote machine .I made changes into script as my requirement into this script.
This scrip use psexec.exe to enter PS session on remote machine in same network. You have to copy this Script.ps1 and psexec.exe into Same folder(like C:\Temp).You can run this script on multiple machine and server at a time
just you have to pass machine name “C:\Temp\CBSLogSizeCompList.txt” and it will create the log in to “C:\temp\CBSLogSize.txt




#################################################################################

#  Get Folder Size from Remote machine

Function Class-Size($size)
{
IF($size -ge 1GB)
{
"{0:n2}" -f  ($size / 1GB) + " GB"
}
ELSEIF($size -ge 1MB)
{
"{0:n2}" -f  ($size / 1MB) + " MB"
}
ELSE
{
"{0:n2}" -f  ($size / 1KB) + " KB"
}
}

function Get-FolderSize
{
Param(
$Path, [Array]$Computers
)
$Array = @()
Foreach($Computer in $Computers)
    {
    $ErrorActionPreference = "SilentlyContinue"

$Length = Invoke-Command -ComputerName $Computer -ScriptBlock {
 (Get-ChildItem $args[0] -Recurse | Measure-Object -Property Length -Sum).Sum

} -ArgumentList $Path

$Result = "" | Select Computer,Folder,Length
$Result.Computer = $Computer
$Result.Folder = $Path
Write-Host $length
$Result.Length = Class-Size $length
$array += $Result
$Result | Add-Content c:\Temp\CBSLogSize.txt
}

return $array
}

$sComputerName = Get-Content 'C:\SWKSoftware\CBSLogSizeCompList.txt'

foreach ($sComputerName1 in $sComputerName)
{

        if (test-Connection -ComputerName $sComputerName1 -Count 2 -Quiet )
        { 
                       
            #"$sComputerName1 is Pinging"

            C:\SWKSoftware\PSEXEC.exe "\\$sComputerName1" -s -h -d powershell.exe "enable-psremoting" -force | Out-Null
         
             #"$sComputerName1 is Pinging" | Add-Content c:\Temp\CBSLogSize.txt

             Get-FolderSize -Computers $sComputerName1 -Path "C:\Windows\Logs\CBS"

             #Get-FolderSize -Computers $sComputerName1 -Path $Path

             " " | Add-Content c:\Temp\CBSLogSize.txt
         
        }

        Else

            {
                    "$sComputerName1 not pinging"
           
            }   
       
 }

#################################################################################


Note:- Please test script in your test environment before running in production directly.

No comments:

Post a Comment