Formatting Output of dsregcmd /status

Welcome Forums General PowerShell Q&A Formatting Output of dsregcmd /status

This topic contains 9 replies, has 6 voices, and was last updated by

 
Participant
8 months, 3 weeks ago.

  • Author
    Posts
  • #94776

    Participant
    Points: 1
    Rank: Member

    Hi team,

    Any suggestion on how to convert the output of dsregcmd /status into an object? Or, any PowerShell alternative to find if a machine has joined AzureAd or not.

    I am getting an output something like this:

    PS C:\> dsregcmd /status

    +———————————————————————-+
    | Device State |
    +———————————————————————-+

    AzureAdJoined : YES
    EnterpriseJoined : NO

    I have to further put an if condition using AzureAdJoined : YES parameter, but I am not able to select it, as it is not a property.
    Any help on filtering or formatting?

    Thanks,

  • #94779

    Participant
    Points: 165
    Helping Hand
    Rank: Participant

    If you have only the "strings" from the command you have to parse it by yourself. As far as I know there is no built in magic what transforms this to a Powershell object. So catch the text and use regex to cut it into suitable pieces.

  • #94815

    Participant
    Points: 0
    Rank: Member

    Look at ConvertFrom-String as a possible solution

    • #94855

      Participant
      Points: 1
      Rank: Member

      Thanks Richard, that helped. One question, when I used the code:

      dsregcmd /status | ConvertFrom-String | where P2 -eq 'AzureAdJoined'

      It's generating properties as P1, P2, P3 and P4. Just wanted to understand if they are random or consistent so that I can hard-code it to my script?

  • #94818
    Jon

    Participant
    Points: 26
    Rank: Member
    • #94860

      Participant
      Points: 1
      Rank: Member

      Thanks Jon, It was a good read. Got multiple solutions for one scenario now.. 🙂

  • #94825

    Participant
    Points: -18
    Rank: Member

    Hello,

    I made a little a function with help from the link that Jon posted. Thanks for the link, was a good read.

    function Get-DSregcmdstatus {
        
        $status = dsregcmd /status 
        $status -replace ':', ' ' | 
            Select-Object -Index 5, 6, 7, 13, 14, 15, 16, 17, 18, 19, 25, 26, 27, 28, 29, 30, 31 | 
            ForEach-Object {$_.Trim() }  | 
            ConvertFrom-String -PropertyNames 'State', 'Status'
    } 
    • #94858

      Participant
      Points: 1
      Rank: Member

      Thanks Anders, That was help. I found another solution to it meanwhile :). I know it's not a formatting solution, however it worked for a temporary break-fix.

      $status = dsregcmd /status | Select-String -Pattern "AzureAdJoined"
      
          if ($status -match "AzureAdJoined : YES")
              {
                  [System.Windows.MessageBox]::Show('Your computer is already a member of AzureAd...') 
              }
          
          else
          {
  • #94837
    js

    Participant
    Points: 224
    Helping Hand
    Rank: Participant

    Check it out! I had to do 3 examples.

    $template = @'
            AzureAdJoined : {AzureAdJoined*:YES}
         EnterpriseJoined : {EnterpriseJoined:NO}
            AzureAdJoined : {AzureAdJoined*:NO}
         EnterpriseJoined : {EnterpriseJoined:YES}
            AzureAdJoined : {AzureAdJoined*:NO}
         EnterpriseJoined : {EnterpriseJoined:NO}
    '@
    
    PS C:\> dsregcmd /status | ConvertFrom-String -TemplateContent $template
    
    AzureAdJoined EnterpriseJoined
    ------------- ----------------
    NO            NO
    
    • #94861

      Participant
      Points: 1
      Rank: Member

      Great JS, thanks a lot. I can see multiple solutions now... Happiest I am... 🙂

The topic ‘Formatting Output of dsregcmd /status’ is closed to new replies.