我在PowerShell腳本模塊中定義了一大組函數。我想用Export-ModuleMember -Function *
,但我想排除只有一個函數。排除這一個功能比列出所有包含的功能要容易得多。無論如何要實現這一目標?我可以從Export-ModuleMember中排除單個函數嗎?
5
A
回答
16
我的股票回答關於排除函數是使用動詞名詞命名我想要導出的函數,並使用首字母大寫的一切。
然後,Export-ModuleMember -function *-*
照顧它。
4
查找腳本的所有功能,然後篩選基於要排除(假設的PowerShell V2)什麼:
$errors = $null
$functions = [system.management.automation.psparser]::Tokenize($psISE.CurrentFile.Editor.Text, [ref]$errors) `
| ?{(($_.Content -Eq "Function") -or ($_.Content -eq "Filter")) -and $_.Type -eq "Keyword" } `
| Select-Object @{"Name"="FunctionName"; "Expression"={
$psISE.CurrentFile.Editor.Select($_.StartLine,$_.EndColumn+1,$_.StartLine,$psISE.CurrentFile.Editor.GetLineLength($_.StartLine))
$psISE.CurrentFile.Editor.SelectedText
}
}
這是我用來V2創建ISE Function Explorer的技術。但是,我沒有看到爲什麼這種方式不能在ISE之外使用純文本。儘管如此,您仍需要解決脫字符行細節問題。這只是一個如何實現你想要的例子。
現在,過濾什麼是不需要的,並將其傳遞給Export-ModuleMember
!
$functions | ?{ $_.FunctionName -ne "your-excluded-function" }
如果您使用PowerShell v3,parser makes it a lot easier。
0
我的解決方案,使用PowerShell的V3,通過ravikanth(誰使用V2在他的解決方案)的暗示,是定義PSParser
模塊:
Add-Type -Path "${env:ProgramFiles(x86)}\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll"
Function Get-PSFunctionNames([string]$Path) {
$ast = [System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$null, [ref]$null)
$functionDefAsts = $ast.FindAll({ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $true)
$functionDefAsts | ForEach-Object { $_.Name }
}
Export-ModuleMember -Function '*'
和模塊中,如果我想排除給定函數,最後一行看起來像:
Export-ModuleMember -Function ((Get-PSFunctionNames $PSCommandPath) | Where { $_ -ne 'MyPrivateFunction' })
請注意,這隻會在PowerShell中V3工作或後來因爲AST解析器和$PSCommandPath
版本中引入3
相關問題
- 1. 我可以「排隊」XMLHttpRequest函數嗎?
- 2. 我可以從字典中刪除單個值嗎?
- 3. 我可以從.htacces中只爲單個頁面刪除.html嗎?
- 4. 可以從這個函數中刪除遞歸嗎?
- 5. 我們可以稱這個函數爲快速排序嗎
- 6. 我可以從form.reload中排除輸入字段嗎?
- 7. 我可以在rspec中使用多個排除過濾器嗎?
- 8. 我可以刪除一個php函數嗎?
- 9. 函數可以從Python中的另一個函數繼承嗎?
- 10. Codeigniter:我可以從同一個函數返回多個值嗎?
- 11. 我可以從我的場景中刪除一個對象嗎?
- 12. 我可以將參數傳遞給std :: vector排序函數嗎?
- 13. jQuery - 從這個函數中排除李?
- 14. 從函數中排除一個類
- 15. 我可以從CUDA內核函數調用__device__函數嗎?
- 16. 我可以從iphone原生函數調用javascript函數嗎?
- 17. 從函數中刪除「靜態」可以嗎?
- 18. 我可以從另一個函數返回返回值嗎?
- 19. 我可以從遞歸搜索函數返回兩個值嗎?
- 20. 我可以從LotusScript函數返回一個列表嗎?
- 21. 窗口函數排序昂貴,我們可以克服它嗎?
- 22. 我可以調用另一個函數寫入的函數嗎?
- 23. 我可以通過php函數觸發一個javascript函數嗎?
- 24. 我可以在一個腳本中使用多個函數嗎?
- 25. 我可以在一個函數中設置一個狀態嗎?
- 26. 我們可以在Java中的函數中調用函數嗎?
- 27. 我們可以從DOM中移除一個元素嗎?
- 28. 我可以從git倉庫中刪除整個分支嗎?
- 29. 我可以在Python中的函數中聲明一個類嗎?
- 30. 我可以在C++中使用庫中包含/導入單個函數嗎
這個答案的簡單和常規基礎真的對我說話! –