発生状況
PowerShellで共通化モジュールを作って、他のPowerShellから呼び出そうとした際に、以下のエラーが発生した。
The Export-ModuleMember cmdlet can only be called from inside a module.
処理内容
呼び出す側
call.ps1
# 共通化モジュールを読み込む(ここでエラーが発生)
Import-Module .\called.ps1
# エクスポートした関数を呼び出す
test()
呼び出される側
called.ps1
function test(){
Write-Output '呼び出された'
}
Export-ModuleMember -Function test
原因と解決方法
呼び出される側のファイルが.ps1ファイルだったこと。
.psm1というモジュール用の拡張子があるため、called.psm1とするのが正解らしい。