Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get-Content -Raw parameter error

Tags:

sql

powershell

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?

like image 719
Sri Avatar asked Sep 19 '16 18:09

Sri


People also ask

How do I get error messages in PowerShell?

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.

What does $_ mean in PowerShell?

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.

What is Lastexitcode in PowerShell?

$LASTEXITCODEContains the exit code of the last native program or PowerShell script that was run.

How do I get the PowerShell error log?

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.


1 Answers

The -Raw parameter of Get-Content was introduced in PS3.

To get file contents in one string there are several methods.

  • The fastest method that works in any PS version:
    $text = [IO.File]::ReadAllText('c:\path\file.ext')
  • The 2 times slower method for PS3+:
    $text = Get-Content 'c:\path\file.ext' -Raw
  • The 100 times slower PS2-compatible alternative:
    $text = Get-Content 'c:\path\file.ext' | Out-String
  • The 30 times slower PS2-compatible alternative:
    $text = Get-Content 'c:\path\file.ext' -ReadCount 1000 | Out-String
like image 149
wOxxOm Avatar answered Oct 21 '22 19:10

wOxxOm