Pipeline to Split and Trim

The following code runs dsrgecmd to report if a Windows 10 is joined to Azure Active Directory.

The dsregcmd produces a bunch of lines of text, and I select the line with AzureADJoined and the try to split the line “AzureAdJoined : YES ” and then Trim it. I only care about the current state (YES or NO) coming back.

$text = dsregcmd /status | select-string -Pattern 'AzureADJoined'
$answer = ($text -split(":")).trim()[1]
echo $answer

I had hoped that I could pipeline the first right into the second line but I cannot figure out how to do the split in the pipeline not the subsequent trim of the second member of the split array.

This is an uneducated way to achieve what I want. Can any of you help sprinkle some magic pixie dust on this and help me understand how to better do this? I would appreciate any thoughts.

Thanks in advance!
Paul.

Paul,

I don’t know why you would consider some code better just because it uses the pipeline.

$text = 
    dsregcmd /status | 
        Select-String -Pattern 'AzureADJoined' | 
            ForEach-Object {($_ -split ':')[1].trim()}
$text
2 Likes

Million ways to skin this cat

$azureJoined = switch -Regex (dsregcmd /status){
     'AzureADJoined : (.+)' {
         $matches.1
     }
}

$azureJoined

You could turn the entire output of dsregcmd /status into an object (hash table) with just a bit more code.

$output = switch -Regex (dsregcmd /status){
    '^\W.+:' {$_ | ConvertFrom-StringData -Delimiter ':'}
}

$output.AzureAdJoined

Note the previous code is powershell core compatible. To use windows powershell, change it to the following.

$output = switch -Regex (dsregcmd /status){
    '^\W.+:' {$_ -replace ' : ','=' | ConvertFrom-StringData}
}

$output.AzureAdJoined
1 Like

Agreed - it is not necessarily better. I am just trying to wrap my mind around pipelines. Thank you!

Thank you KD!

#1 works perfectly
#2 not tested - don’t have core handy.
#3 works but throws the following error:

ConvertFrom-StringData : parsing “AzureAD\username, username@company.com” - Unrecognized escape sequence \P.
At line:2 char:39
+ ‘^\W.+:’ {$_ -replace ’ : ‘,’=’ | ConvertFrom-StringData}
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:slight_smile: [ConvertFrom-StringData], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromStringDataCommand

Where is the \P coming from?

The user contains a slash, which has special meaning in regex. Try this.

$output = switch -Regex (dsregcmd /status){
    '^\W.+:' {[regex]::Escape($_) -replace ' : ','=' | ConvertFrom-StringData}
}

$output.AzureAdJoined

This does not seem to work for me. I get a bunch of the following:

ConvertFrom-StringData : Data line ‘\ \ \ \ \ \ \ \ \ \ \ \ \ AzureAdJoined\ :\ YES’ is not in ‘name=value’ format.
At line:1 char:95

  • … :’ {[regex]::Escape($_) -replace ’ : ‘,’=’ | ConvertFrom-StringData}}
  •                                              ~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: ( : ) [ConvertFrom-StringData], PSInvalidOperationException
    • FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.ConvertFromStringDataCommand