+2 votes

Most of my PowerShell code is written in VS Code and then copied/pasted to the Adaxes script editor. However, if you use $Context in your scripts, you can't execute them from VS Code (or ISE, Terminal, etc.).

I was recently working on a script that used the Adaxes PowerShell module and I was only using $Context for logging. I realized that I could emulate $Context.LogMessage() and $Context.LogExcecption() easily, and debug right in VS Code. Obviously, this won't work if you're using more advanced features of $Context, but for basic logging it works great.

PS...I had made an enhancement request a while back for an Adaxes plugin for VS Code. Please upvote that post! https://www.adaxes.com/questions/13285/add-visual-studio-code-support

# DO NOT USE THE CODE BELOW IN THE ADAXES SCRIPT EDITOR

# define the message type to avoid any typos when calling LogMessage()
enum MessageType {
    Information = 0
    Warning = 1
    Error = 2
}

class Context {    
    [string]LogMessage ([string]$message, [MessageType]$type) {        
        return "[$type] $message"
    }
    [string]LogException ([string]$message) {
        return "[Error] $message"
    }
}

$Context = [Context]::new()
$Context.LogMessage("foo", "information")

Output:

[Information] foo
by (240 points)

1 Answer

0 votes
by (277k points)

Hello,

Thank you for sharing the approach. We also added a +1 on your behalf to the suggestion.

0

Really nice and helpful. Saves me now a lot of time in future :D

I always did a find/replace of this command with Write-Output ...

Related questions

0 votes
1 answer

I am trying to send a $context.logmessage from a condition script in a Scheduled Task but I get nothing in the log. Is this not possible? Morten A. Steien

asked Jul 20, 2020 by Morten A. Steien (300 points)
0 votes
1 answer

Hi All, I am currently using the 30 day free trial of Adaxes and seeing if we can use it to achieve our method of user provisioning. I am looking into server-side ... variable value within an SQL query Can this be achieved? Any help is much appreciated, Thanks

asked Feb 1 by Lewis (40 points)
0 votes
1 answer

Receive "Index operation failed; the array index evaluated to null. Stack trace: at <ScriptBlock>, <No file>: line 104>" and "Index operation failed; the ... $GroupName, $GroupDN." } } #foreach write-output "" Write-Output "" Stop-Transcript

asked Apr 14, 2022 by jbahou (20 points)
0 votes
1 answer

Hello I'm trying to run a custom PowerShell script to request a Workspace ONE Access Sync when I change something in our users or groups. Here is the script: $ClientId = "api ... of having to create 6 independent rules with each of them a copy of the script)?

asked Sep 25, 2021 by ygini (240 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 (50 points)
3,412 questions
3,109 answers
7,915 comments
545,865 users