Tuesday, July 22, 2014

#19 : Find files more than Modified date of a File

Sometime, we come to a situation where we have to purge some files. The File-Purge has one more trick that it must be greater than LastWriteDate of a file.

Below code might help -

ls | where {$_.LastWriteTime -ge (ls | where { $_.name -eq "somd.flg" } ).LastWriteTime }

Wednesday, July 16, 2014

#18 : How to find Disk Fragmentation with Powershell

Hi Guys,

While doing a research on other topic, I came to know that there is a way to find the disk fragmentation details with Powershell. Below lines of code are sufficient to find disk fragmentation.

$drive = Get-WmiObject -Class Win32_Volume -Filter "DriveLetter = 'e:'"
$report = $drive.DefragAnalysis() > $null
$report.DefragAnalysis


Output: 
__GENUS : 1
__CLASS : Win32_DefragAnalysis
__SUPERCLASS :
__DYNASTY : Win32_DefragAnalysis
__RELPATH : Win32_DefragAnalysis
__PROPERTY_COUNT : 27
__DERIVATION : {}
__SERVER : DIVINE
__NAMESPACE : ROOT\CIMV2
__PATH : \\DIVINE\ROOT\CIMV2:Win32_DefragAnalysis
AverageFileSize : 72
AverageFragmentsPerFile : 1
AverageFreeSpacePerExtent : 8503984128
ClusterSize : 4096
ExcessFolderFragments : 1
FilePercentFragmentation : 0
FragmentedFolders : 1
FreeSpace : 42684555264
FreeSpacePercent : 81
FreeSpacePercentFragmentation : 45
LargestFreeSpaceExtent : 23402860544
MFTPercentInUse : 100
MFTRecordCount : 44159
PageFileSize : 0
TotalExcessFragments : 1
TotalFiles : 32684
TotalFolders : 5792
TotalFragmentedFiles : 1
TotalFreeSpaceExtents : 5
TotalMFTFragments : 2
TotalMFTSize : 45219840
TotalPageFileFragments : 0
TotalPercentFragmentation : 0
TotalUnmovableFiles : 10
UsedSpace : 9744240640
VolumeName :
VolumeSize : 52428795904

Line#2 might take some longer time depending on the disk size. So, if you are planning to run scan on all the servers of your environment - it might take longer time. But, the results are extremely accurate.

Happy scripting!!

Tuesday, July 15, 2014

#17 : How to execute command on Remote machine?

Powersell-remoting is one of the useful feature which might play important role in your scripting. Sometimes, it becomes important to roam over all machines and fetch some information. Such as finding the CPU Utilization in all machines or finding the Machine-Type of all servers of your environment. If you have a limited number of servers, the task can be done manually. But, if you have to run the same thing over all the machines - task would not be that easier.

I had this requirement long back and now I want to share my experiences with all of my audiences.

I will write a detailed article on it very soon.

#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...