我的 C# 程序应该通过打开执行 powershell 函数,如下所示:
function RunIE
{
$a = Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion -name ProgramFilesDir
$path = $a.ProgramFilesDir + "\Internet Explorer\iexplore.exe"
& $path "www.heise.de" -extoff
Start-Sleep 6
$shell = (New-Object -COM Shell.Application)
$ie = @($shell.Application.Windows()) | Where-Object { $_.LocationUrl -like "*heise*" }
Write-Output $ie
}
$ScriptLog.Invoke([log4net.Core.Level]::Debug, "Funktionsdurchlauf durchgeführt")
最后的日志将写入我的日志文件。 (所以我认为这可行。)
在 C# 程序运行期间,应该执行另一个脚本,如下所示:
...
$ie = RunIE
#here I get a mistake: "The term 'RunIE' is not recognized as the name of a
#cmdlet, function, script file, or operable program. Check the spelling of the
#name, or if a path was included, verify that the path is correct and try again."
$ie.Navigate2($path)
Start-Sleep 5
...
如何在没有新的/第二次调用的情况下在其他脚本中使用我的函数? (我认为解决方案必须在 powershell 中创建。)
谢谢。
最佳答案
您需要对包含要在脚本中使用的函数和脚本范围变量的脚本进行点源。例如,如果 IEUtils.ps1 包含您的 RunIE 函数,那么您可以像这样将其点源到主脚本中:
. C:\Temp\IEUtils.ps1
$ie = RunIE
这假定 C:\Temp 中的 IEUtils.ps1。根据需要替换适当的目录。如果它与您的主脚本位于同一目录中,则使用:
$ScriptPath = $MyInvocation.MyCommand.Path
$ScriptDir = Split-Path -parent $ScriptPath
. $ScriptDir\IEUtils.ps1
关于windows - 如何在多脚本中使用 powershell 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4203804/