无法从另一个脚本调用PowerShell脚本
问题描述:
我正在尝试调用文件enable.ps1从另一个文件begin.ps1。这两个文件都在同一个文件夹中。所以,我认为这可能是我可以使用下面的代码来达到这个目的。无法从另一个脚本调用PowerShell脚本
这里是我写的代码begin.ps1进行调用。
#
# begin.ps1
#
function MapDrives ($Message)
{
Write-Host Network drives does not exist till now. Trying again to connect
Write-Host ...............................................................
WriteInLogFile "Network drives does not exist till now. Trying again to connect"
$ScriptPath = Split-Path $MyInvocation.InvocationName
& "$ScriptPath\enable.ps1"
cmd /c pause | out-null
Start-Sleep -s 20
}
有我想打电话给PowerShell的文件:enable.ps1
- 我使用Visual Studio 2015年
- Windows 7的
- PowerShell的5
-
都begin.ps1和enable.ps1在相同的文件夹位置是这样的:
C:\用户\ srijani.ghosh \文档\ Visual Studio的2015年\项目\测试\测试
你对我应该如何着手对此有何想法?
P.S:按照Martin的建议做了一些改变。现在代码如下所示:
function MapDrives ($Message)
{
Write-Host Network drives does not exist till now. Trying again to connect
Write-Host ...............................................................
WriteInLogFile "Network drives does not exist till now. Trying again to connect"
$ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition
& "$ScriptPath\enable.ps1"
cmd /c pause | out-null
Start-Sleep -s 20
}
而且,我试图在PowerShell ISE中运行它。它给这个错误。
Network drives does not exist till now. Trying again to connect
...............................................................
& : The module 'param($Message)
Write-Host Network drives does not exist till now. Trying again to connect
Write-Host ...............................................................
WriteInLogFile "Network drives does not exist till now. Trying again to connect"
$ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition
& "$ScriptPath' could not be loaded. For more information, run 'Import-Module param($Message)
Write-Host Network drives does not exist till now. Trying again to connect
Write-Host ...............................................................
WriteInLogFile "Network drives does not exist till now. Trying again to connect"
$ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition
& "$ScriptPath'.
At C:\Users\srijani.ghosh\Documents\visual studio 2015\Projects\test\test\begin.ps1:45 char:7
+ & "$ScriptPath\enable.ps1"
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (param($Message)...cmd \enable.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CouldNotAutoLoadModule
答
你做得对。但它看起来像$ScriptPath
不包含任何值(如您在错误消息中看到的)。
你可能不得不更换
$ScriptPath = Split-Path $MyInvocation.InvocationName
与
$ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition
感谢您的答复,但不工作要么:( –
你如何执行begin.ps1脚本? –
我正在使用visual studio 2015。 –