0

I'm using the following code to try to use Log Parser to fire off and dump the contents of a .csv file I have into a SQL database. I'm having to try to use a custom function to strip out non-alphanumeric characters so that the columns can be created dynamically, because the end goal is to have this work with any .csv file I give it.

Here's the code:

start-process -NoNewWindow -FilePath "C:\Program Files (x86)\Log Parser 2.2\LogParser.exe" -ArgumentList @"

"Create Function [dbo].[RemoveNonAlphaNumCharacters](@Temp VarChar(1000))
Returns VarChar(1000)
AS
Begin

    Declare @KeepValues as varchar(50) = '%[^a-z0-9]%'
    While PatIndex(@KeepValues, @Temp) > 0
        Set @Temp = Stuff(@Temp, PatIndex(@KeepValues, @Temp), 1, '')

    Return @Temp
End
SELECT.RemoveNonAlphaNumCharacters * INTO SQLCounters FROM C:\Users\SeanLon\Desktop\SQL_Log_0.csv" -i:CSV -o:SQL -server:MJNHNX4 -database:PerfmonCounters -driver:"SQL Server" -createTable:ON

"@

And the error:

start-process : Process with an Id of 221104 is not running.
At C:\Users\seanlon\Desktop\Performance\Powershell examples\LogParser.ps1:1 char:1
+ start-process -NoNewWindow -FilePath "C:\Program Files (x86)\Log Parser 2.2\LogP ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Start-Process], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.StartProcessCommand

Can start-process or log parser simply not handle that complex of SQL within the argument list? Or is the LogParser process closing out before something else happens?

Sean Long
  • 2,163
  • 9
  • 30
  • 49

2 Answers2

1

On PowerShell 3.0 it looks like it is passing the parameters cleanly through. However, to simplify this you could put your SQL in a file foo.sql and then call logparser like so:

$logParser = "C:\Program Files (x86)\Log Parser 2.2\LogParser.exe"
& $logParser file:c:\foo.sql -i:CSV -o:SQL -server:MJNHNX4 -database:PerfmonCounters -driver:"SQL Server" -createTable:ON
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • I'll give that a shot and see if it lets me fire off the entire process that way. It could just be choking on the formatting. Of course, referencing another file means that if I ever do this from a different workstation I'd have to remember to modify the c:\foo.sql directory, or put the directory in a parameter. That is a minor annoyance though. – Sean Long May 28 '13 at 21:42
  • It is a place to start, if the above works then it gives you an idea of where it is failing and perhaps you could look in the SQL string further. If it still fails then perhaps it is another parameter that is not right or getting munged by PowerShell. As for the file, use [io.path]::GetTempFileName() to get the name of a new temp file you can write to. – Keith Hill May 29 '13 at 02:38
0

LogParser knows nothing about the database, it interprets and executes simple SQL queries in its internal engine, which doesn't handle stored procedures. Also see Logparser not recognizing SQL command.

Community
  • 1
  • 1
Gabriele Giuseppini
  • 1,541
  • 11
  • 19