自定义函数无法识别
Custom function is not recognized
我添加到 PowerShell 配置文件中的一个函数在当前会话期间不可用。该函数如下所示:
# \MyModules\Foobar.ps1
function Foo-Bar {
Write-Host "Foobar";
}
# Test loading of this file
Foo-Bar;
我的个人资料是这样的:
# \Microsoft.PowerShell_profile.ps1
Write-Host "Loading MyModules..."
Push-Location ~\Documents\WindowsPowerShell\MyModules
.\Foobar.ps1
Pop-Location
Write-Host "Done"
当我 运行 . $profile
时,输出如下所示,这证实了 Foo-Bar
函数有效。
> . $profile
Loading MyModules...
Foobar
Done
运行 之后的 Foo-Bar
函数,但是,像这样爆炸:
Foo-Bar : The term 'Foo-Bar' 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:1 char:1
+ Foo-Bar
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (Foo-Bar:String) [],
CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
为什么不可用?
好吧,有几种方法可以解决这个问题。请注意,这些方法中的任何一种都不需要您在导入之前点调用您的模块。
1) 使用合适的模块MyMethod.psm1
# MyMethod.psm1 (m for module)
function MyMethod {
# my method
}
Export-ModuleMember MyMethod
# then in your profile
Import-Module "MyMethod"
2) 如果您有一组方法并且需要将它们分成多个文件
# MyMethod1.ps1
function Invoke-MyMethod1{
# my method1
}
Set-Alias imm Invoke-MyMethod1
# MyMethod2.ps1
function Something-MyMethod2 {
# my method2
}
Set-Alias smm Something-MyMethod2
# MyMethod.psm1 (m for module)
Push-Location $psScriptRoot
. .\MyMethod1.ps1
. .\MyMethod2.ps1
Pop-Location
Export-ModuleMember `
-Alias @(
'*') `
-Function @(
'Invoke-MyMethod1',
'Something-MyMethod2')
# then in your profile
Import-Module "MyMethod"
我添加到 PowerShell 配置文件中的一个函数在当前会话期间不可用。该函数如下所示:
# \MyModules\Foobar.ps1
function Foo-Bar {
Write-Host "Foobar";
}
# Test loading of this file
Foo-Bar;
我的个人资料是这样的:
# \Microsoft.PowerShell_profile.ps1
Write-Host "Loading MyModules..."
Push-Location ~\Documents\WindowsPowerShell\MyModules
.\Foobar.ps1
Pop-Location
Write-Host "Done"
当我 运行 . $profile
时,输出如下所示,这证实了 Foo-Bar
函数有效。
> . $profile
Loading MyModules...
Foobar
Done
运行 之后的 Foo-Bar
函数,但是,像这样爆炸:
Foo-Bar : The term 'Foo-Bar' 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:1 char:1
+ Foo-Bar
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (Foo-Bar:String) [],
CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
为什么不可用?
好吧,有几种方法可以解决这个问题。请注意,这些方法中的任何一种都不需要您在导入之前点调用您的模块。
1) 使用合适的模块MyMethod.psm1
# MyMethod.psm1 (m for module)
function MyMethod {
# my method
}
Export-ModuleMember MyMethod
# then in your profile
Import-Module "MyMethod"
2) 如果您有一组方法并且需要将它们分成多个文件
# MyMethod1.ps1
function Invoke-MyMethod1{
# my method1
}
Set-Alias imm Invoke-MyMethod1
# MyMethod2.ps1
function Something-MyMethod2 {
# my method2
}
Set-Alias smm Something-MyMethod2
# MyMethod.psm1 (m for module)
Push-Location $psScriptRoot
. .\MyMethod1.ps1
. .\MyMethod2.ps1
Pop-Location
Export-ModuleMember `
-Alias @(
'*') `
-Function @(
'Invoke-MyMethod1',
'Something-MyMethod2')
# then in your profile
Import-Module "MyMethod"