I’m new to the Powershell and would need some help with a problem. I created a function which returns an object with Infos about the Directory:
DATE: 10-12-2012
COMPUTER: PC1
DIRECTORY: C:\TEMP
FOLDERSIZE_IN_MB: 70
I loop through the directories to collect information of their sizes and export it to a CSV file once a week.
HERE STARTS MY PROBLEM:
I want to get some information about the dir growth. I started to write a script with import the oldest and newest CSV files.
$data="C:\LOG\Data"
$data= gci -path $data -filter "*.csv"
$temp=""
$old,$new=@()
foreach($item in $data){
If((Get-Date $item.LastWriteTime -format ("ddMMyyyy")) -gt $temp){
$new+= $item.FullName |Import-CSV -delimiter ";"
}
Elseif((Get-Date $item.LastWriteTime -format ("ddMMyyyy")) -lt $temp){
$old+= $item.FullName |Import-CSV -delimiter ";"
}
$temp=(Get-Date $item.LastWriteTime -format ("ddMMyyyy"))
}
How can I compare the two arrays to find equal dir vlaues in both and callculate with thier sizes?
I dont know how to check:
IF C:\TEMP in OLD and C:\TEMP in NEW then callulate (1-(SIZEOLD/SITZENEW))*100.
I would be nice to get an output like:
DATE: 10-12-2012
COMPUTER: PC1
DIRECTORY: C:\TEMP
FOLDERSIZE_IN_MB: 80,5
GROWTH_SINCE_LAST_SCAN: 15%
This is what I did to resolve my problem but I does not look solid and I do not know how to convert the hash back into an object to pipe the result into a csv.
$old=$old|Group-object Item
$new=$new|Group-object Item
$result1=compare $new $old -property Name -includeequal -passthru |WHERE {$_.Sideindicator -eq "=="}
$result2=compare $old $new -property Name -includeequal -passthru |WHERE {$_.Sideindicator -eq "=="}
for($i=0;$i -le $result1.count;$i++){
if($result1[$i].Name -contains $result2[$i].Name){
$Size2=($result2[$i].Group)| select-object -property FolderSize_in_MB
$Size1=($result1[$i].Group)| select-object -property FolderSize_in_MB
if(([int]$Size1.FolderSize_in_MB) -ne "0"){
$growth=(1-(([int]$Size2.FolderSize_in_MB)/([int]$Size1.FolderSize_in_MB)))*100
}
else{
$growth="0"
}
}
else{
}
if($result1[$i]){
$result1[$i].Group| ADD-Member NoteProperty Growth ("{0:n2}"-f $growth +"%")
}
}
The most forward way would be based on
gci | measure-object -sum length. The Scripting Guys have just done it that way.For homemade solution, I’d rather store directory names and sizes in a file. On next run, import the data and create a hashtable on its contents. Use full name of each dir as hash key and size as value. Read the current dir sizes and look old sizes from the hashtable. (You could serialize the hashtable, I propably would do that.)