I am not a programmer and I need help. All scripts are written in vbs. If this is the wrong forum I apologize.
this is the script to retrieve the OU a pc resides in, in AD. Should work on any PC:
OPTION EXPLICIT
DIM objNetwork
DIM computerName
DIM ou
' Get the computerName of PC
SET objNetwork = CREATEOBJECT("Wscript.Network")
computerName = objNetwork.ComputerName
' Call function to find OU from computer name
ou = getOUByComputerName(computerName)
wscript.echo ou
FUNCTION getOUByComputerName(BYVAL computerName)
' *** Function to find ou/container of computer object from computer name ***
DIM namingContext, ldapFilter, ou
DIM cn, cmd, rs
DIM objRootDSE
' Bind to the RootDSE to get the default naming context for
' the domain. e.g. dc=wisesoft,dc=co,dc=uk
SET objRootDSE = GETOBJECT("LDAP://RootDSE")
namingContext = objRootDSE.GET("defaultNamingContext")
SET objRootDSE = NOTHING
' Construct an ldap filter to search for a computer object
' anywhere in the domain with a name of the value specified.
ldapFilter = "<LDAP://" & namingContext & _
">;(&(objectCategory=Computer)(name=" & computerName & "))" & _
";distinguishedName;subtree"
' Standard ADO code to query database
SET cn = CREATEOBJECT("ADODB.Connection")
SET cmd = CREATEOBJECT("ADODB.Command")
cn.open "Provider=ADsDSOObject;"
cmd.activeconnection = cn
cmd.commandtext = ldapFilter
SET rs = cmd.EXECUTE
IF rs.eof <> TRUE AND rs.bof <> TRUE THEN
ou = rs(0)
' Convert distinguished name into OU.
' e.g. cn=CLIENT01,OU=WiseSoft_Computers,dc=wisesoft,dc=co,dc=uk
' to: OU=WiseSoft_Computers,dc=wisesoft,dc=co,dc=uk
ou = MID(ou,INSTR(ou,",")+1,LEN(ou)-INSTR(ou,","))
getOUByComputerName = ou
END IF
rs.close
cn.close
END FUNCTION
As you can see there is nothing in that script that is specific to the domain that it is in so it should work on any device that is within a domain. The idea I have is to use the output or variable that the above script creates in the script that I already have by using the variable in the ldap url, just like i use the variable of the computer name in the ldap url. Here is the script as it stands:
Set wshShell = WScript.CreateObject( "WScript.Shell" )
strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
On Error Resume Next
Set objComputer = GetObject _
("LDAP://CN=" & strComputerName & ",OU=This department,OU=Computers,DC=domain,DC=com")
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.CreateTextFile("c:\Windows\Temp\descr.txt", True)
objProperty = objComputer.Get("Description")
If IsNull(objProperty) Then
Wscript.Echo "The description has not been set."
Else
file.WriteLine "" & objProperty
objProperty = Null
End If
sub shell(cmd)
dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run(cmd)
Set objShell = Nothing
end sub
shell "\\serverlocation\bginfo /i\\serverlocation\bginfo.bgi /timer:0 /accepteula"
The only thing in the above script that does not work properly is the "the description has not been set" thing. Otherwise if that particular object, the computer name, is in that particular OU then this script will work and will write the description of the device to that text file, which bginfo can read and then display. So I would like the script to read the 'ou' variable from the first script where "OU=Computers,OU=KGH COMPUTERS,DC=bayhealth,DC=org" is. Replacing that value with " & ou & " is what I am trying to do but I cannot figure out how to integrate the two scripts together. I am not a programmer at nature so this feels exactly like trying to speak another language via google.
this is the script to retrieve the OU a pc resides in, in AD. Should work on any PC:
OPTION EXPLICIT
DIM objNetwork
DIM computerName
DIM ou
' Get the computerName of PC
SET objNetwork = CREATEOBJECT("Wscript.Network")
computerName = objNetwork.ComputerName
' Call function to find OU from computer name
ou = getOUByComputerName(computerName)
wscript.echo ou
FUNCTION getOUByComputerName(BYVAL computerName)
' *** Function to find ou/container of computer object from computer name ***
DIM namingContext, ldapFilter, ou
DIM cn, cmd, rs
DIM objRootDSE
' Bind to the RootDSE to get the default naming context for
' the domain. e.g. dc=wisesoft,dc=co,dc=uk
SET objRootDSE = GETOBJECT("LDAP://RootDSE")
namingContext = objRootDSE.GET("defaultNamingContext")
SET objRootDSE = NOTHING
' Construct an ldap filter to search for a computer object
' anywhere in the domain with a name of the value specified.
ldapFilter = "<LDAP://" & namingContext & _
">;(&(objectCategory=Computer)(name=" & computerName & "))" & _
";distinguishedName;subtree"
' Standard ADO code to query database
SET cn = CREATEOBJECT("ADODB.Connection")
SET cmd = CREATEOBJECT("ADODB.Command")
cn.open "Provider=ADsDSOObject;"
cmd.activeconnection = cn
cmd.commandtext = ldapFilter
SET rs = cmd.EXECUTE
IF rs.eof <> TRUE AND rs.bof <> TRUE THEN
ou = rs(0)
' Convert distinguished name into OU.
' e.g. cn=CLIENT01,OU=WiseSoft_Computers,dc=wisesoft,dc=co,dc=uk
' to: OU=WiseSoft_Computers,dc=wisesoft,dc=co,dc=uk
ou = MID(ou,INSTR(ou,",")+1,LEN(ou)-INSTR(ou,","))
getOUByComputerName = ou
END IF
rs.close
cn.close
END FUNCTION
As you can see there is nothing in that script that is specific to the domain that it is in so it should work on any device that is within a domain. The idea I have is to use the output or variable that the above script creates in the script that I already have by using the variable in the ldap url, just like i use the variable of the computer name in the ldap url. Here is the script as it stands:
Set wshShell = WScript.CreateObject( "WScript.Shell" )
strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
On Error Resume Next
Set objComputer = GetObject _
("LDAP://CN=" & strComputerName & ",OU=This department,OU=Computers,DC=domain,DC=com")
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.CreateTextFile("c:\Windows\Temp\descr.txt", True)
objProperty = objComputer.Get("Description")
If IsNull(objProperty) Then
Wscript.Echo "The description has not been set."
Else
file.WriteLine "" & objProperty
objProperty = Null
End If
sub shell(cmd)
dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run(cmd)
Set objShell = Nothing
end sub
shell "\\serverlocation\bginfo /i\\serverlocation\bginfo.bgi /timer:0 /accepteula"
The only thing in the above script that does not work properly is the "the description has not been set" thing. Otherwise if that particular object, the computer name, is in that particular OU then this script will work and will write the description of the device to that text file, which bginfo can read and then display. So I would like the script to read the 'ou' variable from the first script where "OU=Computers,OU=KGH COMPUTERS,DC=bayhealth,DC=org" is. Replacing that value with " & ou & " is what I am trying to do but I cannot figure out how to integrate the two scripts together. I am not a programmer at nature so this feels exactly like trying to speak another language via google.