Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ All scripts follow a consistent three-part structure defined in `script-template

2. **Input Handling Section**
- **All RMM-supplied variables come via environment variables** (`$env:VarName`). NinjaRMM passes script preset variables to PowerShell as environment variables, so the script must read them via `$env:` at every use site. Bare `$VarName` references resolve to `$null` in true RMM mode and silently fall through to the interactive branch.
- Detects execution context via `$env:RMM` — environment variables are strings, so compare against `"1"` not `1`. Anything other than `"1"` is interactive mode.
- Detects execution context via `$env:RMM` — environment variables are strings, so compare against `"1"` not `1`.
- **Gate the interactive branch on an interactive session too:** `if ($env:RMM -ne "1" -and [Environment]::UserInteractive)`. NinjaRMM runs scripts non-interactively (`[Environment]::UserInteractive` is `$false`), so if the RMM preset variable is ever missing, renamed, or not seen as `"1"`, the script still falls through to RMM mode instead of erroring/looping forever on `Read-Host` (`"Windows PowerShell is in NonInteractive mode. Read and Prompt functionality is not available."`). Never gate solely on `$env:RMM` — a single missing variable should never be able to hang an unattended job.
- Interactive mode: Prompts user with `Read-Host` for required inputs with validation loop. Write the result back to `$env:Description` so the rest of the script can keep referencing `$env:` consistently.
- RMM mode: Uses pre-set environment variables passed by the RMM platform. Defaults for any optional variables (custom field names, state file paths, etc.) should be set at the top of the script by writing to `$env:` directly:
```powershell
Expand Down
5 changes: 4 additions & 1 deletion msft-windows/msft-windows-disable-core-isolation.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

$ScriptLogName = "msft-windows-disable-core-isolation.log"

if ($RMM -ne 1) {
# Only prompt when the session is genuinely interactive. NinjaRMM runs non-interactively
# ([Environment]::UserInteractive is $false), so a non-interactive run never reaches Read-Host
# (which errors with "Windows PowerShell is in NonInteractive mode" and loops forever).
if ($RMM -ne 1 -and [Environment]::UserInteractive) {
$ValidInput = 0
# Checking for valid input.
while ($ValidInput -ne 1) {
Expand Down
5 changes: 4 additions & 1 deletion msft-windows/msft-windows-enable-core-isolation.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

$ScriptLogName = "msft-windows-enable-core-isolation.log"

if ($RMM -ne 1) {
# Only prompt when the session is genuinely interactive. NinjaRMM runs non-interactively
# ([Environment]::UserInteractive is $false), so a non-interactive run never reaches Read-Host
# (which errors with "Windows PowerShell is in NonInteractive mode" and loops forever).
if ($RMM -ne 1 -and [Environment]::UserInteractive) {
$ValidInput = 0
# Checking for valid input.
while ($ValidInput -ne 1) {
Expand Down
5 changes: 4 additions & 1 deletion script-template-powershell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ $ScriptLogName = "EnterLogNameHere.log"

# --- Input handling: RMM vs interactive ----------------------------------

if ($env:RMM -ne "1") {
# Only prompt in a genuinely interactive session. NinjaRMM runs non-interactively
# ([Environment]::UserInteractive is $false), so a missing/renamed RMM variable can never
# leave the script erroring/looping on Read-Host in NonInteractive mode.
if ($env:RMM -ne "1" -and [Environment]::UserInteractive) {
$ValidInput = 0
# Checking for valid input.
while ($ValidInput -ne 1) {
Expand Down