Hi All
I have this piece of code, however its running really slow. I was wondering if there is an alternative to this piece of code.
try
{
$ErrorActionPreference = "Stop"; #Make all errors terminating
$CITbl = "[dbo].[databasedata]”
$s = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $inst
#$dbs=$s.Databases
foreach ($db in $s.Databases) {
if ($db.IsAccessible -eq $True) {
$dt= $db | Select @{n="ServerName";e={$svr}}, @{n="InstanceName";e={$inst}}, Name, Status, Owner, CreateDate, Size,
@{n="DBSpaceAvailableInMB";e={[math]::round(($_.SpaceAvailable / 1024), 2)}},
@{e={"{0:N2}" -f ($_.Size-($_.SpaceAvailable / 1024))};n=”DBUsedSpaceInMB”},
@{e={"{0:P2}" -f (($_.SpaceAvailable / 1024)/$_.Size)};n=”DBPctFreeSpace”},
@{n="DBDataSpaceUsageInMB";e={[math]::round(($_.DataSpaceUsage / 1024), 2)}},
@{n="DBIndexSpaceUsageInMB";e={[math]::round(($_.IndexSpaceUsage / 1024), 2)}},
ActiveConnections, Collation, RecoveryModel, CompatibilityLevel, PrimaryFilePath,
LastBackupDate, LastDifferentialBackupDate, LastLogBackupDate, AutoShrink, AutoUpdateStatisticsEnabled,IsReadCommittedSnapshotOn,
IsFullTextEnabled, BrokerEnabled, ReadOnly, EncryptionEnabled, IsDatabaseSnapshot, ChangeTrackingEnabled,
IsMirroringEnabled, MirroringPartnerInstance, MirroringStatus, MirroringSafetyLevel, ReplicationOptions, AvailabilityGroupName,
@{n="NoOfTbls";e={$_.Tables.Count}}, @{n="NoOfViews";e={$_.Views.Count}}, @{n="NoOfStoredProcs";e={$_.StoredProcedures.Count}},
@{n="NoOfUDFs";e={$_.UserDefinedFunctions.Count}}, @{n="NoOfLogFiles";e={$_.LogFiles.Count}}, @{n="NoOfFileGroups";e={$_.FileGroups.Count}},
@{n="NoOfUsers";e={$_.Users.Count}}, @{n="NoOfDBTriggers";e={$_.Triggers.Count}},
@{n="LastGoodDBCCChecKDB"; e={$($_.ExecuteWithResults("dbcc dbinfo() with tableresults").Tables[0] | where {$_.Field -eq "dbi_dbccLastKnownGood"}| Select Value).Value}},
AutoClose, HasFileInCloud, HasMemoryOptimizedObjects, MemoryAllocatedToMemoryOptimizedObjectsInKB, MemoryUsedByMemoryOptimizedObjectsInKB,
@{n="DateAdded";e={$RunDt}} | out-datatable
Write-DataTable -ServerInstance $SQLInst -Database $Centraldb -TableName $CITbl -Data $dt
#write-log -Message "Database info collection Finished on $svr at | $(get-date)" -NoConsoleOut -Path $progress_log
}}#end For Each
write-log -Message "Database info collection finished on | $svr at | $(get-date)" -NoConsoleOut -Path $progress_log
}
catch
{
$ex = $_.Exception
}finally{
$ErrorActionPreference = "Continue"; #Reset the error action pref to default
}