Hello everyone.
I have a script that runs to get transaction log size. It runs, produces the output file.
However when it runs it does produce errors in the ISE.
I will include the script and then the error. Appreciate any advice on how to make it work or on how to correct it for better performance.
Many thanks in advance for any help supplied.
Script :
#Create a new Excel object using COM
$path
=
"D:\Scripts\ApprovalSQLServers_Translogs.xlsx"
if
((Test-Path-Path$path
)) {
Remove-Item
"D:\Scripts\ApprovalSQLServers_Translogs.xlsx"
}
#Create a new Excel object using COM $Excel
=New-Object-ComObject
Excel.Application
#$Excel.visible = $True #Add new workbook $Excel
=$Excel
.Workbooks.Add()
#Add new worksheet to workbook $Sheet
=$Excel.Worksheets
.Item(1)
#Counter variable for rows $intRow
=
1
ForEach ($instanceinGet-Content"D:\PowerShell\ApprovalSQLServers.txt"
) { [
System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') |
out-null
$s=New-Object ('Microsoft.SqlServer.Management.Smo.Server')
$instance
#This sets the connection to mixed-mode authentication
$s.ConnectionContext.LoginSecure=$false
;
#This sets the login name
$s.ConnectionContext.set_Login("XXXXXX"
);
#This sets the password
$s.ConnectionContext.set_Password("??????"
)
$dbs=$s.
Databases
$intRow
++
#Create column headers
$Sheet.Cells.Item($intRow,1) =
"Server: $s"
$Sheet.Cells.Item($intRow,1).Font.Size =
12
$Sheet.Cells.Item($intRow,1).Font.Bold =
$True
$intRow
++
$Sheet.Cells.Item($intRow,2) =
"Database"
$Sheet.Cells.Item($intRow,2).Font.Bold =
$True
$Sheet.Cells.Item($intRow,3) =
"Log Size (MB)"
$Sheet.Cells.Item($intRow,3).Font.Bold =
$True
$Sheet.Cells.Item($intRow,4) =
"Log Used Space (MB)"
$Sheet.Cells.Item($intRow,4).Font.Bold =
$True
$Sheet.Cells.Item($intRow,5) =
"PercentageUsed(%)"
$Sheet.Cells.Item($intRow,5).Font.Bold =
$True
foreach ($dbin$dbs
) {
write-host$fg.files | select
name
$intRow
++
$logInfo=$db.LogFiles | Select Name, FileName, Size,
UsedSpace
If ($loginfo.UsedSpace/$loginfo.Size* 100 -gt
50 ) {
$dbname=$db.
Name
$Sheet.Cells.Item($intRow,2) =
$dbname
$Sheet.Cells.Item($intRow,3) = ($logInfo.Size/
1024)
$Sheet.Cells.Item($intRow,4) = ($logInfo.UsedSpace/
1024)
$Sheet.Cells.Item($intRow,5) = ($loginfo.UsedSpace/$loginfo.Size*
100)
$Sheet.Cells.Item($intRow,5).Interior.ColorIndex =
3 }
else
{
$dbname=$db.
Name
$Sheet.Cells.Item($intRow,2) =
$dbname
$Sheet.Cells.Item($intRow,3) = ($logInfo.Size/
1024)
$Sheet.Cells.Item($intRow,4) = ($logInfo.UsedSpace/
1024)
$Sheet.Cells.Item($intRow,5) = ($loginfo.UsedSpace/$loginfo.Size*
100)
$Sheet.Cells.Item($intRow,5).Interior.ColorIndex =
14 } }
$intRow
++
} $Sheet
.UsedRange
.EntireColumn.AutoFit()
$Excel
.SaveAs("D:\PowerShell\ApprovalSQLServers_Translogs.xlsx"
)
#Close workbook
$Excel
.Close()
#Exit Excel
#$Excel.Quit() #cls
This is the error produced:
The following exception was thrown when trying to enumerate the collection: "An exception occurred while executing a Transact- SQL statement or batch.". At D:\PowerShell\PS_Scripts\TransactionLogUsage_final.ps1:43 char:17 + $logInfo = <<<< $db.LogFiles | Select Name, FileName, Size, UsedSpace + CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException + FullyQualifiedErrorId : ExceptionInGetEnumerator