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