0

I have a file which is shown as below:

DateTime: 2018-02-09 02:00:12
Database: [master]
Status: ONLINE
Mirroring role: None
Standby: No
Updateability: READ_WRITE
User access: MULTI_USER
Is accessible: Yes
Recovery model: SIMPLE
Differential base LSN: 940000000008500178
Last log backup LSN: NULL

DateTime: 2018-02-09 02:00:12
Command: DECLARE @ReturnCode int EXECUTE @ReturnCode = [master].dbo.xp_create_subdir N'J:\dump_data\sqlserver234\db_dump' IF @ReturnCode  0 RAISERROR('Error creating directory.', 16, 1)
Outcome: Succeeded
Duration: 00:00:00
DateTime: 2018-02-09 02:00:12

DateTime: 2018-02-09 02:00:12
Command: BACKUP DATABASE [master] TO DISK = N'J:\dump_data\sqlserver234\db_dump\master_FULL_20180209_020012.bak' WITH CHECKSUM, COMPRESSION
Processed 512 pages for database 'master', file 'master' on file 1.
Processed 3 pages for database 'master', file 'mastlog' on file 1.
BACKUP DATABASE successfully processed 515 pages in 0.088 seconds (45.693 MB/sec).
Outcome: Succeeded
Duration: 00:00:00
DateTime: 2018-02-09 02:00:12

DateTime: 2018-02-09 02:00:12
Command: RESTORE VERIFYONLY FROM DISK = N'J:\dump_data\sqlserver234\db_dump\master_FULL_20180209_020012.bak'
The backup set on file 1 is valid.
Outcome: Succeeded
Duration: 00:00:00
DateTime: 2018-02-09 02:00:12

DateTime: 2018-02-09 02:00:12
Database: [model]
Status: ONLINE
Mirroring role: None
Standby: No
Updateability: READ_WRITE
User access: MULTI_USER
Is accessible: Yes
Recovery model: SIMPLE
Differential base LSN: 31000001141300037
Last log backup LSN: NULL

DateTime: 2018-02-09 02:00:12
Command: DECLARE @ReturnCode int EXECUTE @ReturnCode = [master].dbo.xp_create_subdir N'J:\dump_data\sqlserver234\db_dump' IF @ReturnCode  0 RAISERROR('Error creating directory.', 16, 1)
Outcome: Succeeded
Duration: 00:00:00
DateTime: 2018-02-09 02:00:12

DateTime: 2018-02-09 02:00:12
Command: BACKUP DATABASE [model] TO DISK = N'J:\dump_data\sqlserver234\db_dump\model_FULL_20180209_020012.bak' WITH CHECKSUM, COMPRESSION
Processed 320 pages for database 'model', file 'modeldev' on file 1.
Processed 2 pages for database 'model', file 'modellog' on file 1.
BACKUP DATABASE successfully processed 322 pages in 0.048 seconds (52.256 MB/sec).
Outcome: Failed
Duration: 00:00:00
DateTime: 2018-02-09 02:00:12

DateTime: 2018-02-09 02:00:12
Command: RESTORE VERIFYONLY FROM DISK = N'J:\dump_data\sqlserver234\db_dump\model_FULL_20180209_020012.bak'
The backup set on file 1 is valid.
Outcome: Failed
Duration: 00:00:00
DateTime: 2018-02-09 02:00:12

I have written a PowerShell file which give me databasename and corresponding outcome:

param(
 [Parameter(Mandatory=$True)][string]$path  
)

#param([string]$path)

# <context>
#   <description>
#   Sending output to console
#   </description>
# </context>

try
{
    foreach($line in [System.IO.File]::ReadLines("E:\utility\TEDM_DBA_M_MNT_BACKUP_System.txt"))
    {
        $database=$line|select-string -pattern 'Database:'          
        $outcome=$line|select-string -pattern 'Outcome:'            
        Write-host $outcome

        if($outcome -eq 'Outcome: Failed')
        {
            Write-Host $outcome
        }
    }           
}
Catch
{
    Write-Host -BackgroundColor Red -ForegroundColor White "Fail" 
    $errText =  $Error[0].ToString() 
    if ($errText.Contains("network-related")) 
    {
        Write-Host "Connection Error. Check server name, port, firewall."
    }   
    Write-Host $errText 
    continue 
}

But when I am comparing string with Outcome: failed it passes for all conditions:

 if($outcome -eq 'Outcome: Failed')

What am I doing wrong in the string comparison?

Tim
  • 4,790
  • 4
  • 33
  • 41
deepti
  • 729
  • 4
  • 17
  • 38
  • Possible duplicate of [How to compare the contents of two string objects in PowerShell](https://stackoverflow.com/questions/18772063/how-to-compare-the-contents-of-two-string-objects-in-powershell) – Ocaso Protal Feb 12 '18 at 12:14
  • iam writing code $a = "is" $b = "is" $c = "is" if($outcome.equals($b)) { write-host "true" } } } its giving me cannot call method on null value expression $outcome=$line|select-string -pattern 'Outcome:' – deepti Feb 12 '18 at 12:24
  • $a = "is" $b = "is" $c = "is" if($outcome.equals($b)) { write-host "true" } } } its giving error cannot call methode on null valued expression – deepti Feb 12 '18 at 12:26
  • Why not use `master.dbo.CommandLog`? – vonPryz Feb 12 '18 at 12:43
  • Ahh, now I understand. You also need to check if $outcome is not null: `-not ([string]::IsNullOrEmpty($outcome))`, see e.g. https://stackoverflow.com/questions/45008016/check-if-a-string-is-not-null-or-empty – Ocaso Protal Feb 12 '18 at 13:10
  • Made more clear and fixed code formatting – Tim Feb 13 '18 at 00:23
  • not working not working – deepti Feb 14 '18 at 05:41
  • what is master.dbo.CommandLog how to use this – deepti Feb 14 '18 at 06:12

0 Answers0