S1LV3RF1$# Posted February 14, 2006 Share Posted February 14, 2006 I am trying to see if there is a way to remove a computer from an OU with a VB Script so that you can just launch it on the system. It will pull the WMI info on the box IE computer name and then wack it from the OU that it Resides in. I have found a way to do this but you have to hard code the OU into the Script is there a way to make it pull what OU it resides in on its own.Thanks Silv3rf1$# Link to comment Share on other sites More sharing options...
gunsmokingman Posted February 15, 2006 Share Posted February 15, 2006 I am trying to see if there is a way to remove a computer from an OU with a VB Script so that you can just launch it on the system. It will pull the WMI info on the box IE computer name and then wack it from the OU that it Resides in. I have found a way to do this but you have to hard code the OU into the Script is there a way to make it pull what OU it resides in on its own.Thanks Silv3rf1$#Try This Const HKEY_LOCAL_MACHINE = &H80000002 Dim Act : Set Act = CreateObject("Wscript.Shell") strComputer = Act.ExpandEnvironmentStrings("%ComputerName%") Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv") strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion" ValueName = "ProgramFilesDir" objReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, ValueName, strValue Dim ProF_OU : ProF_OU = strValue & "\Outlook Express" MsgBox strValue & vbCrLf & ProF_OU Link to comment Share on other sites More sharing options...
RogueSpear Posted February 16, 2006 Share Posted February 16, 2006 @gunsmokingman, I'm not trying to be a smartass here, but how is that script going to remove a computer from an organizational unit?@S1LV3RF1$#, what the script needs to do is tap into ADSI. And something you need to consider is that unless you want to delete the computer account from Active Directory, it will need to reside someplace. In other words, if you remove it from an OU do you then want it placed in a different OU? or would you want it placed in the Computers container?This isn't something that I've specifically scripted before, but I'll see what I can put together. Link to comment Share on other sites More sharing options...
gunsmokingman Posted February 16, 2006 Share Posted February 16, 2006 I thought OU ment Outlook Express, but the script is ment to run against any computer on the network.Dim ProF_OU : ProF_OU = strValue & "\Outlook Express"Here a script the will Enumerate all object in a OUChange the red text to suit your needs Set colItems = GetObject ("LDAP://ou=Servers, dc=fabrikam, dc=com ") For Each objItem in colItems Wscript.Echo objItem.CN NextOr use this link The Hey, Scripting Guy! Archive: Active Directory and find a script that matches your need Link to comment Share on other sites More sharing options...
RogueSpear Posted February 16, 2006 Share Posted February 16, 2006 for a minute there I thought you came up with something I had never heard of. Link to comment Share on other sites More sharing options...
gunsmokingman Posted February 16, 2006 Share Posted February 16, 2006 I am still shaking my head over this one, I guess the 20 cups of coffee and no sleep is not the way to reply to posts. Link to comment Share on other sites More sharing options...
John McGuire Posted February 21, 2006 Share Posted February 21, 2006 To find a computer in the directory (change strDomainSuffix to match your domain), once you know where it resides you can use your existing code to remove the comp from the OU, I've just pulled this from a simular script I wrote to add a comp to a group 'CodestrDomainSuffix = "DC=domain,DC=co,DC=uk"Set WshNetwork = WScript.CreateObject("WScript.Network")Const ADS_SCOPE_SUBTREE = 2Set objConnection = CreateObject("ADODB.Connection")Set objCommand = CreateObject("ADODB.Command")objConnection.Provider = "ADsDSOObject"objConnection.Open "Active Directory Provider"Set objCommand.ActiveConnection = objConnection' Taylor this search to return the computers you want.objCommand.CommandText = "" _& "select distinguishedName " _& "from 'LDAP://" & strDomainSuffix & "' " _& "where objectClass='computer' " _& "and name='" & WshNetwork.ComputerName & "' " _& ""objCommand.Properties("SearchScope") = ADS_SCOPE_SUBTREE objCommand.Properties("Cache Results") = False objCommand.Properties("Timeout") = 300Set objRecordSet = objCommand.ExecuteobjRecordSet.MoveFirstDo Until objRecordSet.EOF 'Wscript.Echo objRecordSet.Fields("distinguishedName").Value strCompOU = objRecordSet.Fields("distinguishedName").Value objRecordSet.MoveNextLoop'This should output the path of the computer within the directorywscript.echo strCompOU'End Code Link to comment Share on other sites More sharing options...
S1LV3RF1$# Posted March 20, 2006 Author Share Posted March 20, 2006 thanks for all the info it will come in handy i just need to find time to intergrate it into my script now. Link to comment Share on other sites More sharing options...
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