简体   繁体   中英

Powershell | data growth query

I'm trying to write a script that measures data growth

I make a query and write an initial csv. Tthen I compare this once a week with a new query.

This is the compare skript:

$vergleich = compare $initial $list -Property SizeRemaining -IncludeEqual -PassThru | Where-Object {$_.SideIndicator -ne "=="}| Select-Object Server,Laufwerksbuchstabe,SizeRemaining,SideIndicator,Datum | Sort-Object -Property Server -Descending
$vergleich | Export-Csv -Delimiter ';' -Append  -Path C:\Administrativ\Festplattenanalyse.csv -NoTypeInformation 

This is the Output:

在此处输入图像描述

now my question: how can I eg the difference from the hard disk C from the TS-1 in a new column or best create a new csv where each hard disk is listed with the data growth?

Any idea?

Personally I really dislike resorting to for loops when using PowerShell, however I could not think up a better way to tackle this at the moment.

The following code does the following:

  • Sort the objects by date using Sort-Object Date so that objects are in chronological order
  • use Group-Object to group the data by server and drive letter
  • loop through each group ($_.Group) using a for loop so that it is easy to access the previous object for comparing
  • inside the loop:
    • if $i = 0, add the 'Diff' property with no value (-)
    • for the rest, get the difference in size between the current object and the previous object
# BEGIN TEST DATA
$data = @'
Server,Size,Date,Drive
TS-2,1400,02.01.2022,C
TS-1,400,05.02.2022,C
TS-1,2400,05.03.2022,C
TS-2,700,13.02.2022,C
TS-1,100,2022-jan-22 00:00:00,C
'@ | ConvertFrom-Csv | Select-Object *, @{n = 'Date'; e = { $_.date -as [datetime] } } -ExcludeProperty Date
# END TEST DATA

$data | Sort-Object Date | 
    Group-Object -Property Server, Drive | 
        ForEach-Object {
            for ($i = 0; $i -lt $_.Group.Count; $i++) {
                $currentItem = $_.Group[$i]
                if ($i -eq 0) {
                    $currentItem | Add-Member -NotePropertyName 'Diff' -NotePropertyValue '-' -PassThru
                }
                else {
                    $previousItem = $_.Group[$i - 1]
                    $diff = $currentItem.Size - $previousItem.Size
                    $currentItem | Add-Member -NotePropertyName 'Diff' -NotePropertyValue $diff -PassThru
                }
            }
        } | Format-Table

OUTPUT

Server Size Drive Date                Diff
------ ---- ----- ----                ----
TS-1   100  C     22.01.2022 00:00:00 -
TS-1   400  C     05.02.2022 00:00:00 300
TS-1   2400 C     05.03.2022 00:00:00 2000
TS-2   1400 C     02.01.2022 00:00:00 -
TS-2   700  C     13.02.2022 00:00:00 -700

To output the data to csv file, replace Format-Table in the last line with Export-Csv -Path C:\Administrativ\Festplattenanalyse_diff.csv -NoTypeInformation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM