0 votes

Having a strange issue, when executing a powershell command in adaxes as a business rule, the first line, no matter what is there comes up as invalid. Originally my script started with a commented line, but I've tried everything and universally the first line is flagged as invalid

image.png image.png

by (290 points)
0

I don't believe you need to import the module from Adaxes each time you run the script or even just once. I'd recommend importing the modul on the server itself if you haven't already. You really just need to invoke the connection itself via the token when running the script via the Business Rule.

Let me know how that goes.

0

The import-module was just an example, as noted in my post anything on the first line is returning invalid. Even commenting out the first line results in an error.

0

Can you please post the script here for review?

0

Import-Module Adaxes
# Define the base path for the CSV file
$baseCsvPath = "C:\HCM-IMPORT\Outbound-to-HCM\"

# Function to get the timestamped filename
function Get-TimestampedFilename {
    $timestamp = Get-Date -Format "yyyyMMddHHmm"
    return "${baseCsvPath}OutboundUpdates_$timestamp.csv"
}

# Function to get the latest CSV file in the base directory
function Get-LatestCsvFile {
    $csvFiles = Get-ChildItem -Path $baseCsvPath -Filter "OutboundUpdates_*.csv" | Sort-Object LastWriteTime -Descending
    if ($csvFiles.Count -gt 0) {
        return $csvFiles[0].FullName
    } else {
        return $null
    }
}

# Function to log the modified attributes to a pipe-delimited file
function Log-ModifiedAttributes {
    param (
        [string]$employeeNumber,
        [string]$username,
        [string]$usernameAD,
        [string]$email,
        [string]$emailAD,
        [string]$phone,
        [string]$phoneAD,
        [string]$mobile,
        [string]$mobileAD
    )

    # Check if the file exists, if not create it with a timestamped filename
    $csvPath = Get-LatestCsvFile
    if (-not $csvPath) {
        $csvPath = Get-TimestampedFilename
        # Add header to the new file
        "EmployeeNumber|Username|UsernameAD|Email|EmailAD|Phone|PhoneAD|Mobile|MobileAD" | Out-File -FilePath $csvPath
    }

    # Create a custom object with the modified attributes
    $modifiedUser = [PSCustomObject]@{
        EmployeeNumber = $employeeNumber
        Username       = $username
        UsernameAD     = $usernameAD
        Email          = $email
        EmailAD        = $emailAD
        Phone          = $phone
        PhoneAD        = $phoneAD
        Mobile         = $mobile
        MobileAD       = $mobileAD
    }

    # Convert the custom object to a pipe-delimited string
    $pipeDelimitedString = $modifiedUser.PSObject.Properties.Value -join '|'

    # Append the pipe-delimited string to the file
    Add-Content -Path $csvPath -Value $pipeDelimitedString
}

# Get the modified user object
$user = $Context.TargetObject

# Retrieve the employeeID directly from the user object
$employeeNumber = $user.Get("employeeID")

# Check if the properties are modified
$username = if ($Context.IsPropertyModified("userPrincipalName")) { $Context.GetModifiedPropertyValue("userPrincipalName") } else { $null }
$email = if ($Context.IsPropertyModified("mail")) { $Context.GetModifiedPropertyValue("mail") } else { $null }
$phone = if ($Context.IsPropertyModified("telephoneNumber")) { $Context.GetModifiedPropertyValue("telephoneNumber") } else { $null }
$mobile = if ($Context.IsPropertyModified("mobile")) { $Context.GetModifiedPropertyValue("mobile") } else { $null }

# Set the AD fields to 'A' for Add or Update if the properties are modified
$usernameAD = if ($username) { "A" } else { $null }
$emailAD = if ($email) { "A" } else { $null }
$phoneAD = if ($phone) { "A" } else { $null }
$mobileAD = if ($mobile) { "A" } else { $null }

