Guidelines

This site is for tech Q&A. Please keep your posts focused on the subject at hand.

Ask one question at a time. Don't conflate multiple problems into a single question.

Make sure to include all relevant information in your posts. Try to avoid linking to external sites.

Links to documentation are fine, but in addition you should also quote the relevant parts in your posts.

1 vote
157 views
157 views

I need a table from vssadmin list writers with PowerShell and I only need the writer ID from the list of all the writers.

I tried your solution as below for shadowstorage but I was not able to get it work. Is it possible to help me sorting this out?

$pattern = 'for volume: \((.*?)\)[\s\S]*?' +
           'used.*?space: (\d+.*?b)[\s\S]*?' +
           'allocated.*?space: (\d.*?b)'

& vssadmin list shadowstorage | Out-String |
  Select-String $pattern -AllMatches |
  Select-Object -Expand Matches |
  ForEach-Object {
    New-Object -Type PSObject -Property @{
      ComputerName   = $env:COMPUTERNAME
      Drive          = $_.Groups[1].Value
      UsedSpace      = $_.Groups[2].Value
      AllocatedSpace = $_.Groups[3].Value
    }
  }
in Scripting
edited by
by (5)
1
edit history

Please log in or register to answer this question.

1 Answer

0 votes

You don't need the overhead of creating custom object if you just want a list of single values. Simply matching lines with the string "Writer Id:" should give you just the lines with the IDs:

& vssadmin list writers | Select-String -Pattern 'Writer Id:'

By using a positive lookbehind assertion (?<=...) in the pattern you could even extract just the ID from the matching lines:

& vssadmin list writers |
    Select-String -Pattern '(?<=Writer Id: ).*' |
    Select-Object -Expand Matches |
    Select-Object -Expand Value

Alternatively you could use a combination of the -match and -replace operators to the same end:

(& vssadmin list writers) -match 'Writer Id:' -replace '^.*: '

The above matches lines containing the string "Writer Id:" and then removes everything from the beginning of the line (^) up to and including the colon and space, leaving just the ID.

by (115)
2 18 33
edit history
...