Jump to content

[VB] Remove Computer from OU


Recommended Posts

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


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

@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

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 OU

Change 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

Next

Or 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

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

'Code

strDomainSuffix = "DC=domain,DC=co,DC=uk"

Set WshNetwork = WScript.CreateObject("WScript.Network")

Const ADS_SCOPE_SUBTREE = 2

Set 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") = 300

Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst

Do Until objRecordSet.EOF

'Wscript.Echo objRecordSet.Fields("distinguishedName").Value

strCompOU = objRecordSet.Fields("distinguishedName").Value

objRecordSet.MoveNext

Loop

'This should output the path of the computer within the directory

wscript.echo strCompOU

'End Code

Link to comment
Share on other sites

  • 4 weeks later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...