Environment variables not recognized when compiled

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
MoneySolver
Posts: 7
Joined: Tue Dec 31, 2019 7:43 am

Environment variables not recognized when compiled

Post by MoneySolver »

I'm trying to dynamically leverage the currently connected domain controller by using the environment variable $env:LOGONSERVER.
More specifically like
$DomainController = ($env:LOGONSERVER).Replace("\\", "")

That works in every powershell scenario except when I compile it as a Windows Service. At that point it returns a NULL value. Windows event logs as well as my own transcript logs show it happening as such. Am I missing something?

Side note: I don't want to use the command get-addomaincontroller because I want this to be able to run on any machine of my choosing w/o the need for AD PS Module.

jvierra
Posts: 14621
Joined: Tue May 22, 2007 9:57 am
Answers: 3
Has voted: 1 time
Been upvoted: 3 times
Contact:

Re: Environment variables not recognized when compiled

Post by jvierra »

Your code is just wrong both syntactically and logically.
Here are valid methods:

Code: Select all

$domainController = $env:LOGONSERVER -replace '\\\\'
$domainController = $env:LOGONSERVER -replace '\\+'
$domainController = $env:LOGONSERVER.Replace( '\','')

MoneySolver
Posts: 7
Joined: Tue Dec 31, 2019 7:43 am

Re: Environment variables not recognized when compiled

Post by MoneySolver »

Thanks for the suggestion but none of the three versions you provided worked. They all returned the same error as my version. "Cannot validate argument on parameter 'ComputerName'. The argument is null or empty".

Can you explain why my method was syntactically wrong when it worked in every powershell scenario besides being compiled as a service?

Just to add, if I even try doing something as simple as write-host "Test $env:logonserver" I get no data in the variable and get the output "Test ".

I guess I can provide more context as to the command I'm trying to run
  1. $domainController = $env:LOGONSERVER.Replace( '\','')
  2.  
  3. invoke-command -computername $domainController  -scriptblock {[extra code here]}

jvierra
Posts: 14621
Joined: Tue May 22, 2007 9:57 am
Answers: 3
Has voted: 1 time
Been upvoted: 3 times
Contact:

Re: Environment variables not recognized when compiled

Post by jvierra »

Then you environment variable is empty. You will need to discover why.

Use a message box to display the contents of the variable. When running as a service there is no Logon variable. Service run under the system account and the system is not logged onto anything. It is joined. Try running the service under a full domain user account.

MoneySolver
Posts: 7
Joined: Tue Dec 31, 2019 7:43 am

Re: Environment variables not recognized when compiled

Post by MoneySolver »

I could kind of understand that may be the cause because the service was running under system but after changing the service to run under a domain account, it still could not pull that environment variable. I decided to abandon the dynamic method for now and just leverage the config file by having the domain controller specified in that. It's going to be a server application with proper ntfs permissions applied so I'm not worried about specific things being in the config file.

Thanks for the assistance.

jvierra
Posts: 14621
Joined: Tue May 22, 2007 9:57 am
Answers: 3
Has voted: 1 time
Been upvoted: 3 times
Contact:

Re: Environment variables not recognized when compiled

Post by jvierra »

You will have issues if the profile is not loaded by the service.

It is also a very bad idea to specify a server for a command. If the logon server is an RODC or is unavailable then the command will fail. Let windows find a server automatically. It will always pick the best DC for the command and will resolve all issues.

Locked