为什么Powershell在第一次执行脚本后无法找到我导入的命令?

为什么Powershell在第一次执行脚本后无法找到我导入的命令?,powershell,Powershell,我有两个Powershell脚本。脚本A包含一个简单的Powershell函数。脚本B导入脚本A,然后调用脚本A中定义的函数。第一次执行脚本B时,此操作正常,但在后续执行时,Powershell会抱怨无法找到脚本A中定义的函数。在脚本B中向导入模块调用添加-Force似乎可以解决问题,但我想了解为什么Powershell会出现这种意外行为 这是脚本A function foo($param) { Write-Host $param } 这是脚本B Import-Module "C:\S

我有两个Powershell脚本。脚本A包含一个简单的Powershell函数。脚本B导入脚本A,然后调用脚本A中定义的函数。第一次执行脚本B时,此操作正常,但在后续执行时,Powershell会抱怨无法找到脚本A中定义的函数。在脚本B中向导入模块调用添加-Force似乎可以解决问题,但我想了解为什么Powershell会出现这种意外行为

这是脚本A

function foo($param)
{
    Write-Host $param
}
这是脚本B

Import-Module "C:\SomePath\scriptA.ps1"

Foo "hello"
我正在从PS命令行调用脚本:

.\scriptB.ps1
这是我在执行脚本的第二次和后续时间得到的错误


脚本A不是powershell模块(因为它有ps1扩展)。相反,请尝试这样的dot sourcing scriptA:

. c:\somepath\scripta.ps1
foo "hello"

脚本A不是powershell模块(因为它有ps1)扩展。相反,请尝试这样的dot sourcing scriptA:

. c:\somepath\scripta.ps1
foo "hello"
k、 我是说

Windows PowerShell
Copyright (C) 2015 Microsoft Corporation. All rights reserved.

PS C:\Users\joshua> cd .\Desktop
PS C:\Users\joshua\Desktop> .\scriptB.ps1
hello
PS C:\Users\joshua\Desktop> .\scriptB.ps1
Foo : The term 'Foo' 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 C:\Users\joshua\Desktop\scriptB.ps1:3 char:1
+ Foo "hello"
+ ~~~
    + CategoryInfo          : ObjectNotFound: (Foo:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\Users\joshua\Desktop> Get-Command -Module scriptA
PS C:\Users\joshua\Desktop> get-module scriptA

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     0.0        scriptA


PS C:\Users\joshua\Desktop> Remove-Module scriptA
PS C:\Users\joshua\Desktop> . .\scriptB.ps1
hello
PS C:\Users\joshua\Desktop> Get-Command -Module scriptA
PS C:\Users\joshua\Desktop> get-module scriptA

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     0.0        scriptA


PS C:\Users\joshua\Desktop>
似乎ps1是作为模块加载的,但作用域是怎么回事;(

k,我是说

Windows PowerShell
Copyright (C) 2015 Microsoft Corporation. All rights reserved.

PS C:\Users\joshua> cd .\Desktop
PS C:\Users\joshua\Desktop> .\scriptB.ps1
hello
PS C:\Users\joshua\Desktop> .\scriptB.ps1
Foo : The term 'Foo' 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 C:\Users\joshua\Desktop\scriptB.ps1:3 char:1
+ Foo "hello"
+ ~~~
    + CategoryInfo          : ObjectNotFound: (Foo:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\Users\joshua\Desktop> Get-Command -Module scriptA
PS C:\Users\joshua\Desktop> get-module scriptA

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     0.0        scriptA


PS C:\Users\joshua\Desktop> Remove-Module scriptA
PS C:\Users\joshua\Desktop> . .\scriptB.ps1
hello
PS C:\Users\joshua\Desktop> Get-Command -Module scriptA
PS C:\Users\joshua\Desktop> get-module scriptA

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     0.0        scriptA


PS C:\Users\joshua\Desktop>

似乎ps1是作为模块加载的,但作用域是怎么回事?;(

不确定,但可能相关:介意试试“。\scriptB.ps1”?:)
导入模块
不会第二次加载模块,除非您使用
-Force
开关。这应该可以工作。它对我有效。可能是您已经在会话中导入了具有该名称的模块(不会自动重新导入)。尝试将-Force添加到导入模块命令。您必须删除并重新导入或-Force不确定,但可能相关:请尝试“。\scriptB.ps1”?:)
导入模块不会再次加载模块,除非您使用
-Force
开关。这应该可以工作。它对我有用。可能您已经在会话中导入了具有该名称的模块(它不会自动重新导入)。尝试在导入模块命令中添加-Force。当然,您必须删除并重新导入或-Force ps1不能像模块一样加载?@JaquelineVanek Yes。这里的问题是,scriptb正在加载模块,当它退出时,模块会超出范围(即使get模块将其显示为已加载)。我认为真正的答案是,从脚本中加载该模块是个问题。我建议在退出之前,要么像前面提到的那样进行点源搜索,要么在scriptb中添加逻辑以卸载模块。ps1不能像模块一样加载吗?@JaquelineVanek是的。这里的问题是,scriptb正在加载模块,当它退出时,模块会超出范围(即使get模块将其显示为已加载)。我认为真正的答案是,从脚本中加载该模块是个问题。我建议在退出之前,要么像前面提到的那样进行点源搜索,要么向scriptb添加逻辑以卸载模块。我没有标记这是可接受的答案,但感谢您指出卸载模块允许再次导入。嘿,这不是答案,只是说明了我的想法。此外,PetSerAI指出,我没有标记这是公认的答案,但感谢您指出卸载模块允许再次导入。嘿,这不是答案,只是说明了我的想法。此外,佩瑟莱指出了这一点