Solved

PowerShell Logging

Posted on 2014-10-08
14
208 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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
 
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 69

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article will help you understand what HashTables are and how to use them in PowerShell.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 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