ccadit Posted June 12, 2014 Posted June 12, 2014 I need to run a script to add a computer to a security group however I need to run it as a domain user, I can achieve it using the two scripts below. When I run script1 which in turns runs script2 as the domain user. Ideally I’d like to run it all through one script: any help would be much appreciated: Script1: Option explicit Dim oShell set oShell= Wscript.CreateObject("WScript.Shell") oShell.Run "RunAs /user:[email protected] ""cscript.exe gl_student_computer_membership_part2.vbs" WScript.Sleep 100 oShell.Sendkeys "domainpassword~" Wscript.Quit Part 2: Option Explicit Dim strComputerDN, objSysInfo Dim objComputer, objGroup ' Retrieve computer Distinguished Name. Set objSysInfo = CreateObject("ADSystemInfo") strComputerDN = objSysInfo.ComputerName ' Bind to the computer object. Set objComputer = GetObject("LDAP://" & strComputerDN) ' Bind to the group object. Set objGroup = GetObject("LDAP://cn=Student Computers,ou=Computers,ou=AB,ou=Groups,dc=domain,dc=local") ' Check if computer already a member of the group. If (objGroup.IsMember(objComputer.AdsPath) = False) Then ' Add the computer to the group. objGroup.Add(objComputer.AdsPath) End If Any help woudl be much appreciate
jinnantonnixx Posted June 12, 2014 Posted June 12, 2014 (edited) You could store your scripts in a scheduled task with the appropriate security credentials, but scheduled to never run (stay with me...) Then, when you need to, use the "schtasks /run /tn TaskName " command to run the script on demand. How to use Schtasks.exe to Schedule Tasks in Windows Server 2003 SCHTASKS /Run [/s system [/u username [/P [password]]]] [/i] /TN taskname Description: Runs a scheduled task on demand. Parameter List: /S system Specifies the remote system to connect to. /U username Specifies the user context under which the schtasks.exe should execute. /P [password] Specifies the password for the given user context. Prompts for input if omitted. /I Runs the task immediately by ignoring any constraint. /TN taskname Identifies the scheduled task to run now. /? Displays this help message. Examples: SCHTASKS /Run /? SCHTASKS /Run /TN "Start Backup" SCHTASKS /Run /S system /U user /P password /I /TN "Backup and Restore" Edited June 12, 2014 by jinnantonnixx
ccadit Posted June 12, 2014 Author Posted June 12, 2014 I'll stay be in the same situation as I will still need to create a schedule task. Ideally if I can have it all done in the one script. Unfortunately my vbscript skills are very poor.
ccadit Posted June 12, 2014 Author Posted June 12, 2014 can I deirectly run a vbscript from the commandline using schtasks /run if so that maybe a way forward.
jinnantonnixx Posted June 12, 2014 Posted June 12, 2014 can I deirectly run a vbscript from the commandline using schtasks /run if so that maybe a way forward. Yes, the scheduled task can be a VBS, but you must call it using the full path of the script interpreter (usually cscript) Explained here. vbscript - can .vbs file be a scheduled script? - Stack Overflow
jinnantonnixx Posted June 12, 2014 Posted June 12, 2014 (edited) Much as I hate Powershell, I think it will be the preferred method for what you want to do. Powershell - Adding computers to a security group in Active Directory - Stack Overflow Of course you will need the correct security to do this, but you can run this yourself with nothing more than a list of the computers and the one line of Powershell. To view the group memberships of computers, Powershell is useful. get-adcomputer -filter 'name -eq "name_of_computer"' -Properties memberof You can use wildcards get-adcomputer -filter 'name -like "science*"' -Properties memberof Edited June 12, 2014 by jinnantonnixx
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now