0

I am configuring windows 11 clients as part of a project to migrate our infrastructure to intune. As a part of this im running into trouble with a powershell script, that work on our non intune domain clients. I need to check wether the email adress includes the string "@stu.domain" or if its only "@domain". My script is the following:

# Define the path to the executable
$exePath = "C:\Program Files\uniFLOW SmartClient\momsmartclnt.exe"
$currentUser = ([adsi]"LDAP://$(whoami /fqdn)").mail

# Check if the file exists and wether user is a teacher
if (Test-Path $exePath) {
    # Run the executable
    if($currentUser -notLike "*stu.myDomain*") {
        Start-Process -FilePath $exePath
    }   
} else {
    Write-Host "File not found: $exePath"
}

Now when I run this on my uniflow registered client i get the following error:

Powershell Error

This translates to: "The FQDN could not be requested, because the current user is not a domain user.

However when I run the whoami command i get the domain/username, but not the full email.

Am I confusing something here or is there a better way to get the full user email from the client?

Thank you in advance for any help / suggestions you may provide.

1 Answer 1

0

Welcome to the Stack overflow community👋

Most likely, you run into the problem because LDAP cannot find your FQDN and is unable to match it with an existing user. This might be due to several issues.

You can work around this by searching the AD for your username using the built-in adsisearcher. You can access your username using the environment variable. Keep in mind that this will point to the username of the user that is running the script, which may not be the user currently logged in.

Also, I would recommend that you use REGEX for your pattern matching so your script can be more easily modified to cope with more complex pattern matching requirements.

# Define the path to the executable
$currentUser = [adsisearcher]"(samaccountname=$env:USERNAME)"
$mail = $currentUser.findOne().Properties.mail

# Check if the file exists and wether user is a teacher
if ($true) {
    # Run the executable
    if($mail -match ".*@stu\myDomain\.ch") {
        Write-Host "matched"
    } else {
        Write-Host "not matched"
    }  
} else {
    Write-Host "file does not exist"
}

Please refer to our how to ask a good question guide to get suggestions on how you could improve your future questions. Your ServerFault journey is just yet beginning 🙌

2
  • Thank you for your response. This has already brought me significantly closer to a solution, as this work well if im logged in as a teacher. However for some reason a student gets an error when running : "Exception when running "FindOne" with 0 Arguments: "The Domain does not exist or no connection could be established. I will Update when I find a solution.
    – Samuel
    Commented Jun 12 at 13:46
  • Also I will endevour to take your feedback into account for future posts.
    – Samuel
    Commented Jun 12 at 13:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .