Jump to content

Verify Azure membership


Recommended Posts

I am trying to create a script which would run a specific task if the computer is joined to the domain.  in order to do that, i run dsregcmd.exe, which is available in Windows 10 in c:\windows\system32\.  in order to get the output the utility needs to be ran with /status option.  when i run it from command prompt or from power shell it works fine, but when i try to make it part of a Autoit (!) script it fails to output anything, though a blank file is being created.  The same happens if i create a batch file and try to run it form autoit script.  
Any suggestions?  i just need to verify Azure membership and do a runwait.  
 

What i have tried so far.

$commandline="c:\windows\system32\dsregcmd.exe /status>c:\temp\intune.txt"
RunWait(@ComSpec & " /c " & $commandline,"c:\windows\system32\")

or

#include <Constants.au3>
Local $foo = Run("c:\windows\system32\dsregcmd.exe /status", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
ProcessWaitClose($foo)
Local $sOutput = StdoutRead($foo)
MsgBox(0,"",$sOutput)
 

Edited by YFridman
Link to comment
Share on other sites

I am running everything elevated already.  #requireadmin just checks whether or not it's running elevated.  The original script i made, which executes those, has #requireadmin on it already.   
Since the original message i added cmd /c to the command line in the batch file and it says now "'c:\windows\system32\dsregcmd.exe' is not recognized as an internal or external command, operable program or batch file." but i can see it.  it's a local file, so the network rights are not at play here.

Edited by YFridman
Link to comment
Share on other sites

I was testing this out and was just about to suggest what @rsn did. It seems like it's a x64 thing. This worked for me:

#AutoIt3Wrapper_UseX64=y
#include <Constants.au3>
Global $sCommandLine = ' "C:\Windows\System32\dsregcmd.exe" /status ' ; >C:\GlobalDocs\intune.txt
Global $sWorkingDir = 'C:\WINDOWS\system32' ; @SystemDir
Global $iPid = Run(@ComSpec & " /c " & $sCommandLine, $sWorkingDir, @SW_HIDE, BitOR($RUN_CREATE_NEW_CONSOLE, $STDERR_MERGED))
ProcessWaitClose($iPid)
ConsoleWrite(StdoutRead($iPid) & @CRLF)

Or:

#AutoIt3Wrapper_UseX64=y
#include <Constants.au3>
Global $sOutputFile = 'c:\temp\intune.txt'
Global $sCommandLine = 'c:\windows\system32\dsregcmd.exe /status>' & $sOutputFile
Global $iPid = Run(@ComSpec & " /c " & $sCommandLine, @SystemDir, @SW_HIDE, BitOR($RUN_CREATE_NEW_CONSOLE, $STDERR_MERGED))
ProcessWaitClose($iPid)
ConsoleWrite('StdoutRead: ' & @CRLF & StdoutRead($iPid) & @CRLF)
ConsoleWrite('FileRead: ' & @CRLF & FileRead($sOutputFile) & @CRLF)

 

Edited by mistersquirrle

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

Here's what I came up during my quick little test with but I do like what @mistersquirrle came up with: elegant!
 

#AutoIt3Wrapper_UseX64=y

#include <AutoItConstants.au3>
#include <StringConstants.au3>

$iPID = Run( @ComSpec & " /c dsregcmd.exe /status" , @SystemDir , @SW_HIDE , $STDERR_MERGED )

While ProcessWaitClose($iPID)
    $sMsg = StdoutRead($iPID)
    $sMsg = StringStripWS ( $sMsg , $STR_STRIPLEADING +  $STR_STRIPTRAILING  )
    If @error Then ExitLoop
    If Not $sMsg = 0 Then ExitLoop
WEnd


MsgBox (4096 , @ScriptName , $sMsg )

 

Link to comment
Share on other sites

one more question related to this.  I had been using AutoIT for a while - over 10 years for sure.  i always installed the 32bit version previously because we always had either just 32bit or a mix of 32bit and 64bit computers, Now for the first time all our computers are with 64bit Windows, so i installed the 64bit version of AutoIT.  Would i had the same issue if i had installed the 32bit version as well?

Link to comment
Share on other sites

Unless I'm mistaken, AutoIt only comes in x86. Compiling, however, can be x86 or x64. Personally I only compile as x64. I have no x86 clients and newer versions of Windows are and will be x64 only (i.e., Windows 11).

Link to comment
Share on other sites

When you install AutoIt it only asks you what you want to use/compile/run as Default. I don't think that it forces you into only installing x86 or x64 (or both). I don't know what would happen in a 32-bit system installation of AutoIt. I assume that the x64 binaries and data would still be there, they just obviously could not run.

 

As for trying to run this dsregcmd.exe on a 32-bit system... you just wouldn't be able to. You'd have to find some other way to get the information that you're looking for, if that's even supported on 32-bit systems.

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

#AutoIt3Wrapper_UseX64=n ; running x86

#include <WinAPIFiles.au3>
_WinAPI_Wow64EnableWow64FsRedirection(False) ; this does it  =)

; ------ your code from the first post --------
#include <Constants.au3>
Local $foo = Run(@WindowsDir & "\system32\dsregcmd.exe /status", @TempDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
ProcessWaitClose($foo)
Local $sOutput = StdoutRead($foo)
MsgBox(0,"",$sOutput)

:) 

Edited by argumentum
better code

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

You only need it in a while loop if the process is running and you want/need to get data while it's running. Or if you wanted to do something else in the loop while it's running. In @argumentums example and mine, ProcessWaitClose is used. So it waits until the output is done, then gets it all at once. If it was a process that ran for 10 minutes and you wanted status updates or get the output while it's running, then yes, use StdoutRead in a loop to get data as it's printed.

These are the same:

3 hours ago, argumentum said:
#AutoIt3Wrapper_UseX64=n ; running x86

#include <WinAPIFiles.au3>
_WinAPI_Wow64EnableWow64FsRedirection(False) ; this does it  =)

; ------ your code from the first post --------
#include <Constants.au3>
Local $foo = Run(@WindowsDir & "\system32\dsregcmd.exe /status", @TempDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
ProcessWaitClose($foo)
Local $sOutput = StdoutRead($foo)
MsgBox(0,"",$sOutput)

And:

#AutoIt3Wrapper_UseX64=n ; running x86

#include <WinAPIFiles.au3>
_WinAPI_Wow64EnableWow64FsRedirection(False) ; this does it  =)

; ------ your code from the first post --------
#include <Constants.au3>
Local $foo = Run(@WindowsDir & "\system32\dsregcmd.exe /status", @TempDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
Local $sOutput
While ProcessExists($foo)
    Sleep(10)
    $sOutput &= StdoutRead($foo)
    If @error Then
        ExitLoop
    EndIf
WEnd
$sOutput &= StdoutRead($foo)
MsgBox(0,"",$sOutput)

Obviously argumentum's is shorter and bit cleaner, however if it wasn't a process that ends in under a second, you'd probably want to go with the loop method so you can get status updates and whatnot.

Edited by mistersquirrle

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

Was just thinking about this some more, and depending on what you're doing, it would also probably be a good idea to call StdioClose after you get the output, otherwise the handle to the process may remain open until your script closes.

This would only be needed if you're launching thousands of 'Run's/commands and reading the output from them. I imagine that it would cause a 'memory leak', though I don't know how well Windows and/or AutoIt handles 'dead' process handles. It's a similar question in nature to: 

I think the same thing applies here as my comment in there does. Is it necessary to do? Nah, probably not. Is it hurting you not doing it? Only if you're opening thousands/millions of processes and reading the output over the life of your process.

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

  • 3 months later...

Since you want to check if the computer is joined to the domain using dsregcmd.exe, you can use the following AutoIt script to achieve your goal:

autoit
Copy code
#include <Constants.au3>

Local $commandLine = "c:\windows\system32\dsregcmd.exe /status"
Local $outputFile = "c:\temp\intune.txt"

Local $foo = Run(@ComSpec & " /c " & $commandLine & " > " & $outputFile, @SystemDir, @SW_HIDE, $STDOUT_CHILD)
ProcessWaitClose($foo)

Local $fileContent = FileRead($outputFile)
FileDelete($outputFile)

If StringInStr($fileContent, "AzureAdJoined : YES") Then
    ; Your code to run the specific task when the computer is joined to Azure AD.
    MsgBox(64, "Domain Membership", "Computer is joined to Azure AD.")
Else
    ; Your code when the computer is not joined to Azure AD.
    MsgBox(48, "Domain Membership", "Computer is not joined to Azure AD.")
EndIf


This script will run dsregcmd.exe /status and redirect the output to a temporary file named intune.txt. It will then read the content of the file to check if "AzureAdJoined : YES" is present, indicating that the computer is joined to Azure AD. Depending on the result, it will show appropriate messages using MsgBox.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...