Powershell,使用outvariable保存Web图像

时间:2014-11-08 16:44:33

标签: image powershell file-io

我想将Web图像捕获到变量中进行处理(见下文)。我想避免将其保存到文件,处理文件,然后删除文件。我可以将web请求输出直接保存到文件中,如图所示,但是如果我将输出保存到变量然后将变量内容保存到文件中,我就没有相同的结果。

    # saving output to a file works
        $image='https://www.google.com/images/srpr/logo11w.png'
        Invoke-WebRequest $image -outfile image1.png

    <# I want to skip creating a file, using it, then deleting it
    Save the image into a variable, convert to base64 to store in db, retrieve from db, convert back, save to a file
    #>
        Invoke-WebRequest $image -OutVariable imagevariable
        $imagevariable | out-file image2.png   # this is not a valid image

    # Convert to base64 (using PSCX add-on) to store string
        $toBase64=ConvertTo-Base64($imagevariable)

    # save to, retrieve from db
        #Invoke-Sqlcmd insert...
        #Invoke-Sqlcmd select...

    # Convert from base64 (using PSCX add-on)  & output to a file
        $fromBase64 | ConvertFrom-Base64 -OutputPath image3.png

为方便起见,我将使用PSCX community extensions进行base64转换。

1 个答案:

答案 0 :(得分:2)

你真的很接近但是图像字节在返回的对象的Content属性中,试试这个:

$toBase64 = $imagevariable.Content | ConvertTo-Base64