Showing posts with label space. Show all posts
Showing posts with label space. Show all posts

Friday, September 12, 2014

Powershell: get free disk space from computers listed in text file. using Job


Purpose: This script will search a list of computers and report their c drives total and free space into a text file that can be imported into excel.

This is my first time using jobs to speed up the process of gathering the info so there could be a better way of doing this but this seems to work just fine.

cls
#Get free disk space code from here.
#http://www.codeproject.com/Articles/757946/Retrieve-Disk-Space-of-Remote-Computers-using-Powe




$Global:Servers = Get-Content "C:\temp\computers.txt"
$Global:ReportPath = "C:\temp\freediskspace.txt"

$scriptblock = {
       param($server)
    $ConvertToGB = (1024 * 1024 * 1024)
       $pingResult = Test-Connection -count 1 -ErrorAction SilentlyContinue $server | select IPV4Address
       $disk = Get-WmiObject Win32_LogicalDisk -ComputerName $Server -Filter "DeviceID='C:'" | Select-Object Size,FreeSpace
       $values = @()
       if($Error){
            if( $Error -like "*Access is denied*"){
                return ($server+",,AccessDenied")
            }
            $Error.Clear()
            return ($server+",,")
       }
        
                
       $values += ($Server + "," + ($disk.Size / $ConvertToGB) + "," + ($disk.FreeSpace / $ConvertToGB) )
             
       return $values
}

function getdata{
    $jobs = Get-Job | ? { $_.State -eq "Completed" }
     foreach( $job in $jobs){
        $results = Receive-Job $job
       Add-Content $ReportPath $results
        Remove-Job $job
     }   
}


Add-Content $ReportPath "Server,Total,Free"
foreach($server in $Servers){
        
     while( (Get-Job).count -ge 50 ){
            sleep -Seconds 1
            getdata
     }

     Start-Job -ScriptBlock $scriptblock -ArgumentList $server
}

while( (get-job).count -ne 0 ){
      sleep -Seconds 1
            getdata
            
 }
 

Thursday, March 15, 2012

Batch delete files older then X days using forfiles : updated!


Purpose:
This batch example will search for files with the extension .txt and deletes file less then 5 days old.

Required:
----------------------------------------------------
Forfiles.exe
/p  The path to search
/s Recurse into sub-folders
/M Mask
/C command The command to execute for each file

/D - dd      Select files with a last modified date less than or
                equal to the current date minus "dd" days. (in the past)
-----------------------------------------------------
Del.exe
-----------------------------------------------------

Code:

Echo on
Forfiles /P c:\temp\ /S /M *.txt /C "cmd /c del @path" /D -5
If you have a space in the path the only way i found forfiles.exe to work is the following example

if a file is older then 30 days in e:\example space then delete
forfiles -p "e:\example space" -m *.* -s -d -30 -c "cmd /C del @FILE"




Powershell: Microsoft Graph to add new roles to application registration

PowerShell Script Bulk add new roles to application registration in azure. Update $roles with ,Us...