Scenario:
- Newly imaged PC not joined to the domain logged in as a Local Admin (Username = User1)
- Run a batch file to start a PowerShell Script as a Domain User (net use etc.. powershell.exe -excutionpolicy bypass -file c:local file copied as part of the image)
- PowerShell script 1:
- sets up credentials and adds PC to domain
- asks for username of person PC is being built for and then adds to Adminsitrators and Remote Desktop Groups
This all works great. What i am trying to do is automate the build process for our ConferenceRoom PC's. The ConferenceRoom PC's all use a single Domain account called ConferenceRoom, but in order to log into a PC using ConferenceRoom, the PC name has to be added to the attribute "UserWorkstations". I wrote the following code that gets the current list of PC's names listed in that attribute using ADSI and then adds the PC the code is run on.
$PCUser = Read-Host 'PLEASE ENTER THE USERNAME OF THE PERSON YOU ARE BUILDING THE PC FOR' ## Add PC to ConferenceRoom ID Logon Allowed Logon Workstations If ($PCUser -eq "ConferenceRoom") { $LOsearcher = New-Object System.DirectoryServices.DirectorySearcher([ADSI]'') $LOsearcher.Filter = "(&(objectClass=User)(samAccountName=$PCUser))" $LOresult = $LOsearcher.FindOne() $LOuser = [ADSI] $LOresult.path $LOuser.psbase.invokeSet("userWorkstations",$CN) $LOuser.setinfo() }
This runs perfectly from both my PC, when I am logged on as myself on the domain and on the newly imaged test PC only when I am logged on to the domain.
When run the PowerShell script above, logged in as User1, it fails at
$LOresult = $LOsearcher.FindOne()
The error is Exception calling "FindOne" with "0" argument(s): "The specified domain either does not exist or it could not be contacted."
I have tried adding credentials to the script, but I continually get the error. So I guess my questions are as follows:
1. Can what I want to do be done?
2. If yes, I assume I need the proper credentials, but I cannot figure out how to code it. How would I add credentials to the code?
I always appreciate the help I get here. Sooner or later, I am going to fully understand all of this.
Matt
Matt Dillon