0
Completed

Feature Request: Debug Capability for PowerShell Connector

Bob Bradley 6 years ago in PowerShell connector updated by Adrian Corston 5 years ago 3

Presently debugging a PowerShell connector script requires extensive use of logging.

While the above is still going to be necessary, the ability to attach a PowerShell ISE session to a PowerShell process to allow step-throughs and breakpoints is highly desirable.

Answer

Answer
Completed
# The following approach allows you to debug a PowerShell connector or adapter transform in IdB
# See the following for background:
# - https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/wait-debugger?view=powershell-6
# - https://stackoverflow.com/questions/42731150/how-to-pause-a-powershell-script-until-a-debugger-attaches
# Prerequisite: PowerShell v5, IdB 5.*, PS connector or adapter transform
# 1. Edit the PowerShell code and set a pause in your PowerShell code - long enough to intercept the process and enter debug mode:
# you can use wait-debugger, but for now Matt showed me by using start-sleep instead
#Wait-Debugger -Timeout 10
Start-Sleep 30
# 2. Find the process ID
Get-Runspace
# 3. Attach the ISE 
Enter-PSHostProcess -Name Unify.Service.Connect
# 4. Debug the process
Debug-Runspace -Id xx

GOOD, I'M SATISFIED

Thanks for writing that up for me Matt - you showed it to me before and I was keen to explain this to others.  Now I can!

Satisfaction mark by Bob Bradley 5 years ago
Under review

Hey Bob,

Thanks for the suggestion. We'll investigate - there seems to potentially be a way to debug a script but may not involve the ISE.

Answer
Completed
# The following approach allows you to debug a PowerShell connector or adapter transform in IdB
# See the following for background:
# - https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/wait-debugger?view=powershell-6
# - https://stackoverflow.com/questions/42731150/how-to-pause-a-powershell-script-until-a-debugger-attaches
# Prerequisite: PowerShell v5, IdB 5.*, PS connector or adapter transform
# 1. Edit the PowerShell code and set a pause in your PowerShell code - long enough to intercept the process and enter debug mode:
# you can use wait-debugger, but for now Matt showed me by using start-sleep instead
#Wait-Debugger -Timeout 10
Start-Sleep 30
# 2. Find the process ID
Get-Runspace
# 3. Attach the ISE 
Enter-PSHostProcess -Name Unify.Service.Connect
# 4. Debug the process
Debug-Runspace -Id xx

Additional requirements - you must:

  1. Run ISE as the UNIFYBroker service account credentials

    AND
  2. Run ISE as an Administrator

One way to do this is to log in as the UNIFYBroker service account in order to make the connection.  Since the UNIFYBroker user is not normally granted remote login access this will need to be temporarily granted while debugging is taking place.

An alternative way to achieve this is via the following PowerShell (courtesy Matt Davis):

Start-Process powershell -Credential domain\differentUserName -ArgumentList '-noprofile -command &{Start-Process "powershell.exe" -verb runas}'

Further clarifications regarding using the commands listed by Bob above:

  1. In ISE (to identify the current RunSpace ID): Get-Runspace
  2. At the start of the the PS transformStart-Sleep -Sec 30
  3. In UNIFYBrokerGenerate Changes to run the transform
  4. In ISE: Enter-PSHostProcess -Name Unify.Service.Connect
  5. In ISEGet-Runspace
  6. Ignore the runspace ID from step 1 above, pick the other one instead
  7. In ISE: Debug-Runspace -Id <ID from step 6>
  8. No prompt will appear until the the transform's Start-Sleep command completes.  Once it does, you can look at $entities and run other commands interactively, and they will be executed in the context of the UNIFYBroker PowerShell transform.
  9. In ISE (to exit the debugging session): exit
  10. After exiting the transform you'll need to re-run from step 4 if you want to connect to a subsequent one.