我想调用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"
这只是对答案的补充信息
为了将参数传递给另一个文件
哪里会有争论
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”字符串
我提出我的例子供考虑。这就是我如何在我制作的工具中调用控制器脚本中的一些代码。执行此工作的脚本也需要接受参数,因此本示例将展示如何传递参数。它假设被调用的脚本与控制器脚本(进行调用的脚本)在同一个目录中。
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string[]]
$Computername,
[Parameter(Mandatory = $true)]
[DateTime]
$StartTime,
[Parameter(Mandatory = $true)]
[DateTime]
$EndTime
)
$ZAEventLogDataSplat = @{
"Computername" = $Computername
"StartTime" = $StartTime
"EndTime" = $EndTime
}
& "$PSScriptRoot\Get-ZAEventLogData.ps1" @ZAEventLogDataSplat
上面是一个接受3个参数的控制器脚本。这些在参数块中定义。然后控制器脚本调用名为Get-ZAEventLogData.ps1的脚本。举例来说,这个脚本也接受相同的3个参数。当控制器脚本调用执行该工作的脚本时,它需要调用该脚本并传递参数。上面展示了我如何通过飞溅来做到这一点。