diff --git a/dbareports.psd1 b/dbareports.psd1
index 6c5bfc2..ac9d015 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 = @()
-
+ RequiredModules = @('dbatools')
+
# 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
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
diff --git a/setup/powershell/DiskSpace.ps1 b/setup/powershell/DiskSpace.ps1
index d39798a..f5c5420 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
@@ -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
@@ -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
@@ -44,47 +44,29 @@ 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
{
Write-Log -path $LogFilePath -message "Connecting to $sqlserver" -level info
- $sourceserver = Connect-SqlServer -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
- }
-
}
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]
+
Write-Log -path $LogFilePath -message "Got the list of servers from the dbareports database" -level info
-
+
}
catch
{
@@ -92,118 +74,51 @@ 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
+ 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 "Can't get server list from $InstallDatabase on $($sourceserver.name). - $_" -level Error
+ catch {
+ Write-Log -Path $LogFilePath -Message $_.Message -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
- {
- $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
- }
-
- 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
- }
-
- 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
- }
-
- $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
- }
- }
- }
- }
-
+ $datatable = dbatools\ConvertTo-DbaDataTable -InputObject $data
+
$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
+
+ $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
@@ -215,4 +130,5 @@ PROCESS
END
{
Write-Log -path $LogFilePath -message "DiskSpace Finished"
+ $sourceserver.ConnectionContext.Disconnect()
}
\ No newline at end of file