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

I am trying to execute below PowerShell script to audit a file share for filenames starting with S. This script is executed by a SQL job every 10 seconds.

Script also tries to create a Log file each day and appends the data.

Script:

$svcs = Get-ChildItem -Path "\\XYZ\1100\ARCHIVE" | ?{ $_.name.StartsWith("S") } $filepath = "D:\Audit_1100\" + (date -f yyyy-MM-dd) + "_ArchiveAudit.txt" $svcs | select-object LastWriteTime,Name | sort LastWriteTime | out-file -append $filepath

Problem : I see below error while executing the script. Could you please advise what is missing here ?

A job step received an error at line 1 in a PowerShell script. The corresponding line is '$svcs = Get-ChildItem -Path "\\XYZ\1100\ARCHIVE" | ?{ $_.name.StartsWith("S") } $filepath = "D:\Audit_1100\" + (date -f yyyy-MM-dd) + "_ArchiveAudit.txt" $svcs | select-object LastWriteTime,Name | sort LastWriteTime | out-file -append $filepath'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'A positional parameter cannot be found that accepts argument '$null'.  '.  Process Exit Code -1.  The step failed.

Additional Error Details :

Where-Object : A positional parameter cannot be found that accepts argument 'D:\Audit_1100\2015-02-06_ArchiveAudit.txt'.

At line:1 char:63

+ $svcs = Get-ChildItem -Path "\\XYZ\1100\ARCHIVE" | ?{ $_.name.ToLower ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Where-Object], ParameterBindingException

    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.WhereObjectCommand


  • Edited by KiranAitha Friday, February 06, 2015 11:32 PM
February 7th, 2015 2:23am

Seems you have a problem with placing everything on one line.  You cannot so that I n PowerShell or in any other language:

$filepath = "D:\Audit_1100\$([datetime]::TOday.ToString('yyyy-MM-dd'))_ArchiveAudit.txt" 
Get-ChildItem -Path \\XYZ\1100\ARCHIVE | 
    ?{ $_.name.StartsWith('S') }  | 
    select-object LastWriteTime,Name | 
    out-file -append $filepath
Free Windows Admin Tool Kit Click here and download it now
February 7th, 2015 3:01am

Thank you for the help. I am able to run this script from PowerShell ISE with out any issues.
Same fails to execute as a SQL job with below error :

Unable to start execution of step 1 (reason: line(1): Syntax error).  The step failed.

Job step is configured as below.

Do i need to mention any additional commands for this to execute as part of SQL agent ?

Thanks

 
February 7th, 2015 3:44am

What version of PowerShell is running the SQL job?

Also note that you original question has been answered.  If you are still having issues you sould post in SQLServer forum as they will be able to help you undersstand how SQL Jobs work with PowerShell.

Free Windows Admin Tool Kit Click here and download it now
February 7th, 2015 4:06am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics