I have a PowerShell script that would recursively loop a dir and subdir and run all SQL files inside of it and log the execution in .log files 1 for success and 1 for exceptions. The PowerShell script does what its supposed to do but in the cmd window, I see this error:
Get-Content : A parameter cannot be found that matches parameter name 'Raw'
from this line
$query = Get-Content -Path $_.FullName -Raw
This statement runs within a loop, so FullName
changes per iteration. This is the version I use.
Name : Windows PowerShell ISE Host Version : 5.0.10586.117
Sample script goes below:
Get-ChildItem $ScriptFolder -Recurse -Exclude "*Archive*" -Filter *.sql |
sort Directory |
ForEach-Object {
$query = Get-Content -Path $_.FullName -Raw
$result = SQLCMD -S $FullDBServer -E -I -Q $query -d $Database
Any thoughts?
You can use Get-Error to display a specified number of errors that have occurred in the current session using the Newest parameter. The Get-Error cmdlet also receives error objects from a collection, such as $Error , to display multiple errors from the current session.
The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.
$LASTEXITCODEContains the exit code of the last native program or PowerShell script that was run.
In order to log or report an error, you first need to capture the error. This is called error handling. The most common way to handle a PowerShell error is using the try/catch blocks. The catch block is only invoked when the error is a Terminating Error.
The -Raw
parameter of Get-Content
was introduced in PS3.
To get file contents in one string there are several methods.
$text = [IO.File]::ReadAllText('c:\path\file.ext')
$text = Get-Content 'c:\path\file.ext' -Raw
$text = Get-Content 'c:\path\file.ext' | Out-String
$text = Get-Content 'c:\path\file.ext' -ReadCount 1000 | Out-String
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With