简体   繁体   中英

How can I suppress “Unapproved verbs” in Powershell classes

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...")

 }

}

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 this GitHub issue 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 ](https://docs.microsoft.com/powershell/module/microsoft.powershell.core/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM