SCRT-HQ / PSGSuite

Powershell module for Google / G Suite API calls wrapped in handy functions. Authentication is established using a service account via P12 key to negate the consent popup and allow for greater handsoff automation capabilities
https://psgsuite.io/
Apache License 2.0
234 stars 68 forks source link

Update-GSUser cannot be run in non-interactive mode #343

Closed ShamanStarsha closed 3 years ago

ShamanStarsha commented 3 years ago

Describe the bug Attempting to run Update-GSUser in non-interactive mode fails. Update-GSUser always prompts for confirmation when running in interactive mode. Documentation suggests it should only prompt when -confirm is entered

To Reproduce Steps to reproduce the behavior:

  1. Ran from within Jenkins:
  2. Update-GSUser -user username@domain.com -RecoveryEmail recovery@otherdomain.com

Expected behavior Expect the command without -confirm to not prompt for confirmation.

Screenshots Update-GSUser : Exception calling "ShouldProcess" with "1" argument(s): "Windows PowerShell is in NonInteractive mode. Read and Prompt functionality is not available." At C:\Windows\TEMP\jenkins6105354103505545278.ps1:115 char:13

Environment (please complete the following information):

Additional context Set line 28671 in PSGSuite.psm1 from "if ($PSCmdlet.ShouldProcess("Updating user '$U'")) {" to "if ($true) {" and I get the expected behaviour. edit: same behaviour problem with Remove-GSUser

scrthq commented 3 years ago

Hey @ShamanStarsha - This is default PowerShell behavior for cmdlets/functions using ShouldProcess and a ConfirmPreference set higher than the default of Medium. The Confirm parameter is a built-in parameter for advanced functions/cmdlets when using those settings and the documentation comes from PowerShell itself. All Update-* and Remove-* functions in PSGSuite leverage this to prevent accidental/unexpected changes when using the module.

That being said, to disable confirmation for non-interactive scripts (or while using interactive as well), you would negate it like a typical Switch parameter by using: -Confirm:$false. Note the colon between the parameter and boolean value, not a space. This is important for Switch parameters and will not work otherwise.

More reading: https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-shouldprocess?view=powershell-5.1

Hope this helps!