Shahar
Shahar

Reputation: 501

Powershell finding simple one liner for trimming text inside file

I have lots of files like this:

<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <Obj RefId="0">
    <TN RefId="0">
      <T>System.Management.Automation.PSCredential</T>
      <T>System.Object</T>
    </TN>
    <ToString>System.Management.Automation.PSCredential</ToString>
    <Props>
      <S N="UserName">[email protected]</S>
      <SS N="Password">01000000d08c93c76b9</SS>
    </Props>
  </Obj>
</Objs>

i need to extract only the mail part : [email protected] i know i can use select-string and then trim from char to char, im looking for a simple one liner to find in each one of those files just the mail and trim it

$b = ($a | (select-string "username")).tostring()
$c = $b.trim(' ')

And so on.. Im sure theres a one liner to extract email from this but i cannot find it

Upvotes: 1

Views: 105

Answers (1)

iRon
iRon

Reputation: 23830

This appears to be a serialized PowerShell (PSCredential) object with a Password that is fabricated (or created under a different account):

MethodInvocationException: Exception calling "Deserialize" with "1" argument(s): "The parameter value "01000000d08c93c76b9" is not a valid encrypted string."

As you found yourself, if data comes from a file:

$Credential = Get-Credential
$Credential | Export-Clixml .\Credential.xml

You might import and deserialize the object using the Import-Clixml cmdlet:

$Credential = Import-clixml .\Credential.xml
$Credential.UserName
[email protected]

In case the serialized string is resident in a variable (e.g. $String):

$String = [System.Management.Automation.PSSerializer]::Serialize($Credential)

You might deserialize the string using the Deserialize method like:

[System.Management.Automation.PSSerializer]::Deserialize($String).Username
[email protected]

As it is xml based, you might also do this instead:

([Xml]$String).Objs.Obj.Props.S.'#text'
[email protected]

As a general note: it is usually a bad approach to directly peek and/or poke in serialized data using string cmdlets and/or methods along with Select-String and -Replace.

Upvotes: 2

Related Questions