Reputation: 111
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
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
Reputation: 73526
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
Upvotes: 11
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