繁体   English   中英

Powershell参数错误

[英]Powershell incorrect parameter

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

更迭

C:\\ test 2 \\ 00000027627-00001 \\处理清单

convert:无效参数-2 \\ 00000027627-00001 \\ C:\\ Users \\ pmanca \\ Desktop \\ msbxmlreader.ps1:35 char:13 + convert([string] $ hostdirectoryPlusLoanPlusDocType)+ ~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + CategoryInfo:未指定:(无效的参数.. .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


    }


 }

首先 :在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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM