Posts Tagged ‘code’

Active Directory LDAP / SQL Query Syntax VBScript

Thursday, December 23rd, 2010

Sample VBscript to query Active Directory for certain object properties.

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

objCommand.Properties("Page Size") = 1000
objCommand.Properties("SearchScope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Sort On") = "sn"

'.::LDAP Query Syntax::.
strBase = "<LDAP://dc=contoso,dc=com>" ' Update with your domain
strFilter = "(&(objectCategory=person)(objectClass=user))"
strReturnAttribs = "sn,givenName,mail,sAMAccountName"
strQuery = strBase & ";" & strFilter & ";" & strReturnAttribs & ";subtree"
objCommand.CommandText = strQuery
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
   ' Loop through the record set results (each row could be written to a file or echo'd as below or ...)
    Wscript.Echo objRecordSet.Fields("sn").Value & _
"," & objRecordSet.Fields("givenName").Value & _
"," & objRecordSet.Fields("mail").Value & _
"," & objRecordSet.Fields("sAMAccountName").Value & _
objRecordSet.MoveNext
Loop

To use SQL query syntax to return properties for a user, the query might look like:

'.::SQL Syntax Query::.
sUser = "bob"
objCommand.CommandText = _
    "SELECT givenName, sn, mail FROM 'LDAP://dc=contoso,dc=com' WHERE objectCategory='user' AND sAMAccountName='" & sUser & "'"

Popularity: 13% [?]

FileSystemObject Create A Text File VBScript

Thursday, December 23rd, 2010

Creates a new text file and writes a line to it. That is all.

TargetFile = "myfile.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(TargetFile, True)      'The 'True' parameter means overwrite existing file if it exists
File.WriteLine "my line inside the file"
objFile.Close
Set objFSO = Nothing

Popularity: 3% [?]

Active Directory Bind VBScript

Thursday, December 23rd, 2010
'Bind to Active Directory
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("DefaultNamingContext")

Popularity: 4% [?]

Online OCS 2007 Custom Presence Tool

Friday, September 3rd, 2010

Inspired by the downloadable tool from here [www.confusedamused.com], I put together an online tool. It will basically generate the XML and .reg file necessary to create custom presence for OCS 2007 Communicator clients. You can then copy and paste the content into the files you will actually use. I would recommend the Custom State URL value in most environments would probably be better set using Group Policy anyway, but the .reg file is handy to understand how it all works.

Online OCS Custom Presence Tool

Popularity: 18% [?]

Exchange 2007 Mailbox Quota Notification

Thursday, August 12th, 2010

Powershell script to check mailboxes that are at or close to quota and send the mailbox an email to advise:

$smtpserver = ""
$replyemailadd = ""
$mailboxserver = ""
$users = Get-MailBoxStatistics -Server $mailboxserver | where {"IssueWarning","ProhibitSend","MailboxDisabled" -contains $_.StorageLimitStatus} 

foreach ($objItem in $users)
{
$mailbox1 = $objItem.displayName + " item count: " + $objItem.itemcount + ", using " + $objItem.totalitemsize.value.ToMB() + "MB`n"
$mailbox2 = get-mailboxfolderstatistics $objItem.displayName | ft FolderPath, ItemsInFolder, @{Label="FolderSize(MB)";expression={$_.FolderSize.ToMB()} } -auto | Out-String
$mailbox3 = "Please contact ... if you have any questions regarding your mailbox quota.`n"
$messagesubject = "Quota Warning for " + $objItem.displayName + ", Size " + $objItem.totalitemsize.value.ToMB() + "MB used"
$mailboxfinal = $mailbox1 + $mailbox2 + $mailbox3

$temp = get-mailbox -identity $objItem.DisplayName

$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($replyemailadd, $temp.PrimarySmtpAddress,$messagesubject,$mailboxfinal)
}

Popularity: 6% [?]