|
<< Click to Display Table of Contents >> Navigation: Advanced Features > DBWARM > Appendix > DBWARM OnDBWArmUsersTableUpdated |
This script (.vbs or .ps1) is fired whenever a new user is created/removed/updated in the Users table of the DBWArm Editor.
Please note this script is running as windows script, so outside the MechworksPDM environment; This means it cannot use any Lib or Shell function from MechworksPDM. More, as for every windows script, it doesn't need to have the Sub Main specific module.
LST\DBWARM folder
z:\MechWorks_Pdm_Server\LST\DBWARM\OnDBWArmUsersTableUpdated.VBS
DBWorksApplicationName |
"DBWorks" | "DBWorks Standalone" | "DBInventor" | "DBSolidEdge" |
|---|---|
DBWARM_Action |
"USER_ADDED" | "USER_REMOVED" | "USER_MODIFIED" |
DBWARM_UserName |
|
DBWARM_UserFullName |
|
DBWARM_UserGroup |
|
DBWARM_UserNameWas |
Not null if DBWARM_Action is USER_MODIFIED |
DBWMsgBox "OnDBWArmUsersTableUpdated.VBS" & vbcrlf &_
"Application : " & DBWorksApplicationName & vbcrlf &_
" Action : " & DBWARM_UsersTableAction & vbcrlf &_
" User name : " & DBWARM_UserName & vbcrlf &_
" Full name : " & DBWARM_UserFullName & vbcrlf &_
" Group : " & DBWARM_UserGroup &_
" User name was : "& DBWARM_UserNameWas
# OnDBWArmUsersTableUpdated.ps1
# PowerShell script example for DBWArm user table events
# This script receives the same parameters as the VBS version but as PowerShell parameters
param(
[string]$DBWorksApplicationName,
[string]$DBWARM_UsersTableAction,
[string]$DBWARM_UserName,
[string]$DBWARM_UserFullName,
[string]$DBWARM_UserGroup,
[string]$DBWARM_UserNameWas
)
# Error handling: use try-catch and write errors to stderr
try {
# Example: Log the event to a file
$logPath = "$env:TEMP\DBWArm_UserEvents.log"
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logEntry = @"
[$timestamp] $DBWorksApplicationName - $DBWARM_UsersTableAction
User: $DBWARM_UserName
Full Name: $DBWARM_UserFullName
Group: $DBWARM_UserGroup
Previous Name: $DBWARM_UserNameWas
"@
Add-Content -Path $logPath -Value $logEntry
# Show MessageBox with all parameters
Add-Type -AssemblyName System.Windows.Forms
$message = @"
DBWArm User Event
Application: $DBWorksApplicationName
Action: $DBWARM_UsersTableAction
User Name: $DBWARM_UserName
Full Name: $DBWARM_UserFullName
User Group: $DBWARM_UserGroup
Previous Name: $DBWARM_UserNameWas
"@
# Different icons based on event type
$icon = [System.Windows.Forms.MessageBoxIcon]::Information
# Example: Different actions based on event type
switch ($DBWARM_UsersTableAction) {
"USER_ADDED" {
$icon = [System.Windows.Forms.MessageBoxIcon]::Information
# Add your custom logic here
# Example: Send email notification
# Example: Create user directory
# Example: Initialize user settings
}
"USER_REMOVED" {
$icon = [System.Windows.Forms.MessageBoxIcon]::Warning
# Add your custom logic here
# Example: Archive user data
# Example: Send notification
# Example: Clean up user resources
}
"USER_MODIFIED" {
$icon = [System.Windows.Forms.MessageBoxIcon]::Information
# Add your custom logic here
# Example: Update external systems
# Example: Log the change
# Example: Validate new settings
}
}
[System.Windows.Forms.MessageBox]::Show(
$message,
"DBWArm - $DBWARM_UsersTableAction",
[System.Windows.Forms.MessageBoxButtons]::OK,
$icon
)
# Return success (exit code 0)
exit 0
}
catch {
# Write error to stderr (will be captured and shown in MessageBox)
Write-Error "Error in DBWArm script: $_"
Write-Error $_.Exception.Message
Write-Error $_.ScriptStackTrace
# Return error code
exit 1
}
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
OnDBWArmUsersTableUpdated.py
Python script example for DBWArm user table events
This script receives the same parameters as VBS/PowerShell versions
but as command-line arguments.
Usage:
python OnDBWArmUsersTableUpdated.py <app_name> <action> <user> <fullname> <group> <old_user>
Arguments:
sys.argv[1]: DBWorksApplicationName (always "DBWArm")
sys.argv[2]: DBWARM_UsersTableAction ("USER_ADDED", "USER_REMOVED", "USER_MODIFIED")
sys.argv[3]: DBWARM_UserName (username)
sys.argv[4]: DBWARM_UserFullName (full name)
sys.argv[5]: DBWARM_UserGroup (group name)
sys.argv[6]: DBWARM_UserNameWas (previous username, empty for ADD/REMOVE)
"""
import sys
import os
from datetime import datetime
import tkinter as tk
from tkinter import messagebox
def main():
try:
# Parse command-line arguments
if len(sys.argv) < 7:
raise ValueError("Insufficient arguments. Expected 6 parameters.")
app_name = sys.argv[1]
action = sys.argv[2]
user_name = sys.argv[3]
full_name = sys.argv[4]
user_group = sys.argv[5]
old_user_name = sys.argv[6]
# Log to file
log_path = os.path.join(os.environ.get('TEMP', '/tmp'), 'DBWArm_UserEvents.log')
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
with open(log_path, 'a', encoding='utf-8') as f:
f.write(f"[{timestamp}] {app_name} - {action}\n")
f.write(f" User: {user_name}\n")
f.write(f" Full Name: {full_name}\n")
f.write(f" Group: {user_group}\n")
f.write(f" Previous Name: {old_user_name}\n")
f.write("\n")
# Show MessageBox with all parameters (using tkinter)
# Hide the root window
root = tk.Tk()
root.withdraw()
# Build message
message = f"""DBWArm User Event
Application: {app_name}
Action: {action}
User Name: {user_name}
Full Name: {full_name}
User Group: {user_group}
Previous Name: {old_user_name}"""
# Choose icon based on action
icon_map = {
'USER_ADDED': 'info',
'USER_REMOVED': 'warning',
'USER_MODIFIED': 'info'
}
icon = icon_map.get(action, 'info')
# Show MessageBox
messagebox.showinfo(
title=f"DBWArm - {action}",
message=message,
icon=icon
)
# Custom logic based on action
if action == 'USER_ADDED':
# Add your custom logic here
# Example: Send email notification
# Example: Create user directory
# Example: Initialize user settings
pass
elif action == 'USER_REMOVED':
# Add your custom logic here
# Example: Archive user data
# Example: Send notification
# Example: Clean up user resources
pass
elif action == 'USER_MODIFIED':
# Add your custom logic here
# Example: Update external systems
# Example: Log the change
# Example: Validate new settings
pass
# Success
return 0
except Exception as e:
# Write error to stderr (will be captured by DBWArm)
sys.stderr.write(f"Error in DBWArm Python script: {str(e)}\n")
sys.stderr.write(f"Exception type: {type(e).__name__}\n")
# Also show error in MessageBox
try:
root = tk.Tk()
root.withdraw()
messagebox.showerror(
title="DBWArm Script Error",
message=f"Error in Python script:\n\n{str(e)}"
)
except:
pass # If GUI fails, just use stderr
# Return error code
return 1
if __name__ == '__main__':
sys.exit(main())