Skip to content

Instantly share code, notes, and snippets.

@stevecharon
Forked from alexinnes/Get-VSSWriters.ps1
Last active August 12, 2021 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevecharon/56991709b4f2459eaaaf24f0e66ef4c3 to your computer and use it in GitHub Desktop.
Save stevecharon/56991709b4f2459eaaaf24f0e66ef4c3 to your computer and use it in GitHub Desktop.
Get-VSSWriters
function Get-VSSWriters {
<#
.Synopsis
Performs VSSAdmin list writers
.DESCRIPTION
Gets the VSS Writers and outputs it as a object.
.EXAMPLE
Get-VSSWriters
.NOTES
Uses regex to get the information and then splits it into objects.
Extend the Culture region if use anything beyond german or english or make sure to run in these Cultures
otherwise your result will be empty
#>
Begin {
#stolen from https://devblogs.microsoft.com/scripting/use-powershell-to-write-verbose-output/
if ($verbose){
$oldverbose = $VerbosePreference
$VerbosePreference = "continue"
}
#region Culture
$cult=(get-culture).Name
switch ($cult){
"de-DE" { #make sure to include ":" in the variable!
$writertext="Verfassername:"
$writerid="Verfasserkennung:"
$writerinstance="Verfasserinstanzkennung:"
$writerstate="Status:"
$writerlasterr="Letzter Fehler:"
}
Default {
$writertext="Writer name:"
$writerid="Writer Id:"
$writerinstance="Writer Instance Id:"
$writerstate="State:"
$writerlasterr="Last error:"
}
}
#endregion Culture
[string]$vssWriters = (vssadmin list writers) | Out-String
#Regex to findthe first line and then its proceeding 4 lines
$regex = [regex]::matches($vssWriters, "($writertext.*\n.*\n.*\n.*\n.*)")
$vssObject = @()
$VSSOutput = @()
}
Process {
#loop thought the regex matches and split them into their properties.
foreach($value in $reg.value){
Write-verbose $value
$vssObject = New-Object PSCustomObject -Property([ordered]@{
WriterName = ([regex]::Match($value,"$writertext (.*)")).groups[1].value
WriterId = ([regex]::Match($value,"$writerid (.*)")).groups[1].value
WriterInstanceId = ([regex]::Match($value,"$writerinstance (.*)")).groups[1].value
State = ([regex]::Match($value,"$writerstate \[(.)\]")).groups[1].value
StateInfo = ([regex]::Match($value,"$writerstate \[(.)\] (.*)")).groups[2].value
LastError = ([regex]::Match($value,"$writerlasterr (.*)")).groups[1].value
})
$VSSOutput += $vssObject
}
}
End {
$VerbosePreference = $oldverbose
$VSSOutput
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment