0

我面临这个功能的问题。

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

0 回答 0