Showing posts with label peoplesoft. Show all posts
Showing posts with label peoplesoft. Show all posts

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 {
    }
}