powershell PowerShell函数运行JavaScript / JQuery并将结果返回PS,超时

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell PowerShell函数运行JavaScript / JQuery并将结果返回PS,超时相关的知识,希望对你有一定的参考价值。

# Finds all empty/blank files and directories

function IsAllZeros($file) {
    $bufferSize = [Math]::Pow(2, 16)
    $stream = [System.IO.File]::OpenRead($file)
    while ($stream.Position -lt $stream.Length) {
        $buffer = new-object Byte[] $bufferSize
        $bytesRead = $stream.Read($buffer, 0, $bufferSize)
        for ($c = 0; $c -lt $bytesRead; $c++) {
            if ($buffer[$c] -ne 0) {
                $stream.Close()
                return $false
            }
        }
    }
    $stream.Close()
    return $true
}

dir -Recurse | % {
     if ($_.PSIsContainer) {
         if ($_.GetFileSystemInfos().Count -eq 0) {
             Write-Output "Empty dir:`t`t$($_.FullName)"
         }
     }
     else {
         if ($_.Length -eq 0 -or (IsAllZeros $_.FullName)) {
             Write-Output "Empty file:`t$($_.Length)`t$($_.FullName)"
         }
     }
 }
 
# PowerShell function to run JavaScript/JQuery and return results back to PS, with timeout

# some web page with jQuery in it
$url = "http://jquery.com/"

Function ResetTimer
{
    $script:startTime = [DateTime]::Now
}

Function IsTimeout([TimeSpan]$timeout)
{
    return ([DateTime]::Now - $startTime) -ge $timeout
}

Function WaitForIE
{
    $ie = $script:ie
    Write-Debug "Waiting..."
    ResetTimer
    do {
        if (IsTimeout ([TimeSpan]::FromSeconds(30))) {
            Write-Error "IE response timed out."; ExitFailure
        }
        Start-Sleep -m 100
    }
    until ( $ie.ReadyState -eq 4 -and
            $ie.Document.readyState -eq 'complete')
}

# Use this function to run JavaScript on a web page. Your $jsCommand can
# return a value which will be returned by this function unless $global
# switch is specified in which case $jsCommand will be executed in global
# scope and cannot return a value. If you received error 80020101 it means
# you need to fix your JavaScript code.
Function ExecJavaScript($ie, $jsCommand, [switch]$global)
{
    if (!$global) {
        $jsCommand = "document.body.setAttribute('PSResult', (function(){$jsCommand})());"
    }
    WaitForIE
    $document = $ie.document
    $window = $document.parentWindow
    $window.execScript($jsCommand, 'javascript') | Out-Null
    if (!$global) {
        return $document.body.getAttribute('PSResult')
    }
}

Function CheckJQueryExists
{
    $result = ExecJavaScript $ie 'return window.hasOwnProperty("$");'
    return ($result -eq $true)
}

$ie = New-Object -COM InternetExplorer.Application -Property @{
    Navigate = $url
    Visible = $true
}
do { Start-Sleep -m 100 } while ( $ie.ReadyState -ne 4 )

$jQueryExists = CheckJQueryExists $ie
echo "jQuery exists? $jQueryExists"

# make a jQuery call
ExecJavaScript $ie @'
    // this is JS code, remember to use semicolons
    var content = $('#home-content');
    return content.text();
'@

# Quit and dispose IE COM
$ie.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ie) | out-null
Remove-Variable ie

以上是关于powershell PowerShell函数运行JavaScript / JQuery并将结果返回PS,超时的主要内容,如果未能解决你的问题,请参考以下文章

运行从 PowerShell 显式获取参数的 Python 函数(无需单独传递参数)

PowerShell 该术语未被识别为 cmdlet 函数脚本文件或可运行程序

Powershell 无法将“mysql”项识别为 cmdlet函数脚本文件或可运行程序的名称

PowerShell 学习笔记——运行命令

powershell脚本隐藏退出码

从 Web API 连接并运行 PowerShell 命令