# Log the modified attributes to the pipe-delimited file only if at least one property is modified
if ($usernameAD -or $emailAD -or $phoneAD -or $mobileAD) {
    Log-ModifiedAttributes -employeeNumber $employeeNumber -username $username -usernameAD $usernameAD -email $email -emailAD $emailAD -phone $phone -phoneAD $phoneAD -mobile $mobile -mobileAD $mobileAD
}
0

The script executed fine for me, minus the obvious errors thrown missing specific aspects to your environment. However, I am testing it in the editor.

Just for clarity, when you test the script in the editor does it throw the same error?

What version of Adaxes are you running?

0

It works fine in the script editor, the error only thows on the portal. We are on 2023.2

0

What are the triggers and conditions of the business rule?

0

On update of a user.

0

Hello,

For troubleshooting purposes, please, specify how exactly you add scripts to the Run a program or PowerShell script action?

Also, please, post here or send us (support@adaxes.com) a screenshot of the Multi-server environment dialog. The dialog displays how many Adaxes services you have and what their versions are. For information on how to view it, see https://www.adaxes.com/help/MultiServerEnvironment.

0

Here is how the script is executed: image.png

Here is the MultiServer, we only utilize 1 server: image.png

0

Hello,

Here is how the script is executed

Sorry for the confusion, but the question is not about how the script is executed. We need to know, how you add it to the action. For example, you can directly write the script in the editor. Alternatively, the script can be added from a file that you open through the editor by browsing your file system.

Here is the MultiServer, we only utilize 1 server

Thank you for the provided screenshot.

0

I've copy/pasted it and opened it via a file saved in Powershell ISE, both ways had the same outcome.

1 Answer

0 votes
by (292k points)

Hello,

Thank you for clarifying. To resolve the issue, please, do the following:

  1. Copy the script from this thread (you provided it earlier) to a text editor, e.g. Notepad.
  2. Remove the first two lines (one is empty and the other imports the Adaxes module).
  3. Launch Adaxes Administration console.
  4. Navigate to the business rule in question and right-click Run PowerShell script ‘Outbound-Updates-to-Oracle’ for the user.
  5. In the context menu, click Delete Action. image.png
  6. In the dialog that opens, click Yes.
  7. Right-click Do nothing and then click Add Action.
  8. Select Run a program or PowerShell script.
  9. Specify a script description and click Edit. image.png
  10. Copy/paste the script from the text editor (make sure not to save it in any way).
  11. Click OK twice and save the changes.

The execution should work fine in this case. The best approach is to directly write scripts in Adaxes script editor to avoid any issues with formatting added by other applications.

Related questions

0 votes
1 answer

Hi, I have a lot of custom PowerShell scripts running and I am wondering, if its possible somehow to get the name of the PS Script the line of the execution of a code ... occurs and send directly the action, maybe path in Adaxes and line number of the script.

asked Oct 7 by wintec01 (1.5k points)
0 votes
1 answer

Trying to set the primary proxy when doing a name change on an email address.

asked Jul 13, 2023 by mightycabal (1.0k points)
0 votes
1 answer

I thought I would share this. We are new to Adaxes but really finding it a great tool to host some powershell scripts and make them accessible to others in the team without ... --------- If there are better ways to do this I am definitely interested.

asked Nov 29, 2018 by jbadry (430 points)
0 votes
1 answer

Hi, I have this Script to check if a condition is met # The condition is met if $Context.ConditionIsMet is set to $True. $Context.ConditionIsMet = $False $inputString = " ... or issue with this match function using in a "if script returns true" condition?

asked Oct 31 by wintec01 (1.5k points)
0 votes
1 answer

For creating a computer object, we want to check if the entered CN is already used in our AD. And for that we want to use a powershell script. An other dot ... powershell script should be start before creating the computer object, right? Thanks for your help.

asked Jun 4 by KEME (80 points)
3,569 questions
3,259 answers
8,274 comments
547,937 users