-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserversidescript.ps1
More file actions
57 lines (44 loc) · 1.7 KB
/
serversidescript.ps1
File metadata and controls
57 lines (44 loc) · 1.7 KB
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
function Invoke-Sql {
param(
[string] $dataSource,
[string] $database,
[string] $sqlCommand,
[System.Management.Automation.PsCredential] $credential
)
$authentication = "Integrated Security=SSPI;"
if ($credential) {
$plainCred = $credential.GetNetworkCredential()
$authentication =
("uid={0};pwd={1};" -f $plainCred.Username, $plainCred.Password)
}
$connectionString = "Provider=sqloledb; " +
"Data Source=$dataSource; " +
"Initial Catalog=$database; " +
"$authentication; "
$connection = New-Object System.Data.OleDb.OleDbConnection $connectionString
$command = New-Object System.Data.OleDb.OleDbCommand $sqlCommand, $connection
$connection.Open()
$adapter = New-Object System.Data.OleDb.OleDbDataAdapter $command
$dataset = New-Object System.Data.DataSet
[void] $adapter.Fill($dataSet)
$connection.Close()
$dataSet.Tables | Select-Object -Expand Rows
}
$uid = 'Adminusername'
$pwd = 'AdminPassword'
$securePassword = $pwd | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $uid, $securePassword
$sqlcommand="Sqlquery"
$data=Invoke-Sql `
-datasource "azureservername.database.windows.net" `
-database "azuredatabasename" `
-credential $cred `
-sqlcommand $sqlcommand
$datanamelist=($data | gm | where {$_.MemberType -eq "Property"} | select -expandproperty Name) -split " "
$endstr=""
ForEach ($item in $datanamelist) {
$endstr=$endstr+$item+"-"
$valuetoadd=($data | select -ExpandProperty $item)
$endstr=$endstr+$valuetoadd+"_"
}
out-file -encoding Ascii -FilePath $res -Inputobject ($endstr)