Skip to content

Commit 219680b

Browse files
committed
Added DataTables as a module.
Changed Build-Restore to New-Restore for verb compliance. Added Sync-DBUsers and Get-DBCCCheckDB functions.
1 parent 041324c commit 219680b

File tree

2 files changed

+315
-85
lines changed

2 files changed

+315
-85
lines changed

DataTables.psm1

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
#######################
2+
function Get-Type
3+
{
4+
param($type)
5+
6+
$types = @(
7+
'System.Boolean',
8+
'System.Byte[]',
9+
'System.Byte',
10+
'System.Char',
11+
'System.Datetime',
12+
'System.Decimal',
13+
'System.Double',
14+
'System.Guid',
15+
'System.Int16',
16+
'System.Int32',
17+
'System.Int64',
18+
'System.Single',
19+
'System.UInt16',
20+
'System.UInt32',
21+
'System.UInt64')
22+
23+
if ( $types -contains $type ) {
24+
Write-Output "$type"
25+
}
26+
else {
27+
Write-Output 'System.String'
28+
29+
}
30+
} #Get-Type
31+
32+
#######################
33+
<#
34+
.SYNOPSIS
35+
Creates a DataTable for an object
36+
.DESCRIPTION
37+
Creates a DataTable based on an objects properties.
38+
.INPUTS
39+
Object
40+
Any object can be piped to Out-DataTable
41+
.OUTPUTS
42+
System.Data.DataTable
43+
.EXAMPLE
44+
$dt = Get-psdrive| Out-DataTable
45+
This example creates a DataTable from the properties of Get-psdrive and assigns output to $dt variable
46+
.NOTES
47+
Adapted from script by Marc van Orsouw see link
48+
Version History
49+
v1.0 - Chad Miller - Initial Release
50+
v1.1 - Chad Miller - Fixed Issue with Properties
51+
v1.2 - Chad Miller - Added setting column datatype by property as suggested by emp0
52+
v1.3 - Chad Miller - Corrected issue with setting datatype on empty properties
53+
v1.4 - Chad Miller - Corrected issue with DBNull
54+
v1.5 - Chad Miller - Updated example
55+
v1.6 - Chad Miller - Added column datatype logic with default to string
56+
v1.7 - Chad Miller - Fixed issue with IsArray
57+
.LINK
58+
http://thepowershellguy.com/blogs/posh/archive/2007/01/21/powershell-gui-scripblock-monitor-script.aspx
59+
#>
60+
function Out-DataTable
61+
{
62+
[CmdletBinding()]
63+
param([Parameter(Position=0, Mandatory=$true, ValueFromPipeline = $true)] [PSObject[]]$InputObject)
64+
65+
Begin
66+
{
67+
$dt = new-object Data.datatable
68+
$First = $true
69+
}
70+
Process
71+
{
72+
foreach ($object in $InputObject)
73+
{
74+
$DR = $DT.NewRow()
75+
foreach($property in $object.PsObject.get_properties())
76+
{
77+
if ($first)
78+
{
79+
$Col = new-object Data.DataColumn
80+
$Col.ColumnName = $property.Name.ToString()
81+
if ($property.value)
82+
{
83+
if ($property.value -isnot [System.DBNull]) {
84+
$Col.DataType = [System.Type]::GetType("$(Get-Type $property.TypeNameOfValue)")
85+
}
86+
}
87+
$DT.Columns.Add($Col)
88+
}
89+
if ($property.Gettype().IsArray) {
90+
$DR.Item($property.Name) =$property.value | ConvertTo-XML -AS String -NoTypeInformation -Depth 1
91+
}
92+
else {
93+
$DR.Item($property.Name) = $property.value
94+
}
95+
}
96+
$DT.Rows.Add($DR)
97+
$First = $false
98+
}
99+
}
100+
101+
End
102+
{
103+
Write-Output @(,($dt))
104+
}
105+
106+
} #Out-DataTable
107+
108+
#######################
109+
<#
110+
.SYNOPSIS
111+
Writes data only to SQL Server tables.
112+
.DESCRIPTION
113+
Writes data only to SQL Server tables. However, the data source is not limited to SQL Server; any data source can be used, as long as the data can be loaded to a DataTable instance or read with a IDataReader instance.
114+
.INPUTS
115+
None
116+
You cannot pipe objects to Write-DataTable
117+
.OUTPUTS
118+
None
119+
Produces no output
120+
.EXAMPLE
121+
$dt = Invoke-Sqlcmd2 -ServerInstance "Z003\R2" -Database pubs "select * from authors"
122+
Write-DataTable -ServerInstance "Z003\R2" -Database pubscopy -TableName authors -Data $dt
123+
This example loads a variable dt of type DataTable from query and write the datatable to another database
124+
.NOTES
125+
Write-DataTable uses the SqlBulkCopy class see links for additional information on this class.
126+
Version History
127+
v1.0 - Chad Miller - Initial release
128+
v1.1 - Chad Miller - Fixed error message
129+
.LINK
130+
http://msdn.microsoft.com/en-us/library/30c3y597%28v=VS.90%29.aspx
131+
#>
132+
function Write-DataTable
133+
{
134+
[CmdletBinding()]
135+
param(
136+
[Parameter(Position=0, Mandatory=$true)] [string]$ServerInstance,
137+
[Parameter(Position=1, Mandatory=$true)] [string]$Database,
138+
[Parameter(Position=2, Mandatory=$true)] [string]$TableName,
139+
[Parameter(Position=3, Mandatory=$true)] $Data,
140+
[Parameter(Position=4, Mandatory=$false)] [string]$Username,
141+
[Parameter(Position=5, Mandatory=$false)] [string]$Password,
142+
[Parameter(Position=6, Mandatory=$false)] [Int32]$BatchSize=50000,
143+
[Parameter(Position=7, Mandatory=$false)] [Int32]$QueryTimeout=0,
144+
[Parameter(Position=8, Mandatory=$false)] [Int32]$ConnectionTimeout=15
145+
)
146+
147+
$conn=new-object System.Data.SqlClient.SQLConnection
148+
149+
if ($Username)
150+
{ $ConnectionString = "Server={0};Database={1};User ID={2};Password={3};Trusted_Connection=False;Connect Timeout={4}" -f $ServerInstance,$Database,$Username,$Password,$ConnectionTimeout }
151+
else
152+
{ $ConnectionString = "Server={0};Database={1};Integrated Security=True;Connect Timeout={2}" -f $ServerInstance,$Database,$ConnectionTimeout }
153+
154+
$conn.ConnectionString=$ConnectionString
155+
156+
try
157+
{
158+
$conn.Open()
159+
$bulkCopy = new-object ("Data.SqlClient.SqlBulkCopy") $connectionString
160+
$bulkCopy.DestinationTableName = $tableName
161+
$bulkCopy.BatchSize = $BatchSize
162+
$bulkCopy.BulkCopyTimeout = $QueryTimeOut
163+
$bulkCopy.WriteToServer($Data)
164+
$conn.Close()
165+
}
166+
catch
167+
{
168+
$ex = $_.Exception
169+
Write-Error "$ex.Message"
170+
continue
171+
}
172+
173+
} #Write-DataTable

0 commit comments

Comments
 (0)