我想调用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、函数、脚本文件或可操作程序的名称。检查名称的拼写,或者如果包含了路径,请验证路径是否正确,然后重试。


当前回答

我也遇到过类似的问题,用这种方法解决了。

我的工作目录是一个通用脚本文件夹和几个特定的脚本文件夹在同一根,我需要调用特定的脚本文件夹(其中调用通用脚本与特定问题的参数)。 工作目录是这样的

\Nico\Scripts\Script1.ps1
             \Script2.ps1
      \Problem1\Solution1.ps1
               \ParameterForSolution1.config
      \Problem2\Solution2.ps1
               \ParameterForSolution2.config

solution1和solution2调用Scripts文件夹中的PS1,加载存储在ParameterForSolution中的参数。 所以在powershell ISE中我运行这个命令

.\Nico\Problem1\Solution1.PS1

以及Solution1中的代码。PS1是:

# This is the path where my script is running
$path = split-path -parent $MyInvocation.MyCommand.Definition

# Change to root dir
cd "$path\..\.."

$script = ".\Script\Script1.PS1"

$parametro = "Problem1\ParameterForSolution1.config"
# Another set of parameter Script1.PS1 can receive for debuggin porpuose
$parametro +=' -verbose'

Invoke-Expression "$script $parametro"

其他回答

一句话解决方案:

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

MyScript1的当前路径。ps1和myScript2.ps1不一样。可以得到MyScript2的文件夹路径。并将其连接到MyScript1。然后执行它。两个脚本必须位于相同的位置。

## MyScript2.ps1 ##
$ScriptPath = Split-Path $MyInvocation.InvocationName
& "$ScriptPath\MyScript1.ps1"

如何在脚本中运行PowerShell内置脚本?

如何使用内置脚本

Get-Location
pwd
ls
dir
split-path
::etc...

它们由您的计算机运行,自动检查脚本的路径。

类似地,我可以通过在脚本块中放入脚本名来运行自定义脚本

::sid.ps1 is a PS script I made to find the SID of any user
::it takes one argument, that argument would be the username
echo $(sid.ps1 jowers)


(returns something like)> S-X-X-XXXXXXXX-XXXXXXXXXX-XXX-XXXX


$(sid.ps1 jowers).Replace("S","X")

(returns same as above but with X instead of S)

继续到powershell命令行并键入

> $profile

这将返回每次打开应用程序时PowerShell命令行将执行的文件的路径。

就像这样

C:\Users\jowers\OneDrive\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

转到Documents,查看是否已经有WindowsPowerShell目录。我没有,所以

> cd \Users\jowers\Documents
> mkdir WindowsPowerShell
> cd WindowsPowerShell
> type file > Microsoft.PowerShellISE_profile.ps1

现在,我们已经创建了每次打开PowerShell应用程序时都会启动的脚本。

这样做的原因是,我们可以添加自己的文件夹,其中包含所有自定义脚本。让我们创建这个文件夹,我将其命名为“Bin”,以Mac/Linux保存脚本的目录命名。

> mkdir \Users\jowers\Bin

现在我们想要在每次打开应用程序时将该目录添加到$env:path变量中,因此返回到WindowsPowerShell目录并

> start Microsoft.PowerShellISE_profile.ps1

然后加上这个

$env:path += ";\Users\jowers\Bin"

现在,只要您将脚本保存在“Bin”目录中,shell就会自动找到您的命令。

重新启动powershell,它应该是第一个执行的脚本之一。

重新加载后在命令行上运行这个命令,在你的path变量中看到你的新目录:

> $env:Path

现在我们可以从命令行或从另一个脚本中调用脚本,简单如下:

$(customScript.ps1 arg1 arg2 ...)

如您所见,我们必须使用.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自动变量的更多信息

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

哪里会有争论

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”字符串