Powershell参数不正确

时间:2015-05-18 18:37:45

标签: powershell

所以我试图将文件目录传递给底部的函数convert。当我运行脚本时,我收到输出:

SUCCES

C:\ test 2 \ 00000027627-00001 \ PROCESSING CHECKLIST

转换:无效参数 - 2 \ 00000027627-00001 \ PROCESSING 在C:\ Users \ pmanca \ Desktop \ msbxmlreader.ps1:35 char:13 + convert([string] $ hostdirectoryPlusLoanPlusDocType) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 〜     + CategoryInfo:NotSpecified :(无效的参数... 0001 \ PROCESSING:String)[],RemoteException     + FullyQualifiedErrorId:NativeCommandError

在C:\ test之后,它似乎正在切断文件路径。文件路径可能太长了吗?即便如此,我也不会想象在函数调用时得到错误,而是在函数无法解析路径的某个地方。

#? is a shortcut for where object and % is a shortcut for foreach-object
cls
#count 1 = Loan Number
#count 4 = Cust Num
#count 5 = Document Type
$hostdirectory = "C:\test 2"
$count = 0
$file = (Select-xml -Path "$hostdirectory\index.xml" -XPath / ).Node


$test = $file.ExportedResult.Docs.ExportedDoc.Doc.UdiValues.UdiValue.Value#  | Select-Object  {$_.UdiValue.Value.InnerXML} -Unique #|? {$_.UdiValue.Name -eq "Loan Number"} 


 $test | ForEach-Object{
    $count++
   # Write-Host $_.innerxml "----" $count
    if($count -eq 1){
        [string]$xmlHold = $_.InnerXML
        $hostdirectoryPlusLoan =  "$hostdirectory\$xmlHold"
        if(!(test-path $hostdirectoryPlusLoan)){
            New-Item $hostdirectoryPlusLoan -ItemType directory 
        }
    }
    if($count -eq 5){
        [string]$xmlHold = $_.InnerXML
        $hostdirectoryPlusLoanPlusDocType =  "$hostdirectoryPlusLoan\$xmlHold"
        if(!(test-path $hostdirectoryPlusLoanPlusDocType)){
            New-Item $hostdirectoryPlusLoanPlusDocType -ItemType directory
        }
        if(Test-Path "$hostdirectory\$xmlhold.pdf"){
            $check = Copy-Item  "$hostdirectory\$xmlHold.pdf" -Destination $hostdirectoryPlusLoanPlusDocType -ErrorAction SilentlyContinue
            if(-not $?) {write-warning "Copy Failed"; Write-Host $Error[0].exception.message}
            else {write-host "Succes"} 
            Write-Host $hostdirectoryPlusLoanPlusDocType
            convert([string]$hostdirectoryPlusLoanPlusDocType)
        } 
    }

    if($count -ge 8){
        $count = 0
       # Write-Host "-----------------------------------"
    }


 }





 function convert([string]$inputDirectory){
    write-host $inputDirectory
    #Variable to hold current input directory 
    $InputPathFilter = $InputDirectory + '\*.pdf'



    #Variable to hold the list of PDFs from the current input directory
    $PDFList = @(gci $InputPathFilter | foreach {write-output $_.name})


    #Loop through list of PDF files to convert to TIF image files.
    for ($j=0; $j -lt $PDFList.count; $j++) {   
        #Each PDF will go into its own directory/batch 


        #Create a variable with only the file name
        $FileName = $PDFList[$j] -replace(".pdf",'')


        #Variable of the full path to the current PDF
        $InputPath = $InputDirectory + '\' + $PDFList[$j]


        #Variable to hold output path of each TIF file.  Filename format is that used by batches in ScerIS (i.e. 00010001.tif, 00010002.tif, etc...)
        $OutputFullDirectory = $inputlDirectory + '\' + $FileName + "_" + "{0:D4}" -f + '1' + '%04d.tif' 

        #Calls Ghostscript command line executable to process each PDF file into a group of single page TIF image files
        &'C:\Program Files\gs\gs9.14\bin\gswin64c.exe' -sDEVICE=tiffg4 -dBATCH -dNOPAUSE -q -r600 "-sOutputFile=$OutputFullDirectory" "$InputPath"

        #Increment the counter for the loop
        $DocCounter = $j + 1

        #Rename the current pdf so that it isn't processed again.
        $RenamePath = $PdfList[$j] -replace("pdf", "pd_")

           Rename-Item $InputPath $RenamePath -Force


    }


 }

1 个答案:

答案 0 :(得分:2)

首先:在PowerShell中,当您调用函数时,不得使用括号:

convert $hostdirectoryPlusLoanPlusDocType 

或在评论中建议

convert -inputDirectory $hostdirectoryPlusLoanPlusDocType 

但不是:

convert([string]$hostdirectoryPlusLoanPlusDocType)

第二:您的函数应首先声明并在以下后使用:

function toto ($var)
{
  Write-Host $var
}
toto "voila"

而不是

toto "voila"
function toto ($var)
{
  Write-Host $var
}