【发布时间】:2017-07-12 12:56:50
【问题描述】:
我正在尝试从另一个文件 begin.ps1 调用文件 enable.ps1。这两个文件都在同一个文件夹中。所以,我想可能我可以使用下面的代码来达到这个目的。
这是我在 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:\Users\srijani.ghosh\Documents\visual studio 2015\Projects\test\test
您对我应该如何进行此操作有任何想法吗?
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
【问题讨论】:
标签: windows powershell windows-7 powershell-5.0