2012-11-27 38 views
2

我必須在這裏失去了一些基本的東西,但我是新來的PowerShell ...呼叫/模塊中的一個函數Powershell的

我寫了一個函數和一個名爲「UserSelectionList.psm1」文件保存它時,功能掐滅這樣的:

function Global:UserSelectionList([String[]] $UserOptions) 
{ 
... 
} 

然後我嘗試用這個腳本調用它:

Import-module "l:\support downstream\solarc\cngl\powershell scripts\userselectionlist.psm1" 
$Options = "a","b","c" 
cls 
$result = UserSelectionList $Options 
echo $result 

產生的錯誤是:

The term 'UserSelectionList' 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. 
At line:5 char:28 
+ $result = UserSelectionList <<<< $Options 
    + CategoryInfo   : ObjectNotFound: (UserSelectionList:String) [], CommandNotFoundException 
    + FullyQualifiedErrorId : CommandNotFoundException 

我打算在一個模塊中有多個函數,但這是我所處的位置。

在此先感謝

+0

看起來不對,也許是某個地方的錯字......導入模塊後,運行Get-Module userselectionlist和Get-Item FunctionSelectList的結果是什麼? – latkin

+0

Get-Module UserSelectionList產生:PS L:\ Support Downstream \ Solarc \ CNGL \ Powershell腳本> Get-Module users選擇列表 ModuleType名稱導出命令 ---------- ------ ----------- 腳本userselectionlist {} – codeputer

+0

Get-Item功能:錯誤與「無法找到路徑」功能:\ UserSelectionList',因爲它不存在。「 – codeputer

回答

1

[編輯]我沒有做一個導入模塊與-Force選項。下面的答案是不正確的,但也許Get-Command強制刷新?無論哪種方式,我都會爲了完整體驗而離開它!

感謝latkin推我到另一條路徑,我發現這一點:(?)

How do I retrieve command from a module

你不僅要導入一個模塊,你就必須「搞定」它,以及

Import-Module -Name <ModuleName> 
Get-Command -Module <ModuleName> 

我發佈了Get-Command之後,一切都開始奏效!

感謝拉金快速響應!

+0

'import-module -name ModuleName'後面不需要任何'get-command -module Modulename'來使用ModuleName中的函數/ cmdlet。你有一個奇怪的行爲,不是一個正常的行爲。 –

+0

奇怪它會在我輸入這些命令後正常工作......我會繼續測試並看看發生了什麼。一旦我導入,重新導入會給我最新的函數副本嗎?也許我在每次編輯後都不清醒? – codeputer

+0

重新導入一個模塊是由'import-module -name ModuleName -Force'完成的 –

0

如果您未從模塊正確導出方法,則只需要Get-Command

在你的模塊放在這到底:

Export-ModuleMember -Function UserSelectionList 

注意,它也接受外卡,因此,例如,如果你有一個遵循命名約定,你可以只是5個不同的GET-一些,價值功能做-Force

Export-ModuleMember -Function Get-* 

邊注:所有所做的是檢查是否有模塊已經加載,如果是這樣,與進口繼續之前刪除它。這就像說:

Remove-Module userselectionlist.psm1 
Import-Module userselectionlist.psm1 
0

我遇到了同樣的問題。重現步驟:

  • 寫PowerShell腳本與Import-Module聲明
  • 在PowerShell提示符
  • 執行腳本的功能添加到導入模塊
  • 修改腳本調用新增功能
  • 再次執行該腳本,在相同的PowerShell會話

錯誤走AFTE r我將-Force參數添加到Import-Module。一旦導入模塊中的功能能夠被調用,-Force參數就可以被移除。

請注意,latkin已暗示此解決方案在他的comment的問題。我希望這會讓它更加明顯。