PowerShell and ActiveMQ

PowerShell logoWe have a project going on at work that requires us to take gather specific information about users from Active Directory and get it into another system. We already use PowerShell to create all of our new user accounts so we wanted to stick with that. However, the other system we have to get the information into requires us to write the AD information to an ActiveMQ queue. The preferred format for this information is XML. So I was asked to come up with a way to accomplish this.

First, we only needed a few attributes from the AD accounts (name, email, phone number, and group membership). Second, I needed to take this information and put it into an XML file. I did this by just writing the AD information to an XML file. Finally, I needed to read that XML file and submit the information to the ActiveMQ queue. I’m sure I could have taken the output from AD and posted it directly to the ActiveMQ queue, but I wanted to write it to a file in case I wasn’t able to post it to the queue right away. It also will create a small file that will allow us to keep track of when users were created and tell us who created the account. I’m sure there are other ways to accomplish this, but this is the way I came up with.

We are using PowerShell 3.0 and ActiveMQ 5.5.1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Creats a variable that will hold all of the properties of the Domain Admin creating the account.
$createdBy = Get-ADUser -Identity $env:USERNAME -Properties *
$DateTimeCreated = Get-Date
 
# Creates a variable that will hold all of the property values for the user.
$getADUser = get-aduser -Identity $UserName -Properties *
 
# Creates an empty array that will hold the groups a user is a member of and then populates that array with just the Group Name.
$groupName = @()
$getGroupMembership = $getADUser.memberof
foreach ($eachGroup in $getGroupMembership) {
                $groupShortName = $eachGroup.split(",")[0].split('=')[1]
                $groupName += "" + $groupShortName + ""
            }
 
# Puts the needed user information (First Name, Last Name, Display Name, Email, Office Phone, Group Membership) into an XML Format.
# It also puts in the information about who created the account.
$xml = ""
foreach($userXML in $getADUser){
    $xml += ""
        $xml += ""
            $xml += "" + $getADUser.ObjectGUID + ""
            $xml += "" + $getADUser.givenName + ""
            $xml += "" + $getADUser.Surname + ""
            $xml += "" + $getADUser.givenName + " " + $getADUser.Surname + ""
            $xml += "" + $getADUser.Mail + ""
            $xml += "" + $getADUser.OfficePhone + ""
            $xml += ""
                $xml += $groupName
            $xml += ""
        $xml += ""
    $xml += ""
    $xml += ""
        $xml += "<CreatedBy_Name>" + $createdBy.DisplayName + ""
        $xml += "<CreatedBy_GUID>" + $createdBy.ObjectGUID + ""
        #$xml += "" + $dateCreated + ""
        $xml += "" + $DateTimeCreated + ""
        #$xml += "" + $timeCreated + ""
    $xml += ""
    }
 
$xml += ""
 
# Writes the XML information from above to an actual XML File on the server.
$xml | out-file -FilePath "C:NEW_AD_USERS$UserName.xml"
 
###############################################################
##                                                           ##
##  The following code. Posts the information from the XML   ##
##  file to the ActiveMQ queue ad_inscribe_inq.              ##
##                                                           ##
###############################################################
 
# Creates a variable for the URI.
$uri = "http://ACTIVEMQ-SERVER:8161/admin/sendMessage.action"
 
# Reads the XML File created previously.
[xml]$xmlFile = Get-Content "C:New_AD_USERS$UserName.xml"
 
# Creates a web request to ActiveMQ and the posts the data from the XML file to the New_AD_User queue.
$r = Invoke-WebRequest http://ACTIVEMQ-SERVER:8161/admin/send.jsp -SessionVariable amq
 
$form = $r.Forms[0]
$secret = $form.fields.secret
 
$form.fields["secret"] = $secret
$form.fields["JMSDestination"] = "ad_inscribe_inq"
$form.fields["JMSPersistent"] = "true"
$form.fields["JMSText"] = $xmlFile.OuterXml
 
$r = Invoke-WebRequest -Uri $uri -WebSession $amq -Method Post -Body $form.Fields
$r.StatusDescription

PDF pageEmail pagePrint page

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.