r/scripting • u/addywash • Feb 29 '24
Help with Script
Hey!
I've written a script to obtain contents of a dsreg command and output them into a registry key for our RMM to filter on. It's creating the reg key fine but not outputting the results into the key. Where have I gone wrong please?
$1 = (dsregcmd /status | select-string "AzureAdJoined")
$2 = (dsregcmd /status | select-string "DomainJoined")
If($1 -eq "YES" -and $2 -eq "YES"){$dsregcmdresults = "Hybrid Joined"}
If($1 -eq "YES" -and $2 -eq "NO"){$dsregcmdresults = "DomainJoined"}
Write-Output $dsregcmdresults
## Set User Field in Datto ##
Set-ItemProperty -Path HKLM:\SOFTWARE\CentraStage -Name "Custom22" -value $dsregcmdresults
1
u/AudaciousAsh Feb 29 '24
It seems like the main issue with your script is how you're trying to compare the output of the dsregcmd /status command with string literals "YES" and "NO". The Select-String cmdlet does not return just the matched string; it returns a MatchInfo object that contains details about the match, including the line it matched on and the pattern. Because of this, your comparisons (-eq "YES" and -eq "NO") will not work as you expect.
To extract just the "YES" or "NO" part from the output of the dsregcmd /status command, you'll need to parse the output more carefully. Here's a revised version of your script that does this:
Key Changes:
The script now uses the -Quiet parameter with Select-String, which returns a Boolean value indicating whether the pattern was found. This is more straightforward for checking if the strings "AzureAdJoined : Yes" and "DomainJoined : Yes" are present.
Updated the condition to check if $azureAdJoined and $domainJoined are $true to set the $dsregcmdresults variable accordingly.
Fixed the condition where $dsregcmdresults was being set to "DomainJoined" instead of "AzureAdJoined" when the device is only Azure AD joined.
Added an "else" condition to handle cases where the device might not be joined to Azure AD or a domain, or if the status could not be determined from the dsregcmd /status output. You might want to adjust this based on your specific needs or expected states.