{{ message }}
Repository navigation
Running a script in a headless environment #20291
Answered
by
agammann
skycommand
asked this question in
Q&A
Replies: 1 comment 1 reply
|
Start PowerShell with pwsh -NoProfile -NonInteractive -File ./script.ps1This makes PowerShell host prompts fail instead of waiting for input. I tested your nonempty-directory example on PowerShell 7.6.5: See If the caller also requires a nonzero exit status, wrap the automation commands inside the script: $ErrorActionPreference = 'Stop'
try {
# Your automation commands here
} catch {
Write-Error $_ -ErrorAction Continue
exit 1
}I also tested this wrapper with the same |
1 reply
Answer selected by
skycommand
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

Start PowerShell with
-NonInteractive:This makes PowerShell host prompts fail instead of waiting for input. I tested your nonempty-directory example on PowerShell 7.6.5:
Remove-ItemreportedPowerShell is in NonInteractive mode. Read and Prompt functionality is not available., and the directory remained intact.See
about_Pwsh -NonInteractive. It applies to PowerShell prompts, includingRead-Hostand confirmation prompts; external programs can have their own input behavior.If the caller also requires a nonzero exit status, wrap the automation commands inside the script: