From 695341d5f83196ae16d3876cf93058f1650d0d7c Mon Sep 17 00:00:00 2001 From: Andrew Wickham Date: Thu, 15 Mar 2018 10:36:32 -0400 Subject: [PATCH 01/10] Removing unused functions --- functions/Get-DbrDiskSpace.ps1 | 127 --------------------------- functions/Get-DbrInfo.ps1 | 146 -------------------------------- functions/Get-DbrNewJob.ps1 | 86 ------------------- functions/Get-DbrServerLoad.ps1 | 95 --------------------- functions/Get-DbrSqlVersion.ps1 | 84 ------------------ 5 files changed, 538 deletions(-) delete mode 100644 functions/Get-DbrDiskSpace.ps1 delete mode 100644 functions/Get-DbrInfo.ps1 delete mode 100644 functions/Get-DbrNewJob.ps1 delete mode 100644 functions/Get-DbrServerLoad.ps1 delete mode 100644 functions/Get-DbrSqlVersion.ps1 diff --git a/functions/Get-DbrDiskSpace.ps1 b/functions/Get-DbrDiskSpace.ps1 deleted file mode 100644 index bda8a85..0000000 --- a/functions/Get-DbrDiskSpace.ps1 +++ /dev/null @@ -1,127 +0,0 @@ -Function Get-DiskSpace -{ -<# -.SYNOPSIS -Displays Disk information for all local drives on a server - -.DESCRIPTION -Returns a custom object with Server name, name of disk, label of disk, total size, free size and percent free. - -.PARAMETER ComputerName -The SQL Server (or server in general) that you're connecting to. The -SqlServer parameter also works. - -.PARAMETER Unit -Display the disk space information in a specific unit. Valid values incldue 'KB', 'MB', 'GB', 'TB', and 'PB'. Default is GB. - -.NOTES -dbareports PowerShell module (https://dbareports.io, clemaire@gmail.com) -Copyright (C) 2016 Rob Sewell - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . - -.LINK -https://dbareports.io/functions/Get-DiskSpace - -.EXAMPLE -Get-DiskSpace -ComputerName sqlserver2014a - -Shows disk space for sqlserver2014a in GB - -.EXAMPLE -Get-DiskSpace -ComputerName sqlserver2014a -Unit TB - -Shows disk space for sqlserver2014a in TB - -.EXAMPLE -Get-DiskSpace -ComputerName server1, server2, server3 -Unit MB - -Returns a custom object filled with information for server1, server2 and server3, in MB - -#> - [CmdletBinding(SupportsShouldProcess = $true)] - Param ( - [Alias("ServerInstance", "SqlInstance", "SqlServer")] - [string[]]$ComputerName, - [ValidateSet('KB', 'MB', 'GB', 'TB', 'PB')] - [String]$Unit = "GB" - ) - - BEGIN - { - Function Get-AllDiskSpace - { - - $measure = "1$unit" - $query = "Select SystemName, Name, DriveType, FileSystem, FreeSpace, Capacity, Label from Win32_Volume where DriveType = 2 or DriveType = 3" - - $alldisks = @() - - try - { - $ipaddr = (Test-Connection $server -count 1).Ipv4Address | Select-Object -First 1 - $disks = Get-WmiObject -ComputerName $ipaddr -Query $query | Sort-Object -Property Name - } - catch - { - Write-Exception $_ - throw "Can't connect to $server" - } - - foreach ($disk in $disks) - { - - if (!$disk.name.StartsWith("\\")) - { - $total = "{0:n2}" -f ($disk.Capacity/$measure) - $free = "{0:n2}" -f ($disk.Freespace/$measure) - $percentfree = "{0:n2}" -f (($disk.Freespace / $disk.Capacity) * 100) - - $alldisks += [PSCustomObject]@{ - Server = $server - Name = $disk.Name - Label = $disk.Label - "SizeIn$unit" = $total - "FreeIn$unit" = $free - PercentFree = $percentfree - } - } - } - return $alldisks - } - - $collection = New-Object System.Collections.ArrayList - } - - PROCESS - { - foreach ($server in $ComputerName) - { - $data = Get-AllDiskSpace $server - - if ($data.Count -gt 1) - { - $data.GetEnumerator() | ForEach-Object { $null = $collection.Add($_) } - } - else - { - $null = $collection.Add($data) - } - } - } - - END - { - return $collection - } -} \ No newline at end of file diff --git a/functions/Get-DbrInfo.ps1 b/functions/Get-DbrInfo.ps1 deleted file mode 100644 index e19cb65..0000000 --- a/functions/Get-DbrInfo.ps1 +++ /dev/null @@ -1,146 +0,0 @@ -<# -/* - -Various queries for getting information out of the DBA Database -Connect to Server hosting DBA Database - - -Use - -where IL.Inactive = 0 - -to only get active instances - -*/ - - --- Generic infomration about Servers and locations and environments - -Select IL.ServerName, - IL.InstanceName, - IL.Environment, - IL.location -FROM dbo.InstanceList IL --- where IL.Environment = 'Prod' --- Where IL.Location = 'Bolton' - --- Generic infromation about servers and clients - -Select - DISTINCT C.ClientName, - IL.ServerName -FROM dbo.InstanceList IL -JOIN -dbo.ClientDatabaseLookup CDL -ON -CDL.InstanceID = IL.InstanceID -JOIN dbo.Clients C -ON c.ClientID = cdl.ClientID -WHERE C.ClientName <> 'DBA-Team' ---- AND C.ClientName = '' -- AND IL.ServerName = '' -group by C.ClientName ,ServerName - - --- Generic SQL Instance Information Specifics can be picked from the SQLInfo table as required - The date checked value will show how up to date the data is - -Select IL.ServerName, - IL.InstanceName, - IL.Environment, - IL.location, - SI.* -FROM dbo.InstanceList IL -JOIN info.SQLInfo SI -ON SI.instanceid = IL.InstanceID ---- Use the relevant where clause you require here - -order by SI.ServerName - - --- Generic Windows Information Specifics can be picked from the ServerOSInfo table as required - The date checked value will show how up to date the data is -Select - SOI.* -FROM info.serverosinfo SOI - --- Pick your required where clause here - --- Generic Database Information Specifics can be picked from the Databases table as required - The date checked value will show how up to date the data is - -Select IL.ServerName, - IL.InstanceName, - IL.Environment, - IL.location, - D.* -FROM dbo.InstanceList IL -JOIN info.Databases D -ON D.InstanceID = IL.InstanceID -where D.Name = 'Name of Database 175' - --- pick your required where clause here - - ----- Job Detail INformation is in the AgentJobDetail table this holds infomration about every job that ran -Select IL.ServerName, - IL.InstanceName, - IL.Environment, - IL.location, - AJD.* -FROM dbo.InstanceList IL -JOIN info.AgentJobDetail AJD -ON AJD.InstanceID = IL.InstanceID - --- pick your required where clause here - Think about LastRuntime or outcome or server or job name -WHERE AJD.InstanceID -IN - -(Select IL.InstanceID -FROM dbo.InstanceList IL -WHERE IL.Environment = 'Prod' ---- This clause is looking for Prod Environment Servers with Jobs that have Newport in the name -and AJD.JobName LIKE '%Index%') - -and AJD.LastRunTime > DATEADD(day,-1,GETDATE()) --- That finished since yesterday -ORDER by AJD.LastRunTime desc - - ----- Job Server INformation is in the AgentJobServer table this holds a roll up of each days job records - -Select IL.ServerName, - IL.InstanceName, - IL.Environment, - IL.location, - AJS.* -FROM dbo.InstanceList IL -JOIN info.AgentJobServer AJS -ON AJS.InstanceID = IL.InstanceID - --- pick your required where clause here - Think about LastRuntime or outcome or server or job name -WHERE AJS.InstanceID -IN - -(Select IL.InstanceID -FROM dbo.InstanceList IL -WHERE IL.Environment = 'Prod' ---- This clause is looking for Prod Environment Servers in Bolton -and IL.Location = 'Bolton') - -and AJS.Date > DATEADD(day,-1,GETDATE()) --- That were collected since yesterday -ORDER by IL.ServerName - - --- Find the server a database is on - -SELECT il.ServerName, - il.InstanceName, - il.Port, - d.Name, - il.Environment, - c.ClientName, - cdl.Notes - FROM info.Databases d - join dbo.InstanceList il - on il.InstanceID = d.InstanceID - join dbo.ClientDatabaseLookup cdl - on d.DatabaseID = cdl.DatabaseID - join dbo.clients c - on cdl.ClientID = c.ClientID - where d.name LIKE'%172%' - AND IL.InActive = 0 - - #> \ No newline at end of file diff --git a/functions/Get-DbrNewJob.ps1 b/functions/Get-DbrNewJob.ps1 deleted file mode 100644 index 9e4d47c..0000000 --- a/functions/Get-DbrNewJob.ps1 +++ /dev/null @@ -1,86 +0,0 @@ -Function Get-DbrNewJob -{ -<# -.SYNOPSIS - - -.DESCRIPTION -Returns the JobName, Category, Description, Enabled or not, Status, LastRunTime and Outcome of agent jobs created in the last x hours - -.PARAMETER Hours -Return jobs created within this number of hours - -.NOTES -dbareports PowerShell module (https://dbareports.io, SQLDBAWithABeard.com) -Copyright (C) 2016 Rob Sewell - -This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License along with this program. If not, see . - -.LINK -https://dbareports.io/functions/Get-DbrNewJob - -.EXAMPLE -Get-DbrNewJob - -Returns the JobName, Category, Description, Enabled or not, Status, LastRunTime and Outcome of agent jobs created in the last 24 hours - -.EXAMPLE -Get-DbrNewJob -hours 5 - -Returns the JobName, Category, Description, Enabled or not, Status, LastRunTime and Outcome of agent jobs created in the last 5 hours -#> - [CmdletBinding()] - Param ( - [int]$Hours = 24 - ) - - DynamicParam - { - Get-Config - if ($script:SqlServer) { return (Get-ParamSqlDbrJobs -SqlServer $script:SqlServer -SqlCredential $script:SqlCredential) } - } - - BEGIN - { - Get-Config - $SqlServer = $script:SqlServer - $InstallDatabase = $script:InstallDatabase - $SqlCredential = $script:SqlCredential - - if ($SqlServer.length -eq 0) - { - throw "No config file found. Have you installed dbareports? Please run Install-DbaReports or Install-DbaReportsClient" - } - - If ($Force -eq $true) { $ConfirmPreference = 'None' } - - $sourceserver = Connect-SqlServer -SqlServer $sqlserver -SqlCredential $SqlCredential - $source = $sourceserver.DomainInstanceName - } - - PROCESS - { - If ($Pscmdlet.ShouldProcess($sqlserver, "Doing this")) - { - #Whatever - } - - $sql = "SELECT b.Name as SqlServer, a.JobName, a.Category, - a.Description, a.IsEnabled, a.Status, a.LastRunTime, a.Outcome - FROM [info].[AgentJobDetail] a JOIN [dbo].[InstanceList] b - on a.InstanceID = b.InstanceID - WHERE [DateCreated] >= DATEADD(HOUR, -$hours, GETDATE())" - - $datatable = $sourceserver.Databases[$InstallDatabase].ExecuteWithResults($sql).Tables - return $datatable - } - - END - { - $sourceserver.ConnectionContext.Disconnect() - } -} \ No newline at end of file diff --git a/functions/Get-DbrServerLoad.ps1 b/functions/Get-DbrServerLoad.ps1 deleted file mode 100644 index c63a3cb..0000000 --- a/functions/Get-DbrServerLoad.ps1 +++ /dev/null @@ -1,95 +0,0 @@ -<#/* -Adding a new server to the DBA Database - - -Enter the corect values against the variables - -The errors will tell you what you have done wrong - -AUTHOR - ROb Sewell -DATE - 04/05/2015 - Initial - - 10/08/2015 - Added some failsafes !! - - 18/08/2015 - Added non contactabel and inactive to the query - -- 25/092015 jw - fixed typo NotContactable instead of NonContactable - -*/ - - - - -Use [DBADatabase] -GO - - -DECLARE @Server nvarchar(50) = '' --- ENTER SERVER HERE -DECLARE @InstanceName nvarchar(50) = 'MSSQLSERVER' --- ENTER INSTANCE NAME HERE EVEN IF DEFAULT -DECLARE @Port int = 1433 --- ENTER Port Here EVEN IF DEFAULT -DECLARE @AG bit = 0 --- Is this an Availability group enabled instance? 1 - Yes 0 - No -DECLARE @Environment nvarchar(25) = 'PROD' --- Environment - Prod, Test, Int,PreProd,Dev etc -DECLARE @Location nvarchar(30) = '' --- Location - Data Centre 1 Data Centre 2 etc - - ---------------------------------------------------------------------------------------------------------------------- -/* - - YOU SHOULD NOT NEED TO CHANGE ANYTHING BELOW HERE - -*/ ---------------------------------------------------------------------------------------------------------------------- - - - - -DECLARE @Message nvarchar(150) - -declare @InstanceId int - --- Ensure Instance does not already exist -IF EXISTS -( -SELECT [InstanceID] - FROM [DBADatabase].[dbo].[InstanceList] - Where [ServerName] = @Server -AND [InstanceName] = @InstanceName - ) -BEGIN -DECLARE @P nvarchar(10) = CAST(@Port as nvarchar(10)) -SET @Message = @Server + '\' + @InstanceName + ',' + @P + ' already exists in the DBA Database'; -THROW 50000, @Message, 1 -END - -INSERT INTO [dbo].[InstanceList] - ([ServerName] - ,[InstanceName] - ,[Port] - ,[AG] - ,Inactive - ,Environment - ,Location - ,NotContactable) - VALUES - (@Server - ,@InstanceName - ,@Port - ,@AG - ,0 - ,@Environment - ,@Location - ,0 ---------------------Unless this servers in on a network that cannot be contacted in which case this is a 1 - ) - -set @InstanceId = SCOPE_IDENTITY() - -insert into dbo.InstanceScriptLookup ( - InstanceID, - ScriptID, - NeedsUpdate -) - select - @InstanceId, - s.ScriptID, - 0 -- This will update all scripts if set to 1 - DO NOT DO THIS - from dbo.ScriptList as s - -GO -#> diff --git a/functions/Get-DbrSqlVersion.ps1 b/functions/Get-DbrSqlVersion.ps1 deleted file mode 100644 index ad9e9dd..0000000 --- a/functions/Get-DbrSqlVersion.ps1 +++ /dev/null @@ -1,84 +0,0 @@ -Function Get-DbrSqlVersion -{ -<# -.SYNOPSIS - - -.DESCRIPTION - - -.PARAMETER - - -.PARAMETER - - -.PARAMETER - -.NOTES -dbareports PowerShell module (https://dbareports.io, SQLDBAWithABeard.com) -Copyright (C) 2016 Rob Sewell - -This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License along with this program. If not, see . - -.LINK -https://dbareports.io/functions/Get-DbrSqlVersion - -.EXAMPLE -Verb-SqlNoun -Copies all policies and conditions from sqlserver2014a to sqlcluster, using Windows credentials. - - -.EXAMPLE -Verb-SqlNoun -WhatIf -Shows what would happen if the command were executed. - -.EXAMPLE -Verb-SqlNoun -Policy 'xp_cmdshell must be disabled' -Does this -#> - [CmdletBinding(SupportsShouldProcess = $true)] - Param ( - [Alias("ServerInstance", "SqlInstance")] - [object]$SqlServer, - [object]$SqlCredential, - [string]$InstallDatabase, - [string]$Something, - [switch]$SomethingElse - ) - - - DynamicParam { if ($sqlserver) { return Get-ParamSqlDatabases -SqlServer $sqlserver -SqlCredential $SqlCredential } } - - BEGIN - { - - $sourceserver = Connect-SqlServer -SqlServer $sqlserver -SqlCredential $SqlCredential - $source = $sourceserver.DomainInstanceName - - $Databases = $psboundparameters.Databases - } - - PROCESS - { - # We make three things total - $pkids = Get-SqlPkValue $table $values # think (IN) - $pkid = Get-SqlPkValue $table $value # think = - - # or a dataset where we do fancy stuff - $pktable = Get-SqlPkTable $table - - $sql = "select * from sys.master_files" - $datatable = $sourceserver[$InstallDatabase].ExecuteWithResults($sql).Tables - } - - END - { - $sourceserver.ConnectionContext.Disconnect() - - } -} \ No newline at end of file From c17bf25ac8aacc463bb60683a8cb45ef844bfaa9 Mon Sep 17 00:00:00 2001 From: Andrew Wickham Date: Thu, 15 Mar 2018 10:36:56 -0400 Subject: [PATCH 02/10] Updated FunctionsToExport to remove unused functions --- dbareports.psd1 | 54 ++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/dbareports.psd1 b/dbareports.psd1 index 6c5bfc2..b815a96 100644 --- a/dbareports.psd1 +++ b/dbareports.psd1 @@ -8,64 +8,64 @@ # Updated on: 19 July 2017 # @{ - + # Script module or binary module file associated with this manifest. RootModule = 'dbareports.psm1' - + # Version number of this module. ModuleVersion = '0.9.0' - + # ID used to uniquely identify this module GUID = '654a8346-35f1-4592-a1b5-0ee472fab074' - + # Author of this module Author = 'SQL Collaborative - Initial Author Rob Sewell' - + # Company or vendor of this module CompanyName = 'SQL Collaborative' - + # Copyright statement for this module Copyright = '2016 Rob Sewell' - + # Description of the functionality provided by this module Description = 'Dopest dba dashboards ever' - + # Minimum version of the Windows PowerShell engine required by this module PowerShellVersion = '3.0' - + # Name of the Windows PowerShell host required by this module PowerShellHostName = '' - + # Minimum version of the Windows PowerShell host required by this module PowerShellHostVersion = '' - + # Minimum version of the .NET Framework required by this module DotNetFrameworkVersion = '' - + # Minimum version of the common language runtime (CLR) required by this module CLRVersion = '' - + # Processor architecture (None, X86, Amd64, IA64) required by this module ProcessorArchitecture = '' - + # Modules that must be imported into the global environment prior to importing this module RequiredModules = @() - + # Assemblies that must be loaded prior to importing this module RequiredAssemblies= @('Microsoft.SqlServer.Smo','Microsoft.SqlServer.SmoExtended') - + # Script files () that are run in the caller's environment prior to importing this module ScriptsToProcess = @() - + # Type files (xml) to be loaded when importing this module TypesToProcess = @() - + # Format files (xml) to be loaded when importing this module FormatsToProcess = @() - + # Modules to import as nested modules of the module specified in ModuleToProcess NestedModules = @() - + # Functions to export from this module FunctionsToExport = @('Install-DbaReports', 'Add-DbrCredential', @@ -79,28 +79,26 @@ 'Install-DbaReportsClient', 'Set-DbrInstanceInactiveInInventory', 'Get-DbrConfig', - 'Get-DbrAgentJob', 'Get-DbrInstanceList', 'New-DbrSqlAlias', - 'Get-DbrNewJob', 'Write-Log' ) - + # Cmdlets to export from this module CmdletsToExport = '*' - + # Variables to export from this module VariablesToExport = '*' - + # Aliases to export from this module AliasesToExport = 'Update-DbaReportsClient' - + # List of all modules packaged with this module ModuleList = @() - + # List of all files packaged with this module FileList = '' - + PrivateData = @{ # PSData is module packaging and gallery metadata embedded in PrivateData # It's for rebuilding PowerShellGet (and PoshCode) NuGet-style packages From 134c308b7d5aa243579f94f17888c5689207aa09 Mon Sep 17 00:00:00 2001 From: Andrew Wickham Date: Thu, 15 Mar 2018 12:28:23 -0400 Subject: [PATCH 03/10] Converted to use dbatools Get-DbaDiskSpace --- setup/powershell/DiskSpace.ps1 | 129 ++++++++++++++------------------- 1 file changed, 56 insertions(+), 73 deletions(-) diff --git a/setup/powershell/DiskSpace.ps1 b/setup/powershell/DiskSpace.ps1 index d39798a..d0fa246 100644 --- a/setup/powershell/DiskSpace.ps1 +++ b/setup/powershell/DiskSpace.ps1 @@ -1,11 +1,11 @@ <# -.SYNOPSIS +.SYNOPSIS This Script will check all of the instances in the InstanceList and gather the Windows Info and save to the Info.ServerInfo table -.DESCRIPTION +.DESCRIPTION This Script will check all of the instances in the InstanceList and gather the Windows Info and save to the Info.ServerInfo table -.NOTES +.NOTES dbareports PowerShell module (https://dbareports.io, SQLDBAWithABeard.com) Copyright (C) 2016 Rob Sewell @@ -32,7 +32,7 @@ BEGIN $currentdir = Split-Path -Parent $MyInvocation.MyCommand.Definition . "$currentdir\shared.ps1" . "$currentdir\Write-Log.ps1" - # Create Log File + # Create Log File $Date = Get-Date -Format yyyyMMdd_HHmmss $LogFilePath = $LogFileFolder + '\' + 'dbareports_DiskSpace_' + $Date + '.txt' try @@ -48,12 +48,12 @@ BEGIN $table = "info.DiskSpace" $schema = $table.Split(".")[0] $tablename = $table.Split(".")[1] - + # Connect to dbareports server try { Write-Log -path $LogFilePath -message "Connecting to $sqlserver" -level info - $sourceserver = Connect-SqlServer -SqlServer $sqlserver -SqlCredential $SqlCredential -ErrorAction Stop + $sourceserver = dbatools\Connect-DbaSqlServer -SqlServer $sqlserver -SqlCredential $SqlCredential -ErrorAction Stop } catch { @@ -65,13 +65,13 @@ BEGIN try { Write-Log -path $LogFilePath -message "Intitialising Datatable" -level info - Initialize-DataTable -ErrorAction Stop + Initialize-DataTable -ErrorAction Stop } catch { Write-Log -path $LogFilePath -message "Failed to initialise Data Table - $_" -level Error } - + } PROCESS @@ -84,7 +84,7 @@ PROCESS $sql = "SELECT DISTINCT ServerID, ServerName FROM dbo.instancelist" $sqlservers = $sourceserver.Databases[$InstallDatabase].ExecuteWithResults($sql).Tables[0] Write-Log -path $LogFilePath -message "Got the list of servers from the dbareports database" -level info - + } catch { @@ -104,13 +104,13 @@ PROCESS { Write-Log -path $LogFilePath -message "Can't get server list from $InstallDatabase on $($sourceserver.name). - $_" -level Error } - + foreach ($server in $sqlservers) { $ServerName = $server.ServerName $ServerId = $server.ServerId $date = Get-Date - + Write-Log -path $LogFilePath -message "Processing $ServerName" -level info try { @@ -120,90 +120,73 @@ PROCESS { $ipaddr = Resolve-IpAddress $servername } - + if ($ipaddr -eq $null) { Write-Log -path $LogFilePath -message "Could not resolve IP address for $ServerName. Moving on." -level info Write-Log -path $LogFilePath -message "Tried Resolve-SqlIpAddress $ServerName and Resolve-IpAddress $servername" continue } - - try - { - $query = "Select SystemName, Name, DriveType, FileSystem, FreeSpace, Capacity, Label, BlockSize from Win32_Volume where DriveType = 2 or DriveType = 3" - $disks = Get-WmiObject -ComputerName $ipaddr -Query $query | Sort-Object -Property Name - } - catch - { - Write-Log -path $LogFilePath -message "Could not connect to WMI on $ServerName. " -level Warn - continue - } - + + $disks = dbatools\Get-DbaDiskSpace -ComputerName $ipaddr + foreach ($disk in $disks) { - $diskname = $disk.name - if (!$diskname.StartsWith("\\")) + $update = $true + $row = $table | Where-Object { $_.DiskName -eq $DiskName -and $_.ServerId -eq $ServerId} | Sort-Object -Property Date | Select-Object -First 1 + $key = $row.DiskSpaceID + + if ($key.count -eq 0) + { + $update = $false + } + + # to see results as they come in, skip $null= + try + { + $Null = $datatable.Rows.Add( + $key, + $Date, + $ServerId, + $disk.Name, + $disk.Label, + $disk.SizeInGB, + $disk.FreeInGB, + $disk.PercentFree, + $Update) + + Write-Log -path $LogFilePath -message "Adding $($disk.Name) for $ServerName" -level info + } + catch { - $update = $true - $row = $table | Where-Object { $_.DiskName -eq $DiskName -and $_.ServerId -eq $ServerId} | Sort-Object -Property Date | Select-Object -First 1 - $key = $row.DiskSpaceID - - if ($key.count -eq 0) - { - $update = $false - } - - $total = "{0:f2}" -f ($disk.Capacity/1gb) - $free = "{0:f2}" -f ($disk.Freespace/1gb) - $percentfree = "{0:n0}" -f (($disk.Freespace / $disk.Capacity) * 100) - - # to see results as they come in, skip $null= - try - { - $Null = $datatable.Rows.Add( - $key, - $Date, - $ServerId, - $diskname, - $disk.Label, - $total, - $free, - $percentfree, - $Update) - - Write-Log -path $LogFilePath -message "Adding $diskname for $ServerName" -level info - } - catch - { - Write-Log -path $LogFilePath -message "Failed to add Job to datatable - $_" -level Error - Write-Log -path $LogFilePath -message "Data = $key, - $Date, - $ServerId, - $diskname, - $($disk.Label), - $total, - $free, - $percentfree, - $Update" -level Warn - continue - } + Write-Log -path $LogFilePath -message "Failed to add Job to datatable - $_" -level Error + Write-Log -path $LogFilePath -message "Data = $key, + $Date, + $ServerId, + $($disk.Name), + $($disk.Label), + $($disk.SizeInGB), + $($disk.FreeInGB), + $($disk.PercentFree), + $Update" -level Warn + continue } } } - + $rowcount = $datatable.Rows.Count - + if ($rowcount -eq 0) { Write-Log -path $LogFilePath -message "No rows returned. No update required." -level info continue } - + try { Write-Log -path $LogFilePath -message "Attempting Import of $rowcount row(s)" -level info - Write-Tvp -ErrorAction Stop + Write-Tvp -ErrorAction Stop Write-Log -path $LogFilePath -message "Successfully Imported $rowcount row(s) of DiskSpace into the $InstallDatabase on $($sourceserver.name)" -level info } catch From 62fbd6a4063778215035bd84d70a4cb5beb825e4 Mon Sep 17 00:00:00 2001 From: Andrew Wickham Date: Thu, 15 Mar 2018 12:29:34 -0400 Subject: [PATCH 04/10] More conversion to dbatools --- setup/powershell/DiskSpace.ps1 | 107 ++++++++++++--------------------- 1 file changed, 37 insertions(+), 70 deletions(-) diff --git a/setup/powershell/DiskSpace.ps1 b/setup/powershell/DiskSpace.ps1 index d0fa246..3826c4d 100644 --- a/setup/powershell/DiskSpace.ps1 +++ b/setup/powershell/DiskSpace.ps1 @@ -53,25 +53,14 @@ BEGIN try { Write-Log -path $LogFilePath -message "Connecting to $sqlserver" -level info - $sourceserver = dbatools\Connect-DbaSqlServer -SqlServer $sqlserver -SqlCredential $SqlCredential -ErrorAction Stop + $sourceserver = dbatools\Connect-DbaInstance -SqlServer $sqlserver -SqlCredential $SqlCredential -ErrorAction Stop } catch { Write-Log -path $LogFilePath -message "Failed to connect to $sqlserver - $_" -level Error } - # Get columns automatically from the table on the SQL Server - # and creates the necessary $script:datatable with it - try - { - Write-Log -path $LogFilePath -message "Intitialising Datatable" -level info - Initialize-DataTable -ErrorAction Stop - } - catch - { - Write-Log -path $LogFilePath -message "Failed to initialise Data Table - $_" -level Error - } - + $data = @() } PROCESS @@ -112,69 +101,31 @@ PROCESS $date = Get-Date Write-Log -path $LogFilePath -message "Processing $ServerName" -level info - try - { - $ipaddr = Resolve-SqlIpAddress $ServerName - } - catch - { - $ipaddr = Resolve-IpAddress $servername - } - - if ($ipaddr -eq $null) - { - Write-Log -path $LogFilePath -message "Could not resolve IP address for $ServerName. Moving on." -level info - Write-Log -path $LogFilePath -message "Tried Resolve-SqlIpAddress $ServerName and Resolve-IpAddress $servername" - continue - } - - $disks = dbatools\Get-DbaDiskSpace -ComputerName $ipaddr + $disks = dbatools\Get-DbaDiskSpace -ComputerName $ServerName foreach ($disk in $disks) { - $update = $true - $row = $table | Where-Object { $_.DiskName -eq $DiskName -and $_.ServerId -eq $ServerId} | Sort-Object -Property Date | Select-Object -First 1 - $key = $row.DiskSpaceID - - if ($key.count -eq 0) - { - $update = $false - } - - # to see results as they come in, skip $null= - try - { - $Null = $datatable.Rows.Add( - $key, - $Date, - $ServerId, - $disk.Name, - $disk.Label, - $disk.SizeInGB, - $disk.FreeInGB, - $disk.PercentFree, - $Update) - - Write-Log -path $LogFilePath -message "Adding $($disk.Name) for $ServerName" -level info - } - catch - { - Write-Log -path $LogFilePath -message "Failed to add Job to datatable - $_" -level Error - Write-Log -path $LogFilePath -message "Data = $key, - $Date, - $ServerId, - $($disk.Name), - $($disk.Label), - $($disk.SizeInGB), - $($disk.FreeInGB), - $($disk.PercentFree), - $Update" -level Warn - continue + # TODO: Figure out how to merge/update if already exists for that day + + $size = [decimal]("{0:n2}" -f $disk.SizeInGB) + $free = [decimal]("{0:n2}" -f $disk.FreeInGB) + $percent = [decimal]("{0:n2}" -f $disk.PercentFree) + + $data += [PSCustomObject]@{ + 'DiskSpaceID' = $null + 'Date' = $Date + 'ServerID' = $ServerId + 'DiskName' = $disk.Name + 'Label' = $disk.Label + 'Capacity' = $size + 'FreeSpace' = $free + 'Percentage' = $percent } } - } + $datatable = dbatools\ConvertTo-DbaDataTable -InputObject $data + $rowcount = $datatable.Rows.Count if ($rowcount -eq 0) @@ -186,7 +137,22 @@ PROCESS try { Write-Log -path $LogFilePath -message "Attempting Import of $rowcount row(s)" -level info - Write-Tvp -ErrorAction Stop + + $params = @{ + 'SqlInstance' = $SqlServer + 'Database' = $InstallDatabase + 'Schema' = 'info' + 'Table' = 'DiskSpace' + 'InputObject' = $datatable + 'EnableException' = $true + } + + if ($SqlCredential) { + $params['SqlCredential'] = $SqlCredential + } + + dbatools\Write-DbaDataTable @params + Write-Log -path $LogFilePath -message "Successfully Imported $rowcount row(s) of DiskSpace into the $InstallDatabase on $($sourceserver.name)" -level info } catch @@ -198,4 +164,5 @@ PROCESS END { Write-Log -path $LogFilePath -message "DiskSpace Finished" + $sourceserver.ConnectionContext.Disconnect() } \ No newline at end of file From df66bbe972ed24b3dbf21c89355f4a6e0e8f6ce8 Mon Sep 17 00:00:00 2001 From: Andrew Wickham Date: Thu, 15 Mar 2018 12:51:03 -0400 Subject: [PATCH 05/10] Clean up unused code --- setup/powershell/DiskSpace.ps1 | 78 ++++++++++++++-------------------- 1 file changed, 31 insertions(+), 47 deletions(-) diff --git a/setup/powershell/DiskSpace.ps1 b/setup/powershell/DiskSpace.ps1 index 3826c4d..3ecba2a 100644 --- a/setup/powershell/DiskSpace.ps1 +++ b/setup/powershell/DiskSpace.ps1 @@ -32,6 +32,7 @@ BEGIN $currentdir = Split-Path -Parent $MyInvocation.MyCommand.Definition . "$currentdir\shared.ps1" . "$currentdir\Write-Log.ps1" + # Create Log File $Date = Get-Date -Format yyyyMMdd_HHmmss $LogFilePath = $LogFileFolder + '\' + 'dbareports_DiskSpace_' + $Date + '.txt' @@ -44,11 +45,6 @@ BEGIN Write-error "Failed to create Log File at $LogFilePath" } - # Specify table name that we'll be inserting into - $table = "info.DiskSpace" - $schema = $table.Split(".")[0] - $tablename = $table.Split(".")[1] - # Connect to dbareports server try { @@ -65,13 +61,21 @@ BEGIN PROCESS { - $DateChecked = Get-Date - try { Write-Log -path $LogFilePath -message "Getting a list of servers from the dbareports database" -level info - $sql = "SELECT DISTINCT ServerID, ServerName FROM dbo.instancelist" - $sqlservers = $sourceserver.Databases[$InstallDatabase].ExecuteWithResults($sql).Tables[0] + + $params = @{ + 'ServerInstance' = $SqlServer + 'Database' = $InstallDatabase + 'Query' = "SELECT DISTINCT ServerID, ServerName FROM dbo.instancelist" + } + + if ($SqlCredential) { + $params['Credential'] = $SqlCredential + } + $sqlservers = Invoke-Sqlcmd2 @params + Write-Log -path $LogFilePath -message "Got the list of servers from the dbareports database" -level info } @@ -81,46 +85,26 @@ PROCESS break } - # Get list of all servers already in the database - try - { - Write-Log -path $LogFilePath -message "Getting a list of servers from the dbareports database" -level info - $sql = "SELECT a.DiskSpaceID, a.DiskName, b.ServerID, b.ServerName FROM $table a JOIN info.Serverinfo b on a.ServerId = b.ServerId" - $table = $sourceserver.Databases[$InstallDatabase].ExecuteWithResults($sql).Tables[0] - Write-Log -path $LogFilePath -message "Got the list of servers from the dbareports database" -level info - } - catch - { - Write-Log -path $LogFilePath -message "Can't get server list from $InstallDatabase on $($sourceserver.name). - $_" -level Error - } - foreach ($server in $sqlservers) { - $ServerName = $server.ServerName - $ServerId = $server.ServerId - $date = Get-Date - - Write-Log -path $LogFilePath -message "Processing $ServerName" -level info - - $disks = dbatools\Get-DbaDiskSpace -ComputerName $ServerName - foreach ($disk in $disks) - { - # TODO: Figure out how to merge/update if already exists for that day - - $size = [decimal]("{0:n2}" -f $disk.SizeInGB) - $free = [decimal]("{0:n2}" -f $disk.FreeInGB) - $percent = [decimal]("{0:n2}" -f $disk.PercentFree) - - $data += [PSCustomObject]@{ - 'DiskSpaceID' = $null - 'Date' = $Date - 'ServerID' = $ServerId - 'DiskName' = $disk.Name - 'Label' = $disk.Label - 'Capacity' = $size - 'FreeSpace' = $free - 'Percentage' = $percent - } + Write-Log -path $LogFilePath -message "Processing $($server.ServerName)" -level info + + try { + $disks = dbatools\Get-DbaDiskSpace -ComputerName $server.ServerName -EnableException + + $data += $disks | Select-Object -Property @( + @{Name = 'DiskSpaceID'; Expression = { $null }}, + @{Name = 'Date'; Expression = { Get-Date }}, + @{Name = 'ServerID'; Expression = { $server.ServerId }}, + @{Name = 'DiskName'; Expression = { $_.Name }}, + @{Name = 'Label'; Expression = { $_.Label }}, + @{Name = 'Capacity'; Expression = { [decimal]("{0:n2}" -f $_.SizeInGB) }}, + @{Name = 'FreeSpace'; Expression = { [decimal]("{0:n2}" -f $_.FreeInGB) }}, + @{Name = 'Percentage'; Expression = { [decimal]("{0:n2}" -f $_.PercentFree) }} + ) + } + catch { + Write-Log -Path $LogFilePath -Message " Failed to get disk space" -Level Error } } From e8791b904111ff6d3fd31629cb754275b1c87eef Mon Sep 17 00:00:00 2001 From: Andrew Wickham Date: Thu, 15 Mar 2018 13:01:07 -0400 Subject: [PATCH 06/10] Processing array of servers is handled by Get-DbaDiskSpace --- setup/powershell/DiskSpace.ps1 | 38 +++++++++++++--------------------- 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/setup/powershell/DiskSpace.ps1 b/setup/powershell/DiskSpace.ps1 index 3ecba2a..f8e5487 100644 --- a/setup/powershell/DiskSpace.ps1 +++ b/setup/powershell/DiskSpace.ps1 @@ -32,7 +32,6 @@ BEGIN $currentdir = Split-Path -Parent $MyInvocation.MyCommand.Definition . "$currentdir\shared.ps1" . "$currentdir\Write-Log.ps1" - # Create Log File $Date = Get-Date -Format yyyyMMdd_HHmmss $LogFilePath = $LogFileFolder + '\' + 'dbareports_DiskSpace_' + $Date + '.txt' @@ -55,8 +54,6 @@ BEGIN { Write-Log -path $LogFilePath -message "Failed to connect to $sqlserver - $_" -level Error } - - $data = @() } PROCESS @@ -85,27 +82,20 @@ PROCESS break } - foreach ($server in $sqlservers) - { - Write-Log -path $LogFilePath -message "Processing $($server.ServerName)" -level info - - try { - $disks = dbatools\Get-DbaDiskSpace -ComputerName $server.ServerName -EnableException - - $data += $disks | Select-Object -Property @( - @{Name = 'DiskSpaceID'; Expression = { $null }}, - @{Name = 'Date'; Expression = { Get-Date }}, - @{Name = 'ServerID'; Expression = { $server.ServerId }}, - @{Name = 'DiskName'; Expression = { $_.Name }}, - @{Name = 'Label'; Expression = { $_.Label }}, - @{Name = 'Capacity'; Expression = { [decimal]("{0:n2}" -f $_.SizeInGB) }}, - @{Name = 'FreeSpace'; Expression = { [decimal]("{0:n2}" -f $_.FreeInGB) }}, - @{Name = 'Percentage'; Expression = { [decimal]("{0:n2}" -f $_.PercentFree) }} - ) - } - catch { - Write-Log -Path $LogFilePath -Message " Failed to get disk space" -Level Error - } + try { + $data = dbatools\Get-DbaDiskSpace -ComputerName $sqlservers.ServerName -EnableException -ErrorAction 'Continue' | Select-Object -Property @( + @{Name = 'DiskSpaceID'; Expression = { $null }}, + @{Name = 'Date'; Expression = { Get-Date }}, + @{Name = 'ServerID'; Expression = { $server.ServerId }}, + @{Name = 'DiskName'; Expression = { $_.Name }}, + @{Name = 'Label'; Expression = { $_.Label }}, + @{Name = 'Capacity'; Expression = { [decimal]("{0:n2}" -f $_.SizeInGB) }}, + @{Name = 'FreeSpace'; Expression = { [decimal]("{0:n2}" -f $_.FreeInGB) }}, + @{Name = 'Percentage'; Expression = { [decimal]("{0:n2}" -f $_.PercentFree) }} + ) + } + catch { + Write-Log -Path $LogFilePath -Message $_.Message -Level Error } $datatable = dbatools\ConvertTo-DbaDataTable -InputObject $data From 8bef2c535f26827697e2431c599d86f582ca5ce1 Mon Sep 17 00:00:00 2001 From: Andrew Wickham Date: Thu, 15 Mar 2018 13:09:13 -0400 Subject: [PATCH 07/10] Invoke-Sqlcmd2 bad --- setup/powershell/DiskSpace.ps1 | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/setup/powershell/DiskSpace.ps1 b/setup/powershell/DiskSpace.ps1 index f8e5487..426f0f1 100644 --- a/setup/powershell/DiskSpace.ps1 +++ b/setup/powershell/DiskSpace.ps1 @@ -62,16 +62,8 @@ PROCESS { Write-Log -path $LogFilePath -message "Getting a list of servers from the dbareports database" -level info - $params = @{ - 'ServerInstance' = $SqlServer - 'Database' = $InstallDatabase - 'Query' = "SELECT DISTINCT ServerID, ServerName FROM dbo.instancelist" - } - - if ($SqlCredential) { - $params['Credential'] = $SqlCredential - } - $sqlservers = Invoke-Sqlcmd2 @params + $sql = "SELECT DISTINCT ServerID, ServerName FROM dbo.instancelist" + $sqlservers = $sourceserver.Databases[$InstallDatabase].ExecuteWithResults($sql).Tables[0] Write-Log -path $LogFilePath -message "Got the list of servers from the dbareports database" -level info From 42c849a49c8df479aad701e29de96e47a0ae5ed4 Mon Sep 17 00:00:00 2001 From: Andrew Wickham Date: Thu, 15 Mar 2018 13:15:20 -0400 Subject: [PATCH 08/10] Use Get-DbrInstanceList --- setup/powershell/DiskSpace.ps1 | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/setup/powershell/DiskSpace.ps1 b/setup/powershell/DiskSpace.ps1 index 426f0f1..bf3ce57 100644 --- a/setup/powershell/DiskSpace.ps1 +++ b/setup/powershell/DiskSpace.ps1 @@ -20,10 +20,10 @@ You should have received a copy of the GNU General Public License along with thi [CmdletBinding()] Param ( [Alias("ServerInstance", "SqlInstance")] - [object]$SqlServer = "--installserver--", + [object]$SqlServer = "SVPSQLDBA", [object]$SqlCredential, - [string]$InstallDatabase = "--installdb--", - [string]$LogFileFolder = "--logdir--" + [string]$InstallDatabase = "dbareports", + [string]$LogFileFolder = "Q:\Backups\dbareports\logs" ) BEGIN @@ -58,21 +58,7 @@ BEGIN PROCESS { - try - { - Write-Log -path $LogFilePath -message "Getting a list of servers from the dbareports database" -level info - - $sql = "SELECT DISTINCT ServerID, ServerName FROM dbo.instancelist" - $sqlservers = $sourceserver.Databases[$InstallDatabase].ExecuteWithResults($sql).Tables[0] - - Write-Log -path $LogFilePath -message "Got the list of servers from the dbareports database" -level info - - } - catch - { - Write-Log -path $LogFilePath -message " Failed to get instances - $_" -level Error - break - } + $sqlservers = Get-DbrInstanceList try { $data = dbatools\Get-DbaDiskSpace -ComputerName $sqlservers.ServerName -EnableException -ErrorAction 'Continue' | Select-Object -Property @( From 8e650cb4ad17f9631fb30debc902651ec0970e64 Mon Sep 17 00:00:00 2001 From: Andrew Wickham Date: Thu, 15 Mar 2018 13:33:21 -0400 Subject: [PATCH 09/10] Using Get-DbrInstanceList breaks agent job --- setup/powershell/DiskSpace.ps1 | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/setup/powershell/DiskSpace.ps1 b/setup/powershell/DiskSpace.ps1 index bf3ce57..f5c5420 100644 --- a/setup/powershell/DiskSpace.ps1 +++ b/setup/powershell/DiskSpace.ps1 @@ -58,7 +58,21 @@ BEGIN PROCESS { - $sqlservers = Get-DbrInstanceList + try + { + Write-Log -path $LogFilePath -message "Getting a list of servers from the dbareports database" -level info + + $sql = "SELECT DISTINCT ServerID, ServerName FROM dbo.instancelist" + $sqlservers = $sourceserver.Databases[$InstallDatabase].ExecuteWithResults($sql).Tables[0] + + Write-Log -path $LogFilePath -message "Got the list of servers from the dbareports database" -level info + + } + catch + { + Write-Log -path $LogFilePath -message " Failed to get instances - $_" -level Error + break + } try { $data = dbatools\Get-DbaDiskSpace -ComputerName $sqlservers.ServerName -EnableException -ErrorAction 'Continue' | Select-Object -Property @( From 3e0db8ad6da3bc02d1de1568bf7506ed16b4fcde Mon Sep 17 00:00:00 2001 From: Andrew Wickham Date: Thu, 15 Mar 2018 13:58:23 -0400 Subject: [PATCH 10/10] Add dbatools as required module --- dbareports.psd1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbareports.psd1 b/dbareports.psd1 index b815a96..ac9d015 100644 --- a/dbareports.psd1 +++ b/dbareports.psd1 @@ -49,7 +49,7 @@ ProcessorArchitecture = '' # Modules that must be imported into the global environment prior to importing this module - RequiredModules = @() + RequiredModules = @('dbatools') # Assemblies that must be loaded prior to importing this module RequiredAssemblies= @('Microsoft.SqlServer.Smo','Microsoft.SqlServer.SmoExtended')