我想调用myScript1的执行。第二个myScript2中的ps1脚本。Powershell ISE中的ps1脚本。

下面是MyScript2中的代码。ps1,从Powershell管理工作很好,但不工作在Powershell ISE:

#Call myScript1 from myScript2
invoke-expression -Command .\myScript1.ps1

当我执行MyScript2时,我得到了以下错误。ps1来自PowerShell ISE:

术语“。\myScript1.”Ps1’不能识别为cmdlet、函数、脚本文件或可操作程序的名称。检查名称的拼写,或者如果包含了路径,请验证路径是否正确,然后重试。


当前回答

一句话解决方案:

& ((Split-Path $MyInvocation.InvocationName) + "\MyScript1.ps1")

其他回答

一句话解决方案:

& ((Split-Path $MyInvocation.InvocationName) + "\MyScript1.ps1")

你可能已经找到了答案,但下面是我要做的。

我通常把这一行放在安装脚本的开头:

if(!$PSScriptRoot){ $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent } #In case if $PSScriptRoot is empty (version of powershell V.2).  

然后我可以使用$PSScriptRoot变量作为当前脚本(路径)的位置,就像下面的例子:

if(!$PSScriptRoot){ $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent } #In case if $PSScriptRoot is empty (version of powershell V.2).  

Try {
If (Test-Path 'C:\Program Files (x86)') {
    $ChromeInstallArgs= "/i", "$PSScriptRoot\googlechromestandaloneenterprise64_v.57.0.2987.110.msi", "/q", "/norestart", "/L*v `"C:\Windows\Logs\Google_Chrome_57.0.2987.110_Install_x64.log`""
    Start-Process -FilePath msiexec -ArgumentList $ChromeInstallArgs -Wait -ErrorAction Stop
    $Result= [System.Environment]::ExitCode
} Else {
    $ChromeInstallArgs= "/i", "$PSScriptRoot\googlechromestandaloneenterprise_v.57.0.2987.110.msi", "/q", "/norestart", "/L*v `"C:\Windows\Logs\Google_Chrome_57.0.2987.110_Install_x86.log`""
    Start-Process -FilePath msiexec -ArgumentList $ChromeInstallArgs -Wait -ErrorAction Stop
    $Result= [System.Environment]::ExitCode
    }

} ### End Try block


Catch  {
    $Result = [System.Environment]::Exitcode
    [System.Environment]::Exit($Result)
   }
[System.Environment]::Exit($Result)

在你的情况下,你可以替换

起动过程……与

Invoke-Expression PSScriptRoot \ ScriptName.ps1美元

您可以在Microsoft网站https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/about/about_automatic_variables上阅读有关$MYINVOCATION和$PSScriptRoot自动变量的更多信息

如果上面的变量为空,您可能正在使用PowerShell ISE。在这种情况下尝试:

如果(psISE美元) { Split-Path -Path $ ppse . currentfile . fullpath } 其他的 { 全球美元:PSScriptRoot }

看看这个讨论。

I had a problem with this. I didn't use any clever $MyInvocation stuff to fix it though. If you open the ISE by right clicking a script file and selecting edit then open the second script from within the ISE you can invoke one from the other by just using the normal .\script.ps1 syntax. My guess is that the ISE has the notion of a current folder and opening it like this sets the current folder to the folder containing the scripts. When I invoke one script from another in normal use I just use .\script.ps1, IMO it's wrong to modify the script just to make it work in the ISE properly...

这只是对答案的补充信息 为了将参数传递给另一个文件

哪里会有争论

PrintName.ps1

Param(
    [Parameter( Mandatory = $true)]
    $printName = "Joe"    
)


Write-Host $printName

如何调用文件

Param(
    [Parameter( Mandatory = $false)]
    $name = "Joe"    
)


& ((Split-Path $MyInvocation.InvocationName) + "\PrintName.ps1") -printName $name

如果你不提供任何输入,它将默认为“Joe”,这将作为参数传递到printName中的printName参数。ps1文件 它会反过来打印出“Joe”字符串