Hey PoSh experts,So I need to get the max size setting of all SQL databases in the primary filegroup, excluding the system databases and the tempdb. I was told I'll have to loop through the DB's to get the maxsize setting of the database, then add them up. No. You have to go into the the filegroup -> files and get the max size there. I could not for the life of me work out how I can do this, will I did, but it didnt work properly...foreach($blah in $Files){$Server.Databases.filegroups.files | select Name, Maxsize | Format-Table -AutoSize}Now I have limited experience with foreach loops, I gotta learn to use them, but this gave me the result 10 times. I know why, I just didnt know how to stop it from doing that. The other issue was it included the system and tempdb databases (Tempdb has multiple datafiles). At one point I created an array which I thought I could use to exclude the files....$ExcludeFiles = @("TempDB", "master", "model", "MSDB")...if this was done correctly, then I could not get it to work in the foreach. I also had trouble getting it to be effective in a pipe.After much tooing an froing(?), I ended up with this...# Ok, so the below code will get the max size of the user databases (leaving out system databases and tempdb), and adds it together.$TotalDataFileSize = ($Server.Databases.filegroups.files | select Name, Maxsize |Where-Object {($_.name -notlike '*TempDB*'-and $_.name -notlike 'master' -and $_.name -notlike '*model*' -and $_.name -notlike '*MSDB*')} | Measure-Object -Sum Maxsize).Sum# Now it converts it from KB to MB.$TotalDataInMB = $TotalDataFileSize / 1MB# This variable is the total in MB.$TotalDataInMBLearning tonnes, but it ain't always belly laughs! TBH, this has been a frustrating experience with smo. I have to do the same for the logs, but they seem much easier to access and play with, so I do not think I'll have as much trouble.Is there a better and neater way of doing this? Is this amateur hour? Or just another way of skinning a cat?Regards,D.
↧