Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
731 views
in Technique[技术] by (71.8m points)

function - Powershell not returning correct value

As some background, this should take an excel file, and convert it to PDF (and place the PDF into a temporary folder).

E.g. 'C:UsersgjacobsDesktopeststock.xlsx' becomes 'C:UsersgjacobsDesktopestpdf_merge_tmpstock.pdf'

However, the new file path does not return correctly.

If I echo the string $export_name from within the function, I can see that it has the correct value: "C:UsersgjacobsDesktopestpdf_merge_tmpstock.pdf".

But once $export_name is returned, it has a different (incorrect value): "C:UsersgjacobsDesktopestpdf_merge_tmp C:UsersgjacobsDesktopestpdf_merge_tmpstock.pdf".

function excel_topdf{

    param(
        $file
    )
    
    #Get the parent path
    $parent = Split-Path -Path $file

    #Get the filename (no ext)
    $leaf = (Get-Item $file).Basename

    #Add them together.
    $export_name = $parent + "pdf_merge_tmp" + $leaf + ".pdf"

    echo ($export_name) #prints without issue.

    #Create tmp dir
    New-Item -Path $parent -Name "pdf_merge_tmp" -ItemType "Directory" -Force

    $objExcel = New-Object -ComObject excel.application
    $objExcel.visible = $false

    $workbook = $objExcel.workbooks.open($file, 3)
    $workbook.Saved = $true

    $xlFixedFormat = “Microsoft.Office.Interop.Excel.xlFixedFormatType” -as [type]
    $workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $export_name)
    $objExcel.Workbooks.close()
    
    $objExcel.Quit()

    return $export_name
}

$a = excel_topdf -file 'C:UsersgjacobsDesktopeststock.xlsx'
echo ($a)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The issue you're experiencing is caused by the way how PowerShell returns from functions. It's not something limited to New-Item cmdlet. Every cmdlet which returns anything would cause function output being altered with the value from that cmdlet.

As an example, let's take function with one cmdlet, which returns an object:

function a {
 Get-Item -Path .
}

$outputA = a
$outputA

#### RESULT ####

    Directory:


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d--hs-       12/01/2021     10:47                C:

If you want to avoid that, these are most popular options (as pointed out by Lasse V. Karlsen in comments):

# Assignment to $null (or any other variable)
$null =  Get-Item -Path .

# Piping to Out-Null
Get-Item -Path . | Out-Null

NOTE: The behavior described above doesn't apply to Write-Host:

function b {
 Write-Host "bbbbbb"
}

$outputB = b
$outputB

# Nothing displayed

Interesting thread to check if you want to learn more.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...