windows - 如何在多脚本中使用 powershell 函数?

标签 windows internet-explorer function powershell

我的 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/

相关文章:

css - ie8 奇怪地显示社交媒体图标

javascript - 在 IE 浏览器中,我的 Web 应用程序(在 HTTPS 上运行)未检测到带 HTTP 的网址

function - Julia 中有类似 python __dict__ 的东西吗?

linux - 将 linux 创建的 vm 移植到 windows

c++ - 为什么 COM 在新线程中不起作用?

node.js - 错误 : Cannot find module 'dateformat' in gulp

c - 如何从 Windows .txt 文件中读取回车符(即 '\r' )

css - Internet Explorer 7 8 和 9 在单个 4 图标 png 文件中复制第一个背景图标

c - 如何在循环中使用 scanf 读取数据直到 EOL?

python - 使用 If 条件时 Python 中的 NoneType 错误