Windows ServerマシンでPowerShell V 4.0を使用しています。デバッグや解決策を見つけることができないという問題が発生しました。インポートされたモジュールの関数は、powershellスクリプトでは使用できません。
2つのpsm1モジュールAとBをインポートするps1スクリプトがあります(Bは別のモジュールCをインポートします)。
Import-Module $PSScriptRoot\..\lib\infra\moduleA.psm1
Import-Module $PSScriptRoot\..\lib\nwm\moduleB.psm1
#get-logger function works fine. This is defined in moduleA
$log = get-logger
$smisXmlData = [xml] $smisConfigData
#The function get-hostLocalCred is defined in moduleB. This is where the error occurs.
($username, $password) = get-hostLocalCred $smisXmlData
私は、スクリプト内の第2のモジュールmoduleBから関数を使用することはできませんよ。スクリプトを実行すると、モジュールBの関数が使用されていた場所でエラーがスローされます。エラーは以下のとおりです(get-hostLocalCredは関数名です)。
get-hostLocalCred : The term 'get-hostLocalCred' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
以下はモジュールBの内容です。
#Importing moduleC.
Import-Module $PSScriptRoot/../infra/moduleC.psm1
#Defining required functions. These are the functions that are not available in the above script.
function get-hostLocalCred {
Param($xmldata)
$log.entry()
$username = $xmldata.component.yourhost.localcred.username
$log.debug("USERNAME: $username")
$password = $xmldata.component.yourhost.localcred.password
$log.exit()
return $username, $password
}
function new-ArrayCredObj {
Param([Parameter(Mandatory = $true)] $user,
[Parameter(Mandatory = $true)] $password)
$log.entry()
$log.info("Creating custom Object")
$log.debug("User : $user")
$log.debug("Pwd : $password")
$arrayCred = New-Object psobject
$arrayCred | Add-Member -MemberType NoteProperty -Name auser -Value $user
$arrayCred | Add-Member -MemberType NoteProperty -Name password -Value $password
$log.exit()
return $arrayCred
}
.
.
.
.
moduleAの関数は正しく実行されていますが、moduleBから関数を実行することはできません。また 、私はモジュールBで利用可能な機能を検索しようとすると、次のコマンドレットを使用して、コンソールでスクリプトを実行した後、
Get-Command -Module ModuleB
IのみによってインポートさModuleC、で定義された関数を参照してくださいmoduleBには定義されている関数は表示されません。私はpowershellで作業していますが、これは初めての問題です。
Get-Moduleを実行すると、moduleAとmoduleBだけが表示されます。
すべてのモジュールは、次のようにインポートされます。
Import-Module $PSScriptRoot/../lib/newmodules/moduleB.psm1
インポートモジュールは、世界的にも問題は解決していません。
次のように実際のパスを指定してモジュールをインポートしても問題は解決されません。
Import-Module C:\folderpath\ModuleB.psm1
すべてのモジュールのすべての機能は、次のように定義されています。どのモジュールでも関数定義に違いはありません。
function get-hostLocalCred {
Param($xmldata)
# Function Definition
return $result
}
私は簡単なことを見逃しているかもしれませんが、私はそれを得ることができません。私はモジュールを通常どおりにインポートしていて、長時間使っていますが、これが問題になったのは初めてです。助けを前にありがとう。
[mcve]を提供できますか?復興の大事なことは何も言い難い。 – PetSerAl
@PetSerAl:簡単なコードは残念です。スクリプトとモジュールの一部を追加しました。 – user3543477
ModuleBでExport-ModuleMember呼び出しがありますか? –