Alan's Blog

"Yeah. I wrote a script that will do that."

Menu
  • About My Blog
Menu

Batch File to Prestage a Computer Account

Posted on July 27, 2013March 9, 2015 by Alan

Batch files are not dead.  Microsoft has said that batch files, and vbscript will be supported into the indefinite future.  And I still write an occasional batch file because the really are quick and easy.  I use the .CMD extension, instead of .BAT, but it does not really matter in execution.

Recently I was asked to write something that would allow for prestaging a computer account in Active Directory.   The code works but is painfully slow.  Still, you may find it useful in your environment.  You could use it with a FOR command to run it against a list of machines overnight:

@echo off
:: Alan Kaplan, loosely based on 
::http://social.technet.microsoft.com/Forums/en-PH/winserverDS/thread/f0c54de7-3f19-4502-958d-00b4a24a1b03
cls
setlocal
:: ************* mandatory edits here ******************
rem omit LDAP://
set NewPCOU=OU=Windows 7,OU=Test Lab,DC=contoso,DC=com
rem this is the group to grant permission to join
set UserOrGroup=alan.kaplan@contoso.com
:: ********** end edits ******************************

if %1z == z echo PC Accounts will be added to "%NewPCOU%"
if %1z == z echo Granting permissions to %UserOrGroup%
if %1z == z set /p PCName=Add what computer account? &goto AddPC
set PCName=%1

:AddPC
set ComputerDN=cn=%PCName%,%NewPCOU%
dsadd computer "%ComputerDN%"
Echo Granting Permissions to %UserOrGroup%

REM full control of object.  This is really slow ....
dsacls "%ComputerDN%" /G %UserOrGroup%:GA

REM specific join computer rights
REM This is 6 times slower than full control
REM dsacls "%ComputerDN%" /G %UserOrGroup%:CALCGRSDDTRC;;
REM dsacls "%ComputerDN%" /G %UserOrGroup%:WP;description;
REM dsacls "%ComputerDN%" /G %UserOrGroup%:WP;sAMAccountName;
REM dsacls "%ComputerDN%" /G %UserOrGroup%:WP;displayName;
REM dsacls "%ComputerDN%" /G %UserOrGroup%:WP;userAccountControl;
REM dsacls "%ComputerDN%" /G %UserOrGroup%:WS;"Validated write to service principalname";
REM dsacls "%ComputerDN%" /G %UserOrGroup%:WS;"Validated write to DNS host name";

cls 
echo %PCName% added to %NewPCOU%
echo Granted permissions to %UserOrGroup% 
echo %NewPCOU%
:end
endlocal


									

You will need to edit the script to make it work.  I have set it to Full Permissions, but as  you can see, you can switch to more granular permissions.

Leave a Reply

You must be logged in to post a comment.

Search

Please Note

All the scripts are saved as .txt files. Newer files have a “View Script” button which will let you save or open a script in notepad. For earlier posts, the easiest way to download with IE is to right click on the link and use “Save Target As”. Rename file from Name_ext.txt to Name.ext.

To see a full post after searching, please click on the title.

PowerShell Scripts were written with version 3 or later.

https connections are supported.

All new users accounts must be approved, as are comments. Please be patient.  If you find a post error or a script which doesn’t work as expected, I appreciate being notified.  My email is my first name at the domain name, and you are welcome to contact me that way.

Tags

1E ACLS Active Directory ActiveDirectory ADSI Advanced Functions Audit Change Administrator Password COMObject Computer Groups DateTime Desktop DNS Excel FileScriptingObject Forms General GPO GPS Group Policy Hacks ISE Lockout logons NAV740 Nessus OU OU permissions Outlook Pick Folder Power PowerShell Powershell Scriptlets RDP SCCM schedule reboot Scripting Security Shell.Application user information VBA Windows Update WMI WordPress WPF

Categories

akaplan.com

  • Back to Home Page

Archives

Scripting Sites

  • A Big Pile of Small Things
  • Adam, the Automator
  • Art of the DBA
  • Ashley McGlone
  • Boe Prox
  • Carlo Mancini
  • DexterPOSH
  • Doug Finke
  • Jaap Brasser's Blog
  • JeffOps The Scripting Dutchman
  • Jonathan Medd's Blog
  • Keith Hill's Blog
  • LazyWinAdmin
  • Nana Lakshmanan
  • PowerShell Magazine
  • PowerShell Team Blog
  • PowerShell.org
  • PwrShell.net
  • Richard Siddaway's Blog
  • Ryan Yates' Blog
  • Skatterbrainz
  • The Lonely Administrator

SQL Site

  • Art of the DBA

Meta

  • Register
  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
©2025 Alan's Blog | Theme by SuperbThemes

Terms and Conditions - Privacy Policy