none
Parse string not work

    Question

  • Hello,

    I have this script:

    cls
    $logfile_package_ct = ""
    $file = "c:\Temp\DatabaseDeploy.log"
    $logfile_package_ct = 	Get-Content $file
    $logfile_package_ct
    if	((($logfile_package_ct | select-string -Pattern 'Error | Exception' -AllMatches).Matches.Count) -gt 0 `
    	-or ($logfile_package_ct -eq $null) `
    	)
    	
    {write-host "found"}
    else
    {write-host "not found"}

    The value from the logfile is:

    The term 'C:\Program Files\DatabaseDeploy.ps1' is not recognized

    as the name of a cmdlet, function, script file, or operable program. Check the

    spelling of the name, or if a path was included, verify that the path is correc

    t and try again.

    At line:1 char:5

    + & {& <<<< 'C:\Program Files\DatabaseDeploy.ps1' 'C:\Program Fil

    es\' 'C:\Program Files\Staging\Logs\MESDI\Robocopy_Task.log'

    '1.4.6.9.1234523' 'GDC' 'Stage to CONS' 'Database;Client;' '1.2.3.4.5.6.7'}

    + CategoryInfo : ObjectNotFound: (C:\Program File...abaseDeploy.p

    s1:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

    Exception

    --------------

    I know, why the script doesn't work, because the ps1 file is not there.

    I want to check, if an error or exception ocure at logfile, the script should stop.

    But now, the select-string command doesn't find the value exception.

    The result from script is:

    not found

    Why that,

    Thanks Horst MOSS 2007 Farm; MOSS 2010 Farm; TFS 2010; IIS 7.5


    • Edited by wuwu Friday, August 23, 2013 1:30 PM wrong entry
    Friday, August 23, 2013 1:26 PM

Answers

  • Give this a try:

    Get-Content 'c:\Temp\DatabaseDeploy.log' | ForEach-Object {
    	$count = (Select-String -InputObject $_ -Pattern 'Error|Exception' -AllMatches).Matches.Count
    	if (($count -gt 0))
    	{
    		Write-Host 'Match Found'
    	}
    	Else
    	{
    		Write-Host 'Match not found'
    	}
    }

    I also took the -or ($logfile_package_ct -eq $null) out, because if the file is null then you can't find a match, so I was confused why you had that there.

    Also, if I had the pattern like 'Error | Exception' it did not seem to find all matches, but once I changed it to 'Error|Exception' that seemed to work, atleast for me it did


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Don't Retire Technet

    • Marked as answer by wuwu Friday, August 23, 2013 5:11 PM
    Friday, August 23, 2013 3:06 PM

All replies

  • Correct the if loop ...... if ((something) -or (Something))

    Change the below

    (((($logfile_package_ct | select-string -Pattern 'Error | Exception' -AllMatches).Matches.Count) -gt 0) -or ($logfile_package_ct -eq $null) `
     )


    Thanks Azam When you see answers please Mark as Answer if Helpful..vote as helpful.


    Friday, August 23, 2013 2:25 PM
  • Hello,

    I have tried this, the same result:

    not found

    He can not find the text string Exception, I don't know why,


    Thanks Horst MOSS 2007 Farm; MOSS 2010 Farm; TFS 2010; IIS 7.5

    Friday, August 23, 2013 2:45 PM
  • Give this a try:

    Get-Content 'c:\Temp\DatabaseDeploy.log' | ForEach-Object {
    	$count = (Select-String -InputObject $_ -Pattern 'Error|Exception' -AllMatches).Matches.Count
    	if (($count -gt 0))
    	{
    		Write-Host 'Match Found'
    	}
    	Else
    	{
    		Write-Host 'Match not found'
    	}
    }

    I also took the -or ($logfile_package_ct -eq $null) out, because if the file is null then you can't find a match, so I was confused why you had that there.

    Also, if I had the pattern like 'Error | Exception' it did not seem to find all matches, but once I changed it to 'Error|Exception' that seemed to work, atleast for me it did


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Don't Retire Technet

    • Marked as answer by wuwu Friday, August 23, 2013 5:11 PM
    Friday, August 23, 2013 3:06 PM
  • Thanks, it works :-)

    Thanks Horst MOSS 2007 Farm; MOSS 2010 Farm; TFS 2010; IIS 7.5

    Friday, August 23, 2013 5:11 PM