Search code examples
powershellsccmwinpe

I prompt the user for a computer name during the WinPE phase of OSD


During the WinPE phase of an OSD Deployment, I launch a form for the user to enter a computer name. Part of the PS Script needs the ActiveDirectory Module imported however, I can't get the AD Module to import.

Import-Module (Join-Path $(Split-Path $env:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1)
Import-Module ActiveDirectory
File Name Set-OSDComputerNamePrompt-TST.ps1 Below is one of the Array's I'm adding
Import-Module ActiveDirectory
$ADSites = (Get-ADReplicationSite -filter *).Name

I have the PowerShell module added to the Boot.Wim file and I've copied the

Prompt for Computer Name form


Solution

  • WinPE is not normal Windows and the Active Directory commandlets are very special (they need a setup to work on non server OS) so this is a bad combination.

    As iRon commented there are ways to include this but those will always be an unsupported hack. If the PE Version changes it could be that it has to be done with different files or it could be that ne version just completely breaks the hack. You should not rely on something like this for OSD. (I don't know about your specific AD setup but for most of them it would be by far less work to hard code the domain names in the script and update the script on each site addition than to always keep up with the PE changes which are by design 3 times a year)

    The fallback for all AD related stuff that works without the module would be adsi which can work in PE (for Powershell to work in general you have to alter the boot image but those changes are supported by SCCM so they provide no additional work on release changes and you probably already did that to come this far)

    With adsi you can get a list of your sites like this:

    $sitesDN = "LDAP://CN=Sites," + $([adsi] "LDAP://RootDSE").Get("ConfigurationNamingContext")
    $ADSites = (([adsi]$sitesDN).psbase.children | where {$_.Objectclass -ieq "site"}).Name
    

    As a side note: I assume you have different sites for this code to be needed. If this is one of those cases where this method would be the "gold standard" because it works regardless of AD setup but you personally only have one site that is not likely to change (like a lot of people probably have) I would advise against such a complicated solution even if it is fail safe and just hard code the name. PE has lots of special cases and is hard to debug sometimes so reduce complexity if possible (as long as it let's you keep the same convenience of course).