Trim() issue (trying to trim a string)

I have a powershell script whose output looks like below:

 

UserListFormat { SchemaVersion: 2, Mode: Open, Users: [("_John.J@SALES": UserEntry { Mode: Whitelist, FriendlyName:
“domain\John” }), ("_Jack@Marketing": UserEntry { Mode: Whitelist, FriendlyName: “domain\Jack” }), ("_Mike.K":
UserEntry { Mode: Whitelist, FriendlyName: “domain\Mike” })] }

 

I just need FriendlyName and its corresponding value as a list in the output and get rid of everything else. Below is the output I want to achieve.

FriendlyName: “domain\John”

FriendlyName: “domain\Jack”

FriendlyName: “domain\Mike”

 

I tried using trim() but its hard to achieve the desired output. I also tried select-string which doesn’t work.

Can someone help me achieve this?

 

Hi,

Have you tried:

$Output = “Your PS output with domain names”

([regex]’\w*\\w*’).Matches($Output) | ForEach-Object {$_.Value}`

It does assume your names don(t contain numbers and stuff. If so you’ll have to add those characters to the regex

Kris.

You can use

ConvertFrom-String

First create a template. You have to remove extra curly braces in the template.

$template = @'
UserListFormat  SchemaVersion: 2, Mode: Open, Users: [(“_John.J@SALES”: UserEntry  Mode: Whitelist, FriendlyName:
{FriendlyName*:“domain\John”} ), (“_Jack@Marketing”: UserEntry  Mode: Whitelist, FriendlyName: {FriendlyName*:“domain\Jack”} ), (“_Mike.K”:
UserEntry  Mode: Whitelist, FriendlyName: {FriendlyName*:“domain\Mike”} )] 
'@

Now let’s run it

@'
UserListFormat { SchemaVersion: 2, Mode: Open, Users: [(“_John.J@SALES”: UserEntry { Mode: Whitelist, FriendlyName:
“domain\John” }), (“_Jack@Marketing”: UserEntry { Mode: Whitelist, FriendlyName: “domain\Jack” }), (“_Mike.K”:
UserEntry { Mode: Whitelist, FriendlyName: “domain\Mike” })] }
'@ | ConvertFrom-String -TemplateContent $template -outvariable names

Output


FriendlyName

“domain\John”
“domain\Jack”
“domain\Mike”

It’s also captured in the $names variable and you can access the contents like this

$names.friendlyname
“domain\John”
“domain\Jack”
“domain\Mike”

And this is how you’d do the same thing reading from a file

get-content c:\temp\friendlynames.txt -raw | ConvertFrom-String -TemplateContent $template -OutVariable names

If your output is truly a single string with no line breaks or newline characters, you can do the following:

($YourString | Select-String -Pattern ‘FriendlyName:([^"]*?"){2}’ -AllMatches).Matches.Value