Sri
Sri

Reputation: 111

Get-Content -Raw parameter error

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?

Upvotes: 11

Views: 10198

Answers (3)

jschmitter
jschmitter

Reputation: 1929

I've experienced this error in ps v 5 when the -path parameter wasn't actually a path.

Try adding Write-Host $_.fullname above the line throwing the error to make sure $_.fullname is actually what you think it is.

Upvotes: 7

woxxom
woxxom

Reputation: 73526

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

Upvotes: 11

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200233

You're using PowerShell v2 or earlier. The parameter -Raw was introduced with PowerShell v3. Either upgrade PowerShell or pipe the output of Get-Content through Out-String:

$query = Get-Content -Path $_.FullName | Out-String

You should also be able to run the files directly with sqlcmd (i.e. without reading their content and passing that to the command):

$result = SQLCMD -S $FullDBServer -E -I -i $_.FullName -d $Database

Upvotes: 3

Related Questions