我想专门为“ vssadmin list writers”的输出选择“ Writer name”,“ Writer Id”和“ Writer Instance ID”,我需要一些有关如何使用正则表达式创建Powershell对象的帮助。一些帮助
PS C:\Windows\system32> $list = vssadmin list writers
PS C:\Windows\system32> $list
vssadmin 1.1 - Volume Shadow Copy Service administrative command-line tool
(C) Copyright 2001-2012 Microsoft Corp.
Writer name: 'Task Scheduler Writer'
Writer Id: {d61d61c8-d73a-4eee-8cdd-f6f9786b7124}
Writer Instance Id: {1bddd48e-5052-49db-9b07-b96f96727e6b}
State: [1] Stable
Last error: No error
Writer name: 'VSS Metadata Store Writer'
Writer Id: {75dfb225-e2e4-4d39-9ac9-ffaff65ddf06}
Writer Instance Id: {088e7a7d-09a8-4cc6-a609-ad90e75ddc93}
State: [1] Stable
Last error: No error
Writer name: 'Performance Counters Writer'
Writer Id: {0bada1de-01a9-4625-8278-69e735f39dd2}
Writer Instance Id: {f0086dda-9efc-47c5-8eb6-a944c3d09381}
State: [1] Stable
Last error: No error
Writer name: 'System Writer'
Writer Id: {e8132975-6f93-4464-a53e-1050253ae220}
Writer Instance Id: {cb8da305-5a18-4bf9-91e6-981bcf836c7b}
State: [9] Failed
Last error: Timed out
如果需要对象,可以使用以下方法:
$cVssOutput = vssadmin list writers
# Creating an empty collection.
$cVssWriters = @()
foreach ($line in $cVssOutput) {
$cNameValue = $line.Trim() -split ': '
if ($cNameValue[1] -ne $null) {
if ($cNameValue[0] -eq 'Writer name') {
# New writer. Initializing a new object.
$oVssVriter = New-Object PSObject
}
# Formatting and adding properties.
$sName = $cNameValue[0] -replace " ", ""
$sValue = $cNameValue[1] -replace "'", ""
$oVssVriter | Add-Member -Type NoteProperty -Name $sName -Value $sValue
if ($cNameValue[0] -eq 'Last error') {
# All properties in place.
# Adding the object to the collection.
$cVssWriters += $oVssVriter
}
}
}
现在,您可以选择所需的属性,例如:
$cVssWriters | % { $_.WriterName }
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句