mOjO
mOjO

Reputation: 98

Verb Warning when loading modules with #Requires -Modules

I have a script which begins with:

#Requires -Modules ActiveDirectory, Microsoft.Online.SharePoint.PowerShell

which is all fine and dandy except that Sharepoint PS module throws a verb warning:

WARNING: The names of some imported commands from the module 'Microsoft.Online.SharePoint.PowerShell' include unapproved verbs that might make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the Verbose parameter. For a list of approved verbs, type Get-Verb.

I'd like to use the #Requires -Modules header in the script but suppress the warning.

I know there are ways to suppress all warnings in the shell before running the script but wondered if there was a better way to do it within the script.

Upvotes: 1

Views: 2114

Answers (2)

Bacon Bits
Bacon Bits

Reputation: 32180

I'm not sure if something like this might work:

$OriginalWarningPreference = $WarningPreference
$WarningPreference = 'SilentlyContinue'
#Requires -Modules ActiveDirectory, Microsoft.Online.SharePoint.PowerShell
$WarningPreference = $OriginalWarningPreference

Alternately, you can sacrifice some of the functionality of #Requires and do this:

#Requires -Modules ActiveDirectory
Import-Module -Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking -ErrorAction Stop

Upvotes: 2

a rather sneaky Workaround would be to use #requires only for the modules, that dont produce warnings and use

if (-not (get-module Microsoft.Online.SharePoint.PowerShell)) {
  Import-Module Microsoft.Online.SharePoint.PowerShell -warningaction silentlycontinue
}

for the ones not strictly following naming conventions

Upvotes: 1

Related Questions