1

我试图了解此错误的实际含义。我是 PowerShell 的新手,无法弄清楚这一点。我搜索了类似的问题,但内容与我的要求不同。

简而言之,该脚本正在查询数据历史系统以获取批次/批号和该批次的开始时间。该脚本将使用任务调度程序每分钟运行一次。这尚未设置,因为我仍处于测试阶段。我已经设置了一个服务帐户,以便脚本运行。其详细信息存储在 cred 文件中。该脚本使用此批次/批号创建一个文件夹。该脚本会创建一个包含批次编号以及批次开始日期和时间的日志文件。然后,当文件从工厂上传到源文件夹时,脚本会搜索服务器上的源文件夹,脚本会将文件移动到已创建的具有正确批号的文件夹中。如果文件在批处理开始和结束时间之外,则文件将被移动到非批处理文件夹中,将在其中手动查看它们。

我已经完成了测试,我手动将文件添加到服务器上的源文件夹并且一切正常,并且没有得到“找不到从脚本接受参数“+”的位置参数。

我正在研究服务器配置和权限级别,但据我所知,没有任何改变。我看不出脚本有什么问题,但希望有人可以给我一些指示。

下面的错误代码

`PS C:\Users\a-graydx2> E:\Kistler Script\Batch ID with log 2021-11-29.ps1
An error occurred:
Key not valid for use in specified state.

Add-Content : A positional parameter cannot be found that accepts argument '+'.
At E:\Kistler Script\Batch ID with log 2021-11-29.ps1:186 char:11
    +           Add-Content -Path $ErrorFileName -Value (Get-Date -Format " ...
    +           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Add-Content], ParameterBindingException
+ FullyQualifiedErrorId :         
PositionalParameterNotFound,Microsoft.PowerShell.Commands.AddContentCommand

An error occurred:
Key not valid for use in specified state.

Add-Content : A positional parameter cannot be found that accepts argument '+'.
At E:\Kistler Script\Batch ID with log 2021-11-29.ps1:186 char:11
+           Add-Content -Path $ErrorFileName -Value (Get-Date -Format " ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Add-Content], ParameterBindingException
    + FullyQualifiedErrorId :     
PositionalParameterNotFound,Microsoft.PowerShell.Commands.AddContentCommand`

脚本如下感谢您的帮助

`# Declare global variables

$fmSourcePath = "E:\Kistler\CoMo Services\Data\5336_L1.4345277\"
$shSourcePath = "E:\Kistler\CoMo Services\Data\5338_L1.5338_L1\"

$fmDesinationPath = "E:\Kistler XML Files\FM\"
$shDesinationPath = "E:\Kistler XML Files\SH\"

$fmWebAPI = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$shWebAPI = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

# the path to stored credential
$credPath = "E:\Kistler Script\Cred.xml"
$logFileName = "BatchLog.txt"

#Path to the error log file
$ErrorFileName = "E:\Kistler Script\errorlog.txt"

function Move_Kistler_Files {
    param (
        [string]$url,
        [string]$SourcePath,
        [string]$DestinationPath
    )

try {

    # check for stored credential
    if ( Test-Path $credPath ) {
        #crendetial is stored, load it 
        $cred = Import-CliXml -Path $credPath
    } else {
        # no stored credential then: create store, get credential and save it
        $parent = split-path $credpath -parent
        if ( -not ( test-Path $parent ) ) {
            New-Item -ItemType Directory -Force -Path $parent
        }
        $cred = get-credential
        $cred | Export-CliXml -Path $credPath
    }

    # Get the current batch id using the Web-API call 
    $result = Invoke-RestMethod -Uri $url -Credential $Cred
    $BatchID = $result.Value

    $BatchFolder = $DestinationPath + $BatchID

    Write-Host $BatchFolder

    # Create a new folder in the destination path based on the Batch ID
    If(!(test-path $BatchFolder))
    {
          New-Item -ItemType Directory -Force -Path $BatchFolder | Out-Null

          # Add the current date/time to the log file
          $LogFile = $DestinationPath + $logFileName

          # if file exist Update the last record with the batch end date
          If((test-path $LogFile)){
                $txt = get-content $LogFile

                $txt[$txt.length - 1 ] = $txt[$txt.length - 1 ] + ", " + (Get-Date)
                $txt | set-content $LogFile
          }else{
                #add a header row in the file
                Add-Content -Path $LogFile -Value "BatchID, StartDate, EndDate"
          }
          # create a new record in the log file with current Batch Id and date as start of     
batch indicator
          $msg = $BatchID + ", " + (Get-Date)
          Add-Content -Path $LogFile -Value $msg 
    }

    ##############################################################################
    # Copy the Kistler XML files from the source to the destination
    ##############################################################################


    # get al the Kistler XML files in the source folder
    $Files = get-childitem -path $SourcePath -Recurse | Where-Object {$_.Extension -eq ".XML"} 
| Sort-Object LastWriteTime -Descending
   
    # If we have files to process do it
    if ($Files.Length -gt 0) {

        # read back the batch start and end dates from the log table
        $LogFile = $DestinationPath + $logFileName
        $txt = get-content $LogFile

        # Get the latest Batch Id and it's start date
        $FileMoveCount = 0
        $FileNotMoveCount = 0
        $ptr = 1
        $batchArray =$txt[$txt.length - $ptr ].Split(",")
        $MoveToPath = $DestinationPath + $batchArray[0]
        $batchStartDate = $batchArray[1]

        #Process each XML file
        Foreach ($File in $Files ) {

            $FileTime = $File.LastWriteTime
            #write-host $File.FullName $File.Name $FileTime $MoveToPath $batchStartDate


            #if the XML file's date-time is older than the batch start time, skip to the     
previus Batch Id and start time
            while ( ([DateTime]$FileTime -lt [DateTime]$batchStartDate) -and ($ptr -lt 
($txt.length)-1) ) {


                #Write a log for the number of files copied
                if ($FileMoveCount -gt 0){
                    Add-Content -Path $ErrorFileName -Value ((Get-Date -Format "dd/MM/yyyy 
HH:mm") + ": " + $FileMoveCount + " XML files moved to " + $MoveToPath) 
                    $FileMoveCount = 0
                }

                $ptr++
                $batchArray =$txt[$txt.length - $ptr ].Split(",")

                $MoveToPath = $DestinationPath + $batchArray[0]
                $batchStartDate = $batchArray[1]

                #write-host $MoveToPath $batchStartDate
            }

            #Copy the XML file to the destination folder
            if ([DateTime]$FileTime -ge [DateTime]$batchStartDate){
                Move-Item $File.FullName -Destination ($MoveToPath + "\" + $File.Name)
                $FileMoveCount++
            }else{
                Move-Item $File.FullName -Destination ($DestinationPath + "\NoBatch\" + 
$File.Name)
                $FileNotMoveCount++
            }
        }

      #Write a log for the number of files copied
      if ($FileMoveCount -gt 0){
        Add-Content -Path $ErrorFileName -Value ((Get-Date -Format "dd/MM/yyyy HH:mm") + ": " 
+ $FileMoveCount + " XML files moved to " + $MoveToPath) 
      }

      if ($FileNotMoveCount -gt 0){
        Add-Content -Path $ErrorFileName -Value ((Get-Date -Format "dd/MM/yyyy HH:mm") + ": 
Could not find batch ID for " + $FileNotMoveCount + " XML files " )
      }
    }

}catch{

      #Write the error 
      Write-Host "An error occurred:" -ForegroundColor red
      Write-Host $_  -ForegroundColor red

      Add-Content -Path $ErrorFileName -Value (Get-Date -Format "dd/MM/yyyy HH:mm") + ": " + 
$_ 
    }  
}

### Process the FM Kistler files 
Move_Kistler_Files $fmWebAPI $fmSourcePath $fmDesinationPath

### Process the SH Kistler files 
Move_Kistler_Files $shWebAPI $shSourcePath $shDesinationPath`
4

0 回答 0