Solved

PowerShell Logging

Posted on 2014-10-08
14
197 Views
Last Modified: 2014-10-17
I have a script that runs a sql script to import data. I can't get it to write errors to a log file though. I have tried out-file. Can someone please help? Code below
Function Import-MySQLDatabase{

        param(

        [string]$LoginPath,
        [string]$Database
  
)
     
     #Set variables
      $Date = Get-Date -UFormat "%m%d%Y"
      $Timestamp = Get-Date
      $MySQL = "MySQLpath\mysql.exe" 
      $SQLDrop = "ScriptPath\dropAllTablesAndViews.sql"
      $SQLFirst = "ScriptPath\First_Deployment_part1.sql"
      $SQLCreate = "ScriptPath\Create_Tables.sql"
      $PreRefresh = "ScriptPath\PreRefresh.sql"     
      $SQLDataFile="FilePath\SQL_Dump_$Date.sql"
      $LogFile ="LogPath\ImportStatus_$Date.txt"
      $MySQLErrorLog = "Logpath\Errors_$Date.txt"    
      
      Try{      
      Add-Content $Logfile -Value "Running script $Prerefresh"                                   
      &$mysql --login-path=$LoginPath --database=$Database -e "source $PreRefresh" 
                                                                                                                   
      Add-Content $Logfile -Value "Dropping tables..."                                              
      &$mysql --login-path=$LoginPath --database=$Database -e "source $SQLDrop"                  
        
      Add-Content $Logfile -Value "Table drop complete. Creating first tables"                        
      &$mysql --login-path=$LoginPath --database=$Database -e "source $SQLFirst"                  

      Add-Content $Logfile -Value "First tables complete. Creating additional tables..."             
      &$mysql --login-path=$LoginPath --database=$Database -e "source $SQLCreate"              
 
      Add-Content $Logfile -Value "Beginning data import on $Timestamp"  
      Add-Content $Logfile -Value "Importing data..."
      Set-Location "MySQLInstallation\bin\" 
      &cmd /c "MySQL --login-path=$LoginPath -D$Database < $SQLDataFile" 
      }
      Catch{
     
      Write-Output Error[0] | Out-File $MySQLErrorLog
                    
      }
         
       $Timestamp1 = Get-Date
       Add-Content $Logfile -Value "Data import completed on $Timestamp1"
       

}

Open in new window

0
Comment
Question by:AshleySPM
  • 7
  • 6
14 Comments
 
LVL 1

Author Comment

by:AshleySPM
ID: 40368558
That did not seem to work. Is there anything wrong with my syntax anywhere? I saw this work briefly yesterday and then started messing around with the script and I can't get it to work again
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40368590
Maybe something like this will work?

Catch{
$ExceptionName = $_.Exception.GetType().FullName; 
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-Output $ExceptionName $ErrorMessage $FailedItem | Add-Content $MySQLErrorLog
}

Open in new window

0
 
LVL 1

Author Comment

by:AshleySPM
ID: 40368601
&$mysql --login-path=$LoginPath --database=$Database -e "source $PreRefresh"  2>$MySQLErrorLog
                                                                                                                   
      Add-Content $Logfile -Value "Dropping tables..."                                              
      &$mysql --login-path=$LoginPath --database=$Database -e "source $SQLDrop" 2>>$MySQLErrorLog                
       
      Add-Content $Logfile -Value "Table drop complete. Creating first tables"                        
      &$mysql --login-path=$LoginPath --database=$Database -e "source $SQLFirst" 2>>$MySQLErrorLog                  

      Add-Content $Logfile -Value "First tables complete. Creating additional tables..."            
      &$mysql --login-path=$LoginPath --database=$Database -e "source $SQLCreate" 2>>$MySQLErrorLog            
 
      Add-Content $Logfile -Value "Beginning data import on $Timestamp"  
      Add-Content $Logfile -Value "Importing data..."
      Set-Location "MySQLInstallation\bin\"
      &cmd /c "MySQL --login-path=$LoginPath -D$Database < $SQLDataFile"  2>>$MySQLErrorLog
0
 
LVL 1

Author Comment

by:AshleySPM
ID: 40368604
The above worked for me but I'll try yours since it could definitely be cleaner output
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40368614
Yes, I was going to suggest that but It is very difficult to use. Ive had to look through an output file generated like that, it took me 2 days. It was also several thousand lines and using the "Find" tool lol
0
 
LVL 1

Author Comment

by:AshleySPM
ID: 40368620
Sadly no luck with that
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40368635
Maybe this will work, keep it simple

$error[0] | Add-Content $MySQLErrorLog
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 1

Author Comment

by:AshleySPM
ID: 40368707
Like this?
Try{
Do stuff
}
Catch{
      $error[0] | Add-Content $MySQLErrorLog 
      }

Open in new window

0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40368718
yea, I tried it and it worked for me using a single command.

EDIT: Ive also added some returns to help separate the errors and a clear at the end to remove all errors from the session.

try { get-aduser "FakeUser" }
catch { "`n`n" | Add-Content "C:\ErrorLog.txt" 
$error | Add-Content "C:\ErrorLog.txt" }
$error.Clear()

Open in new window

0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40368757
I added to your function

Function Import-MySQLDatabase
{

        param(

        [string]$LoginPath,
        [string]$Database
        )
     
     #Set variables
      $Date = Get-Date -UFormat "%m%d%Y"
      $Timestamp = Get-Date
      $MySQL = "MySQLpath\mysql.exe" 
      $SQLDrop = "ScriptPath\dropAllTablesAndViews.sql"
      $SQLFirst = "ScriptPath\First_Deployment_part1.sql"
      $SQLCreate = "ScriptPath\Create_Tables.sql"
      $PreRefresh = "ScriptPath\PreRefresh.sql"     
      $SQLDataFile="FilePath\SQL_Dump_$Date.sql"
      $LogFile ="LogPath\ImportStatus_$Date.txt"
      $MySQLErrorLog = "Logpath\Errors_$Date.txt"    
      
      Try{      
      Add-Content $Logfile -Value "Running script $Prerefresh"                                   
      &$mysql --login-path=$LoginPath --database=$Database -e "source $PreRefresh" 
                                                                                                                   
      Add-Content $Logfile -Value "Dropping tables..."                                              
      &$mysql --login-path=$LoginPath --database=$Database -e "source $SQLDrop"                  
        
      Add-Content $Logfile -Value "Table drop complete. Creating first tables"                        
      &$mysql --login-path=$LoginPath --database=$Database -e "source $SQLFirst"                  

      Add-Content $Logfile -Value "First tables complete. Creating additional tables..."             
      &$mysql --login-path=$LoginPath --database=$Database -e "source $SQLCreate"              
 
      Add-Content $Logfile -Value "Beginning data import on $Timestamp"  
      Add-Content $Logfile -Value "Importing data..."
      Set-Location "MySQLInstallation\bin\" 
      &cmd /c "MySQL --login-path=$LoginPath -D$Database < $SQLDataFile" 
      }
      Catch { 
      "`n`n" | Add-Content $MySQLErrorLog
      $error | Add-Content $MySQLErrorLog
      $error.Clear()
      }
      
      $Timestamp1 = Get-Date
      Add-Content $Logfile -Value "Data import completed on $Timestamp1"
}

Open in new window

0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40369050
Any luck Ashley?
0
 
LVL 1

Author Comment

by:AshleySPM
ID: 40369123
Nope
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40371842
For using try/catch you need a stopping error, so the complete command fails. I reckon mysql will not throw fatal errors (or exceptions), but outputs errors to stdout only.
Just as a note, if $mysql does not result in an executable name, the catch gets triggered.

Indeed there is no other way than to redirect stderr to stdout or a file to get the error messages.
You can add the redirection to each command, similar as you have done already, or apply it to a script or block:
$(
      &$mysql --login-path=$LoginPath --database=$Database -e "source $PreRefresh" 
                                                                                                                   
      Add-Content $Logfile -Value "Dropping tables..."                                              
      &$mysql --login-path=$LoginPath --database=$Database -e "source $SQLDrop"                  
        
      Add-Content $Logfile -Value "Table drop complete. Creating first tables"                        
      &$mysql --login-path=$LoginPath --database=$Database -e "source $SQLFirst"                  

      Add-Content $Logfile -Value "First tables complete. Creating additional tables..."             
      &$mysql --login-path=$LoginPath --database=$Database -e "source $SQLCreate"              
 
      Add-Content $Logfile -Value "Beginning data import on $Timestamp"  
      Add-Content $Logfile -Value "Importing data..."
      Set-Location "MySQLInstallation\bin\" 
      &cmd /c "MySQL --login-path=$LoginPath -D$Database < $SQLDataFile" 
) 2>&1 | out-file $MySQLErrorLog

Open in new window

But I would write the output to one file, to have the command sequence and timing in view when an error is reported.
0
 
LVL 1

Author Comment

by:AshleySPM
ID: 40382951
Thank you I will try that!
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Creating and Managing Databases with phpMyAdmin in cPanel.
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now