Formatting Output of dsregcmd /status

This topic contains 9 replies, has 6 voices, and was last updated by  amit aman 2 months, 3 weeks ago.

  • Author
    Posts
  • #94776

    amit aman
    Participant

    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

    Olaf Soyk
    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

    Richard Siddaway
    Moderator

    Look at ConvertFrom-String as a possible solution

    • #94855

      amit aman
      Participant

      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
    • #94860

      amit aman
      Participant

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

  • #94825

    Anders
    Participant

    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

      amit aman
      Participant

      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

    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

      amit aman
      Participant

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

You must be logged in to reply to this topic.