Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I suppress "Unapproved verbs" in Powershell classes

Tags:

powershell

For using a class in a powershell (V7) script it seems necessary to declare the module, in which the class (Powershell language) is implemented, via

using module .\MyModule.psm1

The module "MyModule.psm1" imports a powershell module via

Import-Module powershell-yaml -DisableNameChecking

How can I suppress the "unapproved verbs" warnings during execution of the script ? The option DisableNameChecking does not seem to help here

Complete example of the module

 Import-Module powershell-yaml -DisableNameChecking

 class TestManager { 
 hidden [string] $NodeTypeApplication = "Application"

 TestManager () {
 }

 [void] StartDeployment()
 {
     Write-Host("starting deployment...")

 }

}
like image 841
Marco Avatar asked Dec 28 '25 04:12

Marco


1 Answers

While you do use -DisableNameChecking to import the nested module (powershell-yaml), the warning can resurface for the enclosing module, if nonstandard functions from a nested module become part the enclosing module's exports.

You have two options:

  • If you do need to export (nested) nonstandard functions from your enclosing module:

    • The only way to silence the warning for the enclosing module too is to import it with
      Import-Module -DisableNameChecking as well, rather than via using module
      .

    • Caveat: Unfortunately, this precludes using PowerShell custom classes defined in your module; as of v7.0, custom classes only become visible to the importer if you use using module (see GitHub issue #2449 for background information).

    • To solve this problem:

      • Define wrapper functions for those nonstandard functions you need to export and give them standards-compliant names.
      • Then exclude the nonstandard functions from your module's exports - see next point.
  • Otherwise, exclude the nonstandard functions from export, which you can do in one of the following ways:

    • If you don't actually need them inside your enclosing module itself, exclude them from import by passing only the names of the functions you do need to the Import-Module's -Function parameter.

    • Otherwise, you can explicitly control what your enclosing module exports:

      • You can use an Export-ModuleMember call in your enclosing module.
      • Alternatively / additionally, you can restrict what functions are exported if you make your enclosing module use a module manifest (*.psd1 file).

Here's a simple demonstration of the original problem:

# Create a temp. nested module with a nonstandard function.
'function UnapprovedVerb-Foo { ''unapproved foo'' }' > tmp_nested.psm1

# Create the enclosing module that imports the nested module
# with warnings suppressed.
# However, because the enclosing module has no manifest, the nested
# functions are exported alongside its own functions.
'Import-Module $PSScriptRoot/tmp_nested.psm1 -DisableNameChecking; function Get-Foo { ''foo'' }' > tmp_enclosing.psm1

# This now triggers the warning - as import via `using module` would.
# Adding -DisableNameChecking would silence it, but `using module` has
# no equivalent mechanism - and you need the latter to import *custom PS classes*.
Import-Module ./tmp_enclosing.psm1
like image 116
mklement0 Avatar answered Dec 31 '25 00:12

mklement0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!