[英]Cannot import module from Powershell started from Start-Process
我有一个powershell脚本: MyPSScript.ps1
:
function DoFoo() {
$Powershell = "C:\windows\System32\WindowsPowerShell\v1.0\Powershell.exe"
$Command = "& C:\Temp\myscript.ps1"
Start-Process $Powershell -ArgumentList ("-noexit", "-noprofile", "-Command", $Command)
}
在myscript.ps1
我有:
Import-Module "C:\Temp\myscript2.ps1"
DoSomething()
在myscript2.ps1
我有:
function DoSomething() {
Write-Output "Hello World"
}
问题在于myscript.ps1
已正确执行,但未打印任何内容,因此未导入该模块。 另外,由于我使用-noexit
打开了新的powershell窗口,因此可以键入它,如果尝试调用DoSomething
,则Powershell会抱怨,因为它找不到命令。
有趣的是,如果我尝试手动键入相同的导入指令,那么它将正确地导入文件...
从Powershell导入模块时是否存在任何问题,而我正在从其他进程调用该模块?
除了我在问题中修正的错字(缺少“ =”)之外,您提供的脚本中还存在一些问题。 第一个( MyPSScript.ps1
)定义了函数DoFoo,但是从不调用它。 第二个( myscript.ps1
)错误地用括号调用DoSometing()。 如果解决了这两个问题,您将看到“ Hello World”。
MyPSScript.ps1:
function DoFoo() {
$Powershell = "C:\windows\System32\WindowsPowerShell\v1.0\Powershell.exe"
$Command = "& C:\Temp\myscript.ps1"
Start-Process $Powershell -ArgumentList ("-noexit", "-noprofile", "-Command", $Command)
}
DoFoo
myscript.ps1:
Import-Module "C:\Temp\myscript2.ps1"
DoSomething
Powershell模块具有.psm1文件扩展名。
将您的文件“ myscript2.ps1”重命名为“ myscript2.psm1”,然后它将正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.