开发者

How can I use a powershell-function in multi-script?

开发者 https://www.devze.com 2023-01-26 01:27 出处:网络
My C#-program should be by opening perform a powershellfunction, looks like this: function RunIE { $a = Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion -name ProgramFilesDir

My C#-program should be by opening perform a powershellfunction, looks like this:

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") 

The log at the end will written in my logfile. (So I think this works.)

During the C#-program runs an other script should be conducted, like this:

    ...    

    $ie = RunIE  
    #here I get a mistake: "The term 'RunIE' is not recognized as the name of a 
    #cmdlet, fun开发者_StackOverflow社区ction, 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

    ...

How can I use my function in other scripts, without new/second invocation? (I think the solution must create in powershell.)

thanks.


You need to dot source the script that contain the functions and script-scope variables you want to use in your script. For instance, if IEUtils.ps1 contained your RunIE function then you would dot source it into primary script like so:

. C:\Temp\IEUtils.ps1
$ie = RunIE

This assumes that IEUtils.ps1 in C:\Temp. Subtitute the appropriate dir as required. If it is in the same dir as your primary script then use:

$ScriptPath = $MyInvocation.MyCommand.Path
$ScriptDir  = Split-Path -parent $ScriptPath
. $ScriptDir\IEUtils.ps1
0

精彩评论

暂无评论...
验证码 换一张
取 消