使用PowerShell提取SQL数据文件和日志文件

时间:2013-02-06 07:03:55

标签: sql powershell

我正面临着这个功能的问题。

function GetDBSize {
                #Get Instance(s) in the server
                [cmdletbinding()]
                Param
                    (   # How often do you want to collect data (in second)?(15)
                        [Parameter(Mandatory=$true, 
                                   ValueFromPipeline=$true,
                                   HelpMessage="Enter The ServerNames Separated by Comma")]
                        $MachineName = "sarchost1063"
                        # How often do you want to collect data (in second)?(15)
                    )

                $b=@()
                foreach ($mname in $MachineName)

                {
                $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$mname)
                $regKey= $reg.OpenSubKey("SOFTWARE\\Microsoft\\Microsoft SQL Server\\Instance Names\\SQL" )
                $Instances = $regkey.GetValueNames()

                if ($Instances -ne $null) {
                                #Check to see is server has SQL Server install
                                #Define databases that need to be exclude 
                                $SystemDatabases = @("master", "msdb", "model", ,"tempdb", "mssqlsystemresource", "_total")

                                #Define counters
                                $counterdata = ":Databases(*)\Data File(s) Size (KB)"
                                $counterlog = ":Databases(*)\Log File(s) Size (KB)"
                                $DBSize = $null
                                foreach ($Instance in $Instances) {
                                                if ($Instance -eq "MSSQLSERVER") {$SQLServer = "SQLServer"} else {$SQLServer = "MSSQL`$" + $Instance}
                                                Write-Output $Instance
                                                $data = "\" + $SQLServer + $counterdata
                                                $log = "\" + $SQLServer + $counterlog
                                                $counter = @($data, $log)
                                                $DBDataFile = Get-Counter -Counter $counter -MaxSamples 1
                                                $DBDataFile = $DBDataFile.CounterSamples | ? {$SystemDatabases -notcontains $_.InstanceName } 
                                                $DBDataFile = $DBDataFile | select @{name="DatabaseName"; expression={$_.InstanceName}},@{name="Path"; expression={$_.path}},@{name="Data Size (MB)"; Expression={$_.CookedValue/1000}}

                                                #NOTE - If there is no database for the instance file will not generate.
                                                if ($DBDataFile -ne $null) {if ($DBSize -eq $null) { $DBSize = $DBDataFile} else {$DBSize = $DBSize + $DBDataFile}}
                                }

                                $b+=$dbsize



                }

                }
                return $b
                }

当我针对一组机器名执行此脚本时,我只得到基本服务器的输出,有人可以帮忙吗?

我目前登录到win2k8sql12b并运行win2k8sql12a的函数作为参数但是当我这样做时它给了我输出win2k8sql12b

PS C:\Users\administrator.SEA-TM> GetDBSize -MachineName win2k8sql12a | fl *
MSSQLSERVER


DatabaseName   : win2k8sql12b_delme
Path           : \\win2k8sql12b\sqlserver:databases(win2k8sql12b_delme)\data file(s) size (kb)
Data Size (MB) : 4.096

DatabaseName   : tempdb
Path           : \\win2k8sql12b\sqlserver:databases(tempdb)\data file(s) size (kb)
Data Size (MB) : 8.192

DatabaseName   : win2k8sql12b_delme
Path           : \\win2k8sql12b\sqlserver:databases(win2k8sql12b_delme)\log file(s) size (kb)
Data Size (MB) : 1.016

DatabaseName   : tempdb
Path           : \\win2k8sql12b\sqlserver:databases(tempdb)\log file(s) size (kb)
Data Size (MB) : 0.76



PS C:\Users\administrator.SEA-TM> GetDBSize -MachineName win2k8sql12b | fl *
MSSQLSERVER


DatabaseName   : win2k8sql12b_delme
Path           : \\win2k8sql12b\sqlserver:databases(win2k8sql12b_delme)\data file(s) size (kb)
Data Size (MB) : 4.096

DatabaseName   : tempdb
Path           : \\win2k8sql12b\sqlserver:databases(tempdb)\data file(s) size (kb)
Data Size (MB) : 8.192

DatabaseName   : win2k8sql12b_delme
Path           : \\win2k8sql12b\sqlserver:databases(win2k8sql12b_delme)\log file(s) size (kb)
Data Size (MB) : 1.016

DatabaseName   : tempdb
Path           : \\win2k8sql12b\sqlserver:databases(tempdb)\log file(s) size (kb)
Data Size (MB) : 0.76



PS C:\Users\administrator.SEA-TM>

0 个答案:

没有答案