Tuesday, January 20, 2015

Peoplesoft Roles and Active Directory group membership in harmony?

I recently had a request to synchronize a PeopleSoft role with an Active Directory group.  I am not a programmer normally, so there might be an easier way to do this, but since I couldn't find anyone else on the internet doing it, here is the script I wrote to accomplish it.  You will have to install the Oracle drivers, which was a pain and beyond the scope of this blog post.  Also, our Oracle DBAs created a view for us containing only usernames in which I just dump the data from.


###############################################

###############################################
#
#
#   Sync oracle (peoplesoft role(s)) results to an active directory group after purging group of current users
#
#   Bryan Loveless bryan.loveless@gmail.com
#   Jan 2015
#
#
#   Prerequisites:  Download and install the Oracle Data Access Components prior to accessing a database. 
#                    Download the components here: bit.ly/1t2W790 or http://www.oracle.com/technetwork/topics/dotnet/downloads/index.html
#                    Select the appropriate architecture (x86/x86-64) and ensure the correct PowerShell program architecture is being executed with 
#                    the corresponding Oracle component’s architecture. 
#                    Failure to do so will lead to binary related errors while loading the assembly in PowerShell.
#
#
#
###############################################

#name of the active directory group you want to modify
$activeDirectoryGroup = 'group_NAME_HERE'

# username, password, server name for oracle DB:
$oracleusername = 'USERNAME'

#for dev purposes, ask for password.  remove this bit of code when converted to nightly job

#prompt user with protected pin entering box:
$oraclepasswordencrypted = read-host -prompt "What is the password?" -AsSecureString

#extract plain text pin number from encrypted varible above
$BSTR = `
    [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($oraclepasswordencrypted)
$oraclepassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)   

# end of "prompt password" part

# $oraclepassword = 'password'

$oracleservername = 'SERVERNAME_AS_FQDN'

$servicename = 'SERVICENAME_AS_FQDN'

# default port 1521

#Load Oracle client
### try to load assembly, fail otherwise ###
$Assembly = [System.Reflection.Assembly]::LoadWithPartialName("System.Data.OracleClient")
if ( $Assembly ) {
    write-output "System.Data.OracleClient Loaded!"
 }
else {
     write-output "System.Data.OracleClient could not be loaded! Exiting..."
     Exit 1
 }

#Setup Connection string and open DB connection
#borrowed from http://lvlnrd.com/oracle-database-queries-powershell-script-examples/
### connection string ###
$OracleConnectionString = "SERVER=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=$oracleservername)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=$servicename)));uid=$oracleusername;pwd=$oraclepassword;"
 
### open up oracle connection to database ###
$OracleConnection = New-Object System.Data.OracleClient.OracleConnection($OracleConnectionString);
$OracleConnection.Open()

#SQL select:

 try {
  
     ### sql query command ###
     # $OracleSQLQuery = "SELECT * FROM HOSTS"
        $OracleSQLQuery = "select distinct(roleuser) from ps_roleuser_vw1 where rolename in('ROLENAME1','ROLENAME2','ROLENAME3')"
  
     ### create object ###
     $SelectCommand = New-Object System.Data.OracleClient.OracleCommand;
     
     $SelectCommand.Connection = $OracleConnection
     $SelectCommand.CommandText = $OracleSQLQuery
     $SelectCommand.CommandType = [System.Data.CommandType]::Text
  
     ### create datatable and load results into datatable ###
     $SelectDataTable = New-Object System.Data.DataTable
     $SelectDataTable.Load($SelectCommand.ExecuteReader())

          
 }
 catch {
     echo $_.Exception.GetType().FullName, $_.Exception.Message
     write-output "Error while retrieving data!"
        
  
 }
    finally{
        $OracleConnection.close()
}

#output of SQL query above: $SelectDataTable

# list everyone in the group currenty, then remove everyone in the group
write-output "Removing current members from group now, this may take a minute or two"
Get-ADGroupMember "$activedirectorygroup" | ForEach-Object {Remove-ADGroupMember "$activedirectorygroup" $_ -Confirm:$false}
write-output "Done removing current members from group"

$memberoutput = ($selectdatatable | select-object)

# add-adgroupmember

foreach ($member in $memberoutput.roleuser) {
    try{
    Add-ADGroupMember $activeDirectoryGroup -member $member
    write-output "Added $member to group $activeDirectoryGroup"
    }
    Catch {
    }
}





2 comments:

  1. Ich möchte DR. AKHERE für die wundervolle Arbeit danken, die er für mich und meine Familie geleistet hat. Ich hatte eine ernsthafte Trennung von meinem Ex, aber als ich ihn um Hilfe bat, brachte er ihn mit seinen historischen Kräften zu mir zurück und half mir auch dabei einen Job zu bekommen, da er mich verzaubert hat, hat es mir wirklich gut getan und seit ich ihn kenne, ist mein Mann mir treu geblieben Hilfe, wenn Sie mit einer Trennung oder einem Eheproblem konfrontiert sind, wenden Sie sich einfach an diesen Mann, um Hilfe zu erhalten. Er wird Ihnen helfen, alles mit seiner Macht zu regeln. Bitte kontaktieren Sie ihn über seine E-Mail: AKHERETEMPLE@gmail.com oder rufen Sie / whatsapp: +2349057261346 an Ihre Probleme werden gelöst.


































































    Ich möchte DR. AKHERE für die wundervolle Arbeit danken, die er für mich und meine Familie geleistet hat. Ich hatte eine ernsthafte Trennung von meinem Ex, aber als ich ihn um Hilfe bat, brachte er ihn mit seinen historischen Kräften zu mir zurück und half mir auch dabei einen Job zu bekommen, da er mich verzaubert hat, hat es mir wirklich gut getan und seit ich ihn kenne, ist mein Mann mir treu geblieben Hilfe, wenn Sie mit einer Trennung oder einem Eheproblem konfrontiert sind, wenden Sie sich einfach an diesen Mann, um Hilfe zu erhalten. Er wird Ihnen helfen, alles mit seiner Macht zu regeln. Bitte kontaktieren Sie ihn über seine E-Mail: AKHERETEMPLE@gmail.com oder rufen Sie / whatsapp: +2349057261346 an Ihre Probleme werden gelöst.

    ReplyDelete