I am new to Powershell and am currently working on a script that writes the result of the wbadmin get versions command into a text file. The result contains two (or more) entries.
My Result (in german):
wbadmin 1.0 - Sicherungs-Befehlszeilentool
(C) Copyright Microsoft Corporation. Alle Rechte vorbehalten.
Sicherungszeit: 06.01.2022 10:02
Sicherungsziel: 1394/USB-Datenträger, Beschriftung Volume Ni-ssan Skyline(D:)
Versions-ID: 01/06/2022-09:02
Wiederherstellbar: Volume(s), Datei(en), Anwendung(en), Bare-Metal-Recovery, Systemstatus
Snapshot-ID: {a14340c6-b013-4f24-9e0c-01b508d32e73}
Sicherungszeit: 06.01.2022 15:43
Sicherungsziel: 1394/USB-Datenträger, Beschriftung Volume Ni-ssan Skyline(D:)
Versions-ID: 01/06/2022-14:43
Wiederherstellbar: Volume(s), Datei(en), Anwendung(en), Bare-Metal-Recovery, Systemstatus
Snapshot-ID: {eaac6691-2fc7-416c-aec2-b9c936908206}
Script for testing:
$patht = "C:\blaBla.txt"
$process = wbadmin get versions
New-Item $patht
Add-Content $patht -Value $Process
The problem is that I'm only interested in the second (06.01.2022 15:43) entry. Is there a way that only the wanted entry gets exported to my .txt File?
CodePudding user response:
I'm only interested in the second (06.01.2022 15:43) entry.
I suppose you mean you're only interested in the final entry of the list, not the entry with that particular date.
You can split at a double newline and take the last part:
$process = @"
wbadmin 1.0 - Sicherungs-Befehlszeilentool
(C) Copyright Microsoft Corporation. Alle Rechte vorbehalten.
Sicherungszeit: 06.01.2022 10:02
Sicherungsziel: 1394/USB-Datenträger, Beschriftung Volume Ni-ssan Skyline(D:)
Versions-ID: 01/06/2022-09:02
Wiederherstellbar: Volume(s), Datei(en), Anwendung(en), Bare-Metal-Recovery, Systemstatus
Snapshot-ID: {a14340c6-b013-4f24-9e0c-01b508d32e73}
Sicherungszeit: 06.01.2022 15:43
Sicherungsziel: 1394/USB-Datenträger, Beschriftung Volume Ni-ssan Skyline(D:)
Versions-ID: 01/06/2022-14:43
Wiederherstellbar: Volume(s), Datei(en), Anwendung(en), Bare-Metal-Recovery, Systemstatus
Snapshot-ID: {eaac6691-2fc7-416c-aec2-b9c936908206}
"@
$last = $process -split '\r?\n\r?\n' | select -Last 1
Write-Host $last
prints
Sicherungszeit: 06.01.2022 15:43
Sicherungsziel: 1394/USB-Datenträger, Beschriftung Volume Ni-ssan Skyline(D:)
Versions-ID: 01/06/2022-14:43
Wiederherstellbar: Volume(s), Datei(en), Anwendung(en), Bare-Metal-Recovery, Systemstatus
Snapshot-ID: {eaac6691-2fc7-416c-aec2-b9c936908206}
CodePudding user response:
I assume that wbadmin get versions returns an array of strings and in that case you can do the following:
# join the strings to form a single multilined string
$process = (wbadmin get versions) -join [environment]::NewLine
# split on the empty newlines to get text blocks.
$blocks = $process -split '(\r?\n){2,}' | Where-Object { $_ -match '\S'}
# now select the block that has the info for the date you are after
# since the string contains dots (that have special meaning in regex), you need to escape the search string
$search = [regex]::Escape('Sicherungszeit: 06.01.2022 15:43')
$blocks | Where-Object { $_ -match "(?m)^$search" } | Set-Content -Path "C:\blaBla.txt"
Result:
Sicherungszeit: 06.01.2022 15:43
Sicherungsziel: 1394/USB-Datenträger, Beschriftung Volume Ni-ssan Skyline(D:)
Versions-ID: 01/06/2022-14:43
Wiederherstellbar: Volume(s), Datei(en), Anwendung(en), Bare-Metal-Recovery, Systemstatus
Snapshot-ID: {eaac6691-2fc7-416c-aec2-b9c936908206}
