Importing modules when called from other scripts causes weird error

Hi guys, been a while since I posted - guess my code is getting better!!

I’ve been working on writing a huge DC promotion tool for our servers here, which is specific to Windows Server 2012 and 2016, in an attempt to provide a single script to promote either version to our root domain, or 4 child domains.

It basically does a whole bunch of validation, user input and reporting, and generates an answer file as it goes.

The issue I’m having however happens normally after a reboot. The script handles reboots in a simple drop-and-run type deployment, which writes a batch file to the Startup start menu folder. This just calls the dropper script again to resume from where it left off. When this happens, the first part of the script always loads in the required modules. When you manually kick the script off for the first time, it gets through this section just fine. However, when the script gets called through the batch file, it gives me a non-critical but nonthelesss weird error when trying to import modules. Specifically this error is generated by the ‘ServerManager’ PS module…

Exception calling "Translate" with "1" argument(s): "Some or all identity references could not be translated." At C:\Windows\system32\WindowsPOwershell\v1.0\Modules\servermanager\ServerManager.psm1:32 char 21

In all my years using PowerShell, I have never, not once, seen that message appear when trying to import modules. Let alone the servermanager module.

The code used to import is:

# Load modules
Write-Host "`nInitialising modules..." -foregroundcolor yellow
Import-Module servermanager | Out-Null
Add-WindowsFeature RSAT-AD-PowerShell,RSAT-AD-AdminCenter | Out-Null
Import-Module activedirectory,dnsclient | Out-Null

Basic stuff. Works fine when manually executing script, but if you kick it off outside PowerShell it throws up that weird error. Script continues and functions normally, I just dont like seeing sea’s of red text for no reason!

Any ideas?

Hi Greg,

Looking at the error message you posted, it shows that the error is generated by line 32 in the ServerManager.psm1 file.

Lines 26 to 33 of ServerManager.psm1 from a Windows Server 2016 box:

# S-1-5-32-559 => Performance Log Users
# S-1-5-32-573 => Event Log Readers
# S-1-5-32-580 => Remote Management Users
$groupNames = @()
foreach($sid in @("S-1-5-32-559", "S-1-5-32-573", "S-1-5-32-580"))
    $groupNames = @(((New-Object System.Security.Principal.SecurityIdentifier ($sid)).Translate([System.Security.Principal.NTAccount]).Value -split "\\+")[1]) + $groupNames

So basically it is saying that the translate method of the ‘System.Security.Principal.SecurityIdentifier’ object created at that line, fails to translate an identity.

My guess - with no way of knowing for sure as I have not hit this problem - is that this is a timing issue. That for some reason, at the time when the script is run from the startup folder, the system is not ready to translate those group SID’s into names.

I would try to introduce a delay in the script, prior to loading the module, to allow the boot process to get a bit further, before importing the modules.

Hi Chris, thanks for the insight. Not heard of this kind of thing before!

So you reckon if I add a delay - say about 10 seconds - before loading in the modules, it should be ok?

# Load modules
Write-Host "`nInitialising modules. Please wait..." -foregroundcolor yellow
Start-Sleep 10
Import-Module servermanager | Out-Null
Add-WindowsFeature RSAT-AD-PowerShell,RSAT-AD-AdminCenter | Out-Null
Import-Module activedirectory,dnsclient | Out-Null

Well it’s just a guess - but yes :slight_smile:
I would start out with a relatively high delay to see if that solves it, and if it does, tune it to something lower.

Or you could look for a dependency, say a particular service is running or you can sign-in to the DC.