How to select Substring

Hi All,
I was trying to selecting string using a pattern “error”, So i am getting two different errors like below.

  1. Desktop\DWH\datawarehouse.log:260:2016-03-01 01:41:24,077 [DwhTaskQueue] ERROR dwh
    :316 - Error in SQL: Sql[sqlString=delete from “P_CONTROLMEASURE” where “CONTROLME
    ASUREID” in (select “CONTROLMEASUREID” from “T_CONTROLMEASURE” where “AUDITID”=?),
    sqlTypes=[2]]
    Desktop\DWH\datawarehouse.log:262: at com.microsoft.sqlserver.jdbc.SQLServerExc
    eption.makeFromDatabaseError(SQLServerException.java:216)

  2. Desktop\DWH\datawarehouse.log:549:2016-03-01 11:00:22,038 [DwhTaskQueue] ERROR dwh
    :326 - Connect failed: The driver could not establish a secure connection to SQL S
    erver by using Secure Sockets Layer (SSL) encryption. Error: “Read timed out Clien
    tConnectionId:6d43ba4a-ab04-430d-a966-0f08a1479079”.

I wanted to select all errors in file by searching with “error” string except some of the errors

For example select-string -pattern “error” it will get all errors in file but i dont want to select all errors. the result is above two error but i want to select this error and ignore another one

Desktop\DWH\datawarehouse.log:549:2016-03-01 11:00:22,038 [DwhTaskQueue] ERROR dwh
:326 - Connect failed: The driver could not establish a secure connection to SQL S
erver by using Secure Sockets Layer (SSL) encryption. Error: “Read timed out Clien
tConnectionId:6d43ba4a-ab04-430d-a966-0f08a1479079”.

Please help me how can i do this.

A switch statement will check each line of your log file.

# Display lines containing 'connect failed'
$file = Get-ChildItem -Path '.\Desktop\DWH\datawarehouse.log'
switch -Regex -File $file
{'connect failed' {$_}}

I am gettinf same output by running this script
$file = Get-ChildItem -Path ‘C:\Users\Username\Desktop\folder\datawarehouse.log’|sort {$.LastWriteTime} | select -last 1
switch -Regex -File $file
{‘connect failed’ {$
}}

and
this script
$LogError = gci C:\Users\username\Desktop\DWH\datawarehouse.log | sort {$_.LastWriteTime} | select -last 1 | select-string -pattern “ERROR”

Output:

[DwhTaskQueue] ERROR dwh:316 - Error in SQL: Sql[sqlString
=delete from “P_CONTROLMEASURE” where “CONTROLMEASUREID” in (select “CONTROLMEASUR
EID” from “T_CONTROLMEASURE” where “AUDITID”=?), sqlTypes=[2]]
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServe
rException.java:216)

[DwhTaskQueue] ERROR dwh:316 - Error in SQL: Sql[sqlString
=delete from “P_CONTROLMEASURE” where “CONTROLMEASUREID” in (select “CONTROLMEASUR
EID” from “T_CONTROLMEASURE” where “AUDITID”=?), sqlTypes=[2]]
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServe
rException.java:216)

[DwhTaskQueue] ERROR dwh:326 - Connect failed: The driver
could not establish a secure connection to SQL Server by using Secure Sockets Laye
r (SSL) encryption. Error: “Read timed out ClientConnectionId:6d43ba4a-ab04-430d-a
966-0f08a1479079”.

In this file there are three error in this case i need to select only one that is “connect failed error” and want to ignore other one.
i want to search an error with error string so that wherever error comes in file it will find that.

Please help

If each error is on a separate line, the switch statement should work. It should match any line containing the string ‘connect failed.’ Also, your post has only one log file, you do not have to use sort-object or select-object.

 # Example
$logfile = @"
[DwhTaskQueue] ERROR dwh:316 – Error in SQL: Sql[sqlString=delete from "P_CONTROLMEASURE" where "CONTROLMEASUREID" in (select "CONTROLMEASUREID" from "T_CONTROLMEASURE" where "AUDITID"=?), sqlTypes=[2]]at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
[DwhTaskQueue] ERROR dwh:316 – Error in SQL: Sql[sqlString=delete from "P_CONTROLMEASURE" where "CONTROLMEASUREID" in (select "CONTROLMEASUREID" from "T_CONTROLMEASURE" where "AUDITID"=?), sqlTypes=[2]]at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
[DwhTaskQueue] ERROR dwh:326 – Connect failed: The drivercould not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "Read timed out ClientConnectionId:6d43ba4a-ab04-430d-a966-0f08a1479079".
"@ -split "`n"

$logfile -match 'connect failed'
# Results
# [DwhTaskQueue] ERROR dwh:326 – Connect failed: The drivercould not establish a #secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: # "Read timed out ClientConnectionId:6d43ba4a-ab04-430d-a966-0f08a1479079".#>

Thnaks