-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathRead-DMSSQLData.ps1
139 lines (119 loc) · 4.81 KB
/
Read-DMSSQLData.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#Requires -Version 5.0
#Requires -Modules SQLServer
<#
.SYNOPSIS
Reads data from a table or a view of a SQL database
.DESCRIPTION
.NOTES
This PowerShell script was developed and optimized for ScriptRunner. The use of the scripts requires ScriptRunner.
The customer or user is authorized to copy the script from the repository and use them in ScriptRunner.
The terms of use for ScriptRunner do not apply to this script. In particular, ScriptRunner Software GmbH assumes no liability for the function,
the use and the consequences of the use of this freely available script.
PowerShell is a product of Microsoft Corporation. ScriptRunner is a product of ScriptRunner Software GmbH.
© ScriptRunner Software GmbH
.COMPONENT
Requires Module SQLServer
Requires the library script DMSSqlServer.ps1
.LINK
https://github.com/scriptrunner/ActionPacks/blob/master/DBSystems/SQLServer
.Parameter ServerInstance
Specifies the name of the target computer including the instance name, e.g. MyServer\Instance
.Parameter ServerCredential
Specifies a PSCredential object for the connection to the SQL Server. ServerCredential is ONLY used for SQL Logins.
When you are using Windows Authentication you don't specify -Credential. It is picked up from your current login.
.Parameter ColumnNames
Specifies the names of columns that this cmdlet returns, comma separated
.Parameter ColumnOrder
Specifies the names of columns by which this cmdlet sorts the columns that it returns, comma separated
.Parameter ColumnOrderType
Specifies the order type for columns that this cmdlet returns
.Parameter DatabaseName
Specifies the name of the database that contains the table
.Parameter SchemaName
Specifies the name of the schema for the table or view
.Parameter TableName
Specifies the name of the table
.Parameter ViewName
Specifies the name of the view
.Parameter NumberOfRows
Specifies the number of rows of data that returns
.Parameter ConnectionTimeout
Specifies the time period to retry the command on the target server
#>
[CmdLetBinding()]
Param(
[Parameter(Mandatory = $true,ParameterSetName = "Table")]
[Parameter(Mandatory = $true,ParameterSetName = "View")]
[string]$ServerInstance,
[Parameter(Mandatory = $true,ParameterSetName = "Table")]
[Parameter(Mandatory = $true,ParameterSetName = "View")]
[string]$DatabaseName,
[Parameter(Mandatory = $true,ParameterSetName = "Table")]
[string]$TableName,
[Parameter(Mandatory = $true,ParameterSetName = "View")]
[string]$ViewName,
[Parameter(ParameterSetName = "Table")]
[Parameter(ParameterSetName = "View")]
[pscredential]$ServerCredential,
[Parameter(ParameterSetName = "Table")]
[Parameter(ParameterSetName = "View")]
[string]$SchemaName = "dbo",
[Parameter(ParameterSetName = "Table")]
[Parameter(ParameterSetName = "View")]
[string]$ColumnNames,
[Parameter(ParameterSetName = "Table")]
[Parameter(ParameterSetName = "View")]
[string]$ColumnOrder,
[Parameter(ParameterSetName = "Table")]
[Parameter(ParameterSetName = "View")]
[Int64]$NumberOfRows,
[Parameter(ParameterSetName = "Table")]
[Parameter(ParameterSetName = "View")]
[ValidateSet('ASC','DESC')]
[string]$ColumnOrderType = "ASC",
[Parameter(ParameterSetName = "Table")]
[Parameter(ParameterSetName = "View")]
[int]$ConnectionTimeout = 30
)
Import-Module SQLServer
try{
[hashtable]$cmdArgs = @{'ErrorAction' = 'Stop'
'ServerInstance' = $ServerInstance
'ConnectionTimeout' = $ConnectionTimeout
'DatabaseName' = $DatabaseName
'SchemaName' = $SchemaName
'ColumnOrderType' = $ColumnOrderType
'SuppressProviderContextWarning' = $null
}
if([System.String]::IsNullOrWhiteSpace($ColumnNames) -eq $false){
$cmdArgs.Add('ColumnName',$ColumnNames.Split(','))
}
if([System.String]::IsNullOrWhiteSpace($ColumnOrder) -eq $false){
$cmdArgs.Add('ColumnOrder',$ColumnOrder.Split(','))
}
if($NumberOfRows -gt 0){
$cmdArgs.Add('TopN',$NumberOfRows)
}
if($null -ne $ServerCredential){
$cmdArgs.Add('Credential',$ServerCredential)
}
if($PSCmdlet.ParameterSetName -eq 'Table'){
$cmdArgs.Add('TableName' , $TableName)
$Script:result = Read-SqlTableData @cmdArgs
}
else {
$cmdArgs.Add('ViewName' , $ViewName)
$Script:result = Read-SqlViewData @cmdArgs
}
if($SRXEnv) {
$SRXEnv.ResultMessage = $Script:result
}
else{
Write-Output $Script:result
}
}
catch{
throw
}
finally{
}