Powershell 如何将该值获取到变量中以供以后操作?

Powershell 如何将该值获取到变量中以供以后操作?,powershell,Powershell,我在网上找到了这段代码,一切正常,但我不知道如何获取执行中给出的输出值,这个脚本用于获取图像的文本,它确实给了我文本,但显示了类似这样的内容“图像的文本是…”,我不知道是图像中的所有文本还是缺少了一些内容 我尝试过使用| Out file“path of file”,但它显示的文本与上面提到的相同:/ 谢谢你的帮助 using namespace Windows.Storage using namespace Windows.Graphics.Imaging <# .Synopsis

我在网上找到了这段代码,一切正常,但我不知道如何获取执行中给出的输出值,这个脚本用于获取图像的文本,它确实给了我文本,但显示了类似这样的内容“图像的文本是…”,我不知道是图像中的所有文本还是缺少了一些内容

我尝试过使用| Out file“path of file”,但它显示的文本与上面提到的相同:/

谢谢你的帮助

using namespace Windows.Storage
using namespace Windows.Graphics.Imaging

<#
.Synopsis
   Runs Windows 10 OCR on an image.
.DESCRIPTION
   Takes a path to an image file, with some text on it.
   Runs Windows 10 OCR against the image.
   Returns an [OcrResult], hopefully with a .Text property containing the text
.EXAMPLE
   $result = .\Get-Win10OcrTextFromImage.ps1 -Path 'c:\test.bmp'
   $result.Text
#>
[CmdletBinding()]
Param
(
    # Path to an image file
    [Parameter(Mandatory=$true, 
                ValueFromPipeline=$true,
                ValueFromPipelineByPropertyName=$true, 
                Position=0,
                HelpMessage='Path to an image file, to run OCR on')]
    [ValidateNotNullOrEmpty()]
    $Path
)

Begin {
    # Add the WinRT assembly, and load the appropriate WinRT types
    Add-Type -AssemblyName System.Runtime.WindowsRuntime

    $null = [Windows.Storage.StorageFile,                Windows.Storage,         ContentType = WindowsRuntime]
    $null = [Windows.Media.Ocr.OcrEngine,                Windows.Foundation,      ContentType = WindowsRuntime]
    $null = [Windows.Foundation.IAsyncOperation`1,       Windows.Foundation,      ContentType = WindowsRuntime]
    $null = [Windows.Graphics.Imaging.SoftwareBitmap,    Windows.Foundation,      ContentType = WindowsRuntime]
    $null = [Windows.Storage.Streams.RandomAccessStream, Windows.Storage.Streams, ContentType = WindowsRuntime]
    
    
    # [Windows.Media.Ocr.OcrEngine]::AvailableRecognizerLanguages
    $ocrEngine = [Windows.Media.Ocr.OcrEngine]::TryCreateFromUserProfileLanguages()
    

    # PowerShell doesn't have built-in support for Async operations, 
    # but all the WinRT methods are Async.
    # This function wraps a way to call those methods, and wait for their results.
    $getAwaiterBaseMethod = [WindowsRuntimeSystemExtensions].GetMember('GetAwaiter').
                                Where({
                                        $PSItem.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1'
                                    }, 'First')[0]

    Function Await {
        param($AsyncTask, $ResultType)

        $getAwaiterBaseMethod.
            MakeGenericMethod($ResultType).
            Invoke($null, @($AsyncTask)).
            GetResult()
    }
}

Process
{
    foreach ($p in $Path)
    {
      
        # From MSDN, the necessary steps to load an image are:
        # Call the OpenAsync method of the StorageFile object to get a random access stream containing the image data.
        # Call the static method BitmapDecoder.CreateAsync to get an instance of the BitmapDecoder class for the specified stream. 
        # Call GetSoftwareBitmapAsync to get a SoftwareBitmap object containing the image.
        #
        # https://docs.microsoft.com/en-us/windows/uwp/audio-video-camera/imaging#save-a-softwarebitmap-to-a-file-with-bitmapencoder

        # .Net method needs a full path, or at least might not have the same relative path root as PowerShell
        $p = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($p)
        
        $params = @{ 
            AsyncTask  = [StorageFile]::GetFileFromPathAsync($p)
            ResultType = [StorageFile]
        }
        $storageFile = Await @params


        $params = @{ 
            AsyncTask  = $storageFile.OpenAsync([FileAccessMode]::Read)
            ResultType = [Streams.IRandomAccessStream]
        }
        $fileStream = Await @params


        $params = @{
            AsyncTask  = [BitmapDecoder]::CreateAsync($fileStream)
            ResultType = [BitmapDecoder]
        }
        $bitmapDecoder = Await @params


        $params = @{ 
            AsyncTask = $bitmapDecoder.GetSoftwareBitmapAsync()
            ResultType = [SoftwareBitmap]
        }
        $softwareBitmap = Await @params

        # Run the OCR
        Await $ocrEngine.RecognizeAsync($softwareBitmap) ([Windows.Media.Ocr.OcrResult])

    }
}
使用命名空间Windows.Storage
使用命名空间Windows.Graphics.Imaging
[CmdletBinding()]
Param
(
#图像文件的路径
[参数(必需=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
位置=0,
HelpMessage='要在其上运行OCR的图像文件的路径'']
[ValidateNotNullOrEmpty()]
$Path
)
开始{
#添加WinRT程序集,并加载适当的WinRT类型
添加类型-AssemblyName System.Runtime.WindowsRuntime
$null=[Windows.Storage.StorageFile,Windows.Storage,ContentType=WindowsRuntime]
$NULL= [ Windows .Meal.Or.OrgEngress,Windows .Fund,CordnType = WINDOWSRunTime]
$NULL= [ Windows .Fask.IasycCurrase' 1,Windows .Frime,CordnType = WINDOWSRunTime]
$NULL= [WINDOWSGRICICS.IGING.FieldBITMAP,WINDOWS .BASIC,CordnType = WINDOWSRunTime]
$null=[Windows.Storage.Streams.RandomAccessStream,Windows.Storage.Streams,ContentType=WindowsRuntime]
#[Windows.Media.Ocr.OcrEngine]::可用的认知语言
$ocrEngine=[Windows.Media.Ocr.ocrEngine]::TryCreateFromUserProfileLanguages()
#PowerShell没有对异步操作的内置支持,
#但是所有WinRT方法都是异步的。
#此函数包装了一种调用这些方法并等待其结果的方法。
$GetAwaiterAssemethod=[WindowsRuntimeSystemExtensions].GetMember('GetAwaiter')。
在哪里({
$PSItem.GetParameters()[0]。ParameterType.Name-eq'IAsyncOperation`1'
},第一个“)[0]
函数等待{
参数($AsyncTask,$ResultType)
$getAwaiterBaseMethod。
MakeGenericMethod($ResultType)。
调用($null,@($AsyncTask))。
GetResult()
}
}
过程
{
foreach($Path中的p)
{
#从MSDN加载映像的必要步骤包括:
#调用StorageFile对象的OpenAsync方法以获取包含图像数据的随机访问流。
#调用静态方法BitmapDecoder.CreateAsync以获取指定流的BitmapDecoder类的实例。
#调用GetSoftwareBitmapAsync以获取包含图像的SoftwareBitmap对象。
#
# https://docs.microsoft.com/en-us/windows/uwp/audio-video-camera/imaging#save-a-softwarebitmap-to-a-file-with-bitmapencoder
#.Net方法需要完整路径,或者至少可能没有与PowerShell相同的相对路径根
$p=$ExecutionContext.SessionState.Path.GetUnsolvedProviderPathFromPSPath($p)
$params=@{
AsyncTask=[StorageFile]::GetFileFromPathAsync($p)
结果类型=[StorageFile]
}
$storageFile=wait@params
$params=@{
AsyncTask=$storageFile.OpenAsync([FileAccessMode]::Read)
结果类型=[Streams.irandomaccesstream]
}
$fileStream=wait@params
$params=@{
AsyncTask=[BitmapDecoder]::CreateAsync($fileStream)
结果类型=[BitmapDecoder]
}
$bitmapDecoder=wait@params
$params=@{
AsyncTask=$bitmapDecoder.GetSoftwareBitmapAsync()
结果类型=[软件位图]
}
$softwareBitmap=wait@params
#运行OCR
等待$ocrEngine.RecognizeAsync($softwareBitmap)([Windows.Media.Ocr.OcrResult])
}
}
更改此行

Await $ocrEngine.RecognizeAsync($softwareBitmap) ([Windows.Media.Ocr.OcrResult])


您将得到一个对象,其成员包含文件名、所有文本、所有行和所有单词

请尝试调试。在脚本开始处设置调试点,并逐行遍历脚本。我知道该值的设置位置,但不知道如何在以下位置获取:函数wait{param($AsyncTask,$ResultType)$GetWaiterBaseMethod.MakeGenericMethod($ResultType)。Invoke($null,@($AsyncTask)).GetResult()}或此处等待$ocrEngine.RecognizeAsync($softwareBitmap)([Windows.Media.Ocr.OcrResult])管道到
格式列表
,以避免(隐式)
格式表
(表格)输出时可能发生的列值截断。要保存到文件而不截断(表格格式),请将足够宽的
-Width
值传递到
Out file
。我不知道如何将答案标记为正确,但就是这样!非常感谢。
$result = Await $ocrEngine.RecognizeAsync($softwareBitmap) ([Windows.Media.Ocr.OcrResult])
[PSCustomObject]@{
    File  = $Path
    Lines = $result.lines.text
    Words = $result.lines.words.text
    Text  = $result | select -ExpandProperty text
}