Brink
Brink

Reputation: 67

How to get a object member in string format

I'm not at all sure what the heck I'm doing wrong, I'm running the manage-bde (command line to return bitlocker encryption status) and piping it to a select-string to catch the specific encryption percentage/status with a very specific regex. Select string for some reason returns the whole line, but a property called "Matches" returns the EXACT information I need, but it's a Matchinfo object. There's a method called ToString but it doesn't return at all what I need, seems to be the object info. I just want the output in string format to display in a write-progress gui.

note: quite new to powershell still.

$act = Invoke-Expression -Command "manage-bde -status C:" | select-string -Pattern "(?<=(Percentage Encrypted:)).*"  | Select-Object -Property Matches -Last 1

$act.toString()| Out-Host

Upvotes: 1

Views: 1500

Answers (4)

upioneer
upioneer

Reputation: 1

my answer is based on vasili syrakis' post. got to this page with the exact question as the OP.

$encryptionstatus = [string[]](manage-bde -status c:)
$encryptionstatus[9] | Out-Host

Upvotes: 0

Vasili Syrakis
Vasili Syrakis

Reputation: 9611

You can make almost anything a string by using the following:

[string](code_to_be_parsed)

Example

I can create a string array from the ping command:

$test = [string[]](ping google.com)

Which gives me the result:

# empty line
Pinging google.com [74.125.200.101] with 32 bytes of data:
Reply from 74.125.200.101: bytes=32 time=302ms TTL=36
Reply from 74.125.200.101: bytes=32 time=302ms TTL=36
Reply from 74.125.200.101: bytes=32 time=302ms TTL=36
Reply from 74.125.200.101: bytes=32 time=306ms TTL=36

Ping statistics for 74.125.200.101:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 302ms, Maximum = 306ms, Average = 303ms

When I do a $test.count it returns 11, which is in fact the exact number of lines, each being a string inside of the array.

Upvotes: 2

Χpẘ
Χpẘ

Reputation: 3451

One of the design principles of Powershell is that most everything can be treated as an object (versus as text as in other administrative scripting languages). Since manage-bde is not a Powershell tool (rather a plain ol' command line tool) it doesn't work with PS as easily as a tool designed for PS. In the case of BitLocker there are dozen or so PS cmdlets. In particular Get-BitLockerVolume sounds like a near equivalent to manage-bde -status

However if you want to stick with manage-bde, then you can use the out-string cmdlet to generate string output from objects. So the following may be what you want.

$act = Invoke-Expression -Command "manage-bde -status C:" | 
   select-string -Pattern "(?<=(Percentage Encrypted:)).*"  | 
   Select-Object -Property Matches -Last 1 |
   out-string

$act| Out-Host

Upvotes: 2

mjolinor
mjolinor

Reputation: 68341

MatchInfo objects can be confusing. Try this:

$act = Invoke-Expression -Command "manage-bde -status C:" |
 select-string -Pattern "(?<=(Percentage Encrypted:)).*"  |
 Select-Object -Property Matches -Last 1

$act.matches[0].value

Alternatively:

(manage-bde -status C:) -match '^\s+Percentage Encrypted:' -replace '\D+(\d+%)','$1'

Upvotes: 1

Related Questions