Share via


A positional parameter cannot be found that accepts argument ' '.

Question

Thursday, August 30, 2012 1:26 PM | 1 vote

I'm getting this error that I have been trying to resolve.  Need some help please.

PS H:\ C:\Errors-chk_exch4.ps1
A positional parameter cannot be found that accepts argument ' '.

At C:\Errors-chk_exch4.ps1:1 char:17

+     Get-WinEvent <<<<  -computername $server -filterhashtable $filter_system ` | where-object { ($_.LevelDisplayName -ne "Information") -and (($_.Id -ne 1111) -and ($_.ProviderName -ne "Microsoft-Windows-Term
inalServices-Printers")) -and (($_.Id -ne 6037) -and ($_.ProviderName -ne "LsaSrv")) }  ` | Select-Object LogName, MachineName, timeCreated, LevelDisplayName, ID, ProviderName, Message `
    + CategoryInfo          : InvalidArgument: (:) [Get-WinEvent], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetWinEventCommand

All replies (8)

Thursday, August 30, 2012 9:53 PM ✅Answered | 1 vote

You seem to be supplying both mandatory parameters, the first one coming from the pipeline.

I assume line 175 is the one referencing Get-WinEvent. Funny how it was originally at line 1 in your initial post.

This means that there is a problem in that particular line. If you look closely, you will see a stray backtick character:

    Get-WinEvent -computername $server -filterhashtable $filter_system ` | where-object ... etc.

You should either remove the backtick, or insert a newline immediately after it.

Alternately, having the pipe character "|" as the last one on a line effectively continues the statement to the next line. So instead of this sequence: [first line][backtick][newline][pipecharacter][continued line] you could use [first line][pipecharacter][newline][continued line], i.e.:

    Get-WinEvent -computername $server -filterhashtable $filter_system |
       where-object ... etc.

Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.


Friday, August 31, 2012 4:05 PM ✅Answered

Yes its 175.  Originally it was line 1 since I had not posted the entire script since it was lengthy.

Thanks! Apparently, there were a couple of back tickies and formatting that caused it to error.

works now!

Here is the updated section.

 Get-WinEvent -computername $server -filterhashtable $filter_application |
    where-object { ($_.LevelDisplayName -ne "Information") -and ($_.ProviderName -ne "VMUpgradeHelper") -and (($_.ID -ne 7027) -and ($_.ProviderName -ne "Microsoft Forefront Protection")) `
    -and (($_.ID -ne 4113) -and ($_.ProviderName -ne "MSExchangeRepl")) }  `
    | Select-Object LogName, MachineName, timeCreated, LevelDisplayName, ID, ProviderName, Message `
    | Sort-Object Logname, machineName, timeCreated `
    | Export-Csva $path -append -notypeinformation

#  you can add other events to ignore in these
     
    Get-WinEvent -computername $server -filterhashtable $filter_system |
    where-object { ($_.LevelDisplayName -ne "Information") -and (($_.Id -ne 1111) -and ($_.ProviderName -ne "Microsoft-Windows-TerminalServices-Printers")) -and (($_.Id -ne 6037) -and ($_.ProviderName -ne "LsaSrv")) }  | Select-Object LogName, MachineName, timeCreated, LevelDisplayName, ID, ProviderName, Message `
    | Sort-Object Logname, machineName, timeCreated `
    | Export-Csva $path -append -notypeinformation

Thanks. Tony


Thursday, August 30, 2012 1:29 PM

It is difficult to identify the problem unless you post the script.

Grant Ward, a.k.a. Bigteddy


Thursday, August 30, 2012 4:04 PM

Okay here is the script.

$s = "VEXCASRV02", "VEXCASRV01"
$scan = "\VEXCASRV02\c$"   
  
function Export-CSVa
{
  # Requires -Version 2.0
  # This Export-CSV behaves exactly like native Export-CSV
  # However it has one optional switch -Append
  # Which lets you append new data to existing CSV file: e.g.
  # Get-Process | Select ProcessName, CPU | Export-CSVa processes.csv -Append
  # For details, see
  # http://dmitrysotnikov.wordpress.com/2010/01/19/export-csv-append/
  # (c) Dmitry Sotnikov 
  #
  [CmdletBinding(DefaultParameterSetName='Delimiter', SupportsShouldProcess=$true, ConfirmImpact='Medium')]
  param(
    [Parameter(Mandatory=$true, ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
    [System.Management.Automation.PSObject]
    ${InputObject},
    [Parameter(Mandatory=$true, Position=0)]
  [Alias('PSPath')]
  [System.String]
  ${Path},
 
  #region -Append (added by Dmitry Sotnikov)
  [Switch]
  ${Append},
  #endregion
 
  [Switch]
  ${Force},
 
  [Switch]
  ${NoClobber},
 
  [ValidateSet('Unicode','UTF7','UTF8','ASCII','UTF32','BigEndianUnicode','Default','OEM')]
  [System.String]
  ${Encoding},
 
  [Parameter(ParameterSetName='Delimiter', Position=1)]
  [ValidateNotNull()]
  [System.Char]
  ${Delimiter},
 
  [Parameter(ParameterSetName='UseCulture')]
  [Switch]
  ${UseCulture},
 
  [Alias('NTI')]
  [Switch]
  ${NoTypeInformation})

  begin
  {
    # This variable will tell us whether we actually need to append
    # to existing file
    $AppendMode = $false
 
    try
    {
      $outBuffer = $null
      if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
      { $PSBoundParameters['OutBuffer'] = 1 }
      $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Export-Csv',[System.Management.Automation.CommandTypes]::Cmdlet)
   
      #String variable to become the target command line
      $scriptCmdPipeline = ''
 
      # Add new parameter handling
      #region Dmitry: Process and remove the Append parameter if it is present
      if ($Append)
      {
        $PSBoundParameters.Remove('Append') | Out-Null
 
        if ($Path)
        {
          if (Test-Path $Path) # Need to construct new command line
          {       
            $AppendMode = $true
 
            if ($Encoding.Length -eq 0) # ASCII is default encoding for Export-CSV
           { $Encoding = 'ASCII' }
 
           # For Append we use ConvertTo-CSV instead of Export
           $scriptCmdPipeline += 'ConvertTo-Csv -NoTypeInformation '
 
           # Inherit other CSV convertion parameters
           if ( $UseCulture ) { $scriptCmdPipeline += ' -UseCulture ' }

           if ( $Delimiter ) { $scriptCmdPipeline += " -Delimiter '$Delimiter' " }
 
           # Skip the first line (the one with the property names)
           $scriptCmdPipeline += ' | Foreach-Object {$start=$true}'
           $scriptCmdPipeline += '{if ($start) {$start=$false} else {$_}} '
 
           # Add file output
           $scriptCmdPipeline += " | Out-File -FilePath '$Path' -Encoding '$Encoding' -Append "
 
           if ($Force) { $scriptCmdPipeline += ' -Force' }
 
           if ($NoClobber) { $scriptCmdPipeline += ' -NoClobber' } 
         }
       }
     }
 
    $scriptCmd = {& $wrappedCmd @PSBoundParameters }
 
    if ( $AppendMode )
    {
      #redefine command line
      $scriptCmd = $ExecutionContext.InvokeCommand.NewScriptBlock( $scriptCmdPipeline )
    }
    else
    {
      # execute Export-CSV as we got it because
      # either -Append is missing or file does not exist
      $scriptCmd = $ExecutionContext.InvokeCommand.NewScriptBlock( [string]$scriptCmd )
    }
 
    # standard pipeline initialization
    $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
    $steppablePipeline.Begin($PSCmdlet)
 
  }
  catch { throw}
 
  }
 
  process
  {
    try { $steppablePipeline.Process($_) }
    catch { throw }
  }
 
  end
  {
    try { $steppablePipeline.End() }
    catch { throw }
  }
 
  #.ForwardHelpTargetName Export-Csv
  #.ForwardHelpCategory Cmdlet
}

try
{
  $path = "$scan\Exchange_Errors.csv"
  
  If (test-path $path)
  { remove-item $path }
  
  $myData = @()
  
  foreach($server in $s)
  {
    $filter_application = @{logname = "Application"; starttime=(Get-Date).addDays(-1) }
    $filter_system = @{logname = "System"; starttime=(Get-Date).addDays(-1)}
   
    Get-WinEvent -computername $server -filterhashtable $filter_application | where-object { ($_.LevelDisplayName -ne "Information") -and ($_.ProviderName -ne "VMUpgradeHelper") -and (($_.ID -ne 7027) -and ($_.ProviderName -ne "Microsoft Forefront Protection")) `
    -and (($_.ID -ne 4113) -and ($_.ProviderName -ne "MSExchangeRepl")) }  `
    | Select-Object LogName, MachineName, timeCreated, LevelDisplayName, ID, ProviderName, Message `
    | Sort-Object Logname, machineName, timeCreated `
    | Export-Csva $path -append -notypeinformation

#  you can add other events to ignore in these
     
    Get-WinEvent -computername $server -filterhashtable $filter_system ` | where-object { ($_.LevelDisplayName -ne "Information") -and (($_.Id -ne 1111) -and ($_.ProviderName -ne "Microsoft-Windows-TerminalServices-Printers")) -and (($_.Id -ne 6037) -and ($_.ProviderName -ne "LsaSrv")) }  ` | Select-Object LogName, MachineName, timeCreated, LevelDisplayName, ID, ProviderName, Message `
    | Sort-Object Logname, machineName, timeCreated `
    | Export-Csva $path -append -notypeinformation
  }
   
  write-host "`n"
  write-host "                       " -ForeGroundColor "Blue" -BackGroundColor "White"
  write-host "     Scan Complete     " -ForeGroundColor "Blue" -BackGroundColor "White"
  write-host "                       " -ForeGroundColor "Blue" -BackGroundColor "White"
  write-host "`n"
  
}

catch { throw }

Thanks. Tony


Thursday, August 30, 2012 4:12 PM

As you can see from the parameter declaration, there are two mandatory parameters: InputObject and Path.  You must supply these for the script to work.

param(
    [Parameter(Mandatory=$true, ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
    [System.Management.Automation.PSObject]
    ${InputObject},
    [Parameter(Mandatory=$true, Position=0)]
  [Alias('PSPath')]
  [System.String]
  ${Path}

Grant Ward, a.k.a. Bigteddy


Thursday, August 30, 2012 6:35 PM

Ok

I guess I don't understand.

The script runs to some extent and creates the CSV with events from one of the servers, but still errors.

There's Path defined -  $path = "$scan\Exchange_Errors.csv"

Btw, the error  "A positional parameter cannot be found that accepts argument ' '."   is at C:\Errors-chk_exch4.ps1:175 char:17 

So Line 175 is just about at the end of the script.

Thanks. Tony


Saturday, September 1, 2012 3:21 AM

Just curious as to your reason for marking my post as the answer and then unmarking it?

Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.


Saturday, September 1, 2012 4:38 AM

No reason. Thought I had marked it accordingly. My apologizies.  Thanks again Al!

Thanks. Tony