Key not valid for use in specified state - A positional parameter cannot be found that accepts argument '+'

308 Views Asked by At

I am trying to understand what this error actually means. I am new to PowerShell and cannot figure this one out. I have searched for similar questions but the content differs to my requirement.

In a nut shell the script is queering a data historian system for a batch/lot number and the start time of that batch. This script will run every minute using task scheduler. This has not been set up yet as I am still in the testing phase. I have set up a service account is order for the script to run. The details of which are stored in a cred file. The script creates a folder using this batch/lot number. The script creates a log file with the batch number and the start date and time of the batch. Then the script searches a source folder on the server when a file is uploaded from the factory floor into the source folder the script moves the file into the already created folder with the correct batch number. If files that are outside of the batch start and end time then the files are moved to no batch folder where they will be reviewed manually.

I have done tests whereby I manually added files to the source folder on the server and everything worked and did not get the "a positional parameter cannot be found that accepts argument "+" from the script.

I am looking into the server configuration and permission levels but to my knowledge, nothing has changed. I cannot see what is wrong with the script but hopefully, someone can give me some pointers.

Error Code below

`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`

Script is below Thanks for your help

`# 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`
0

There are 0 best solutions below