Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Generate-SQL2CSVData
- {
- [CmdletBinding()]
- param(
- [Parameter(Position=0,mandatory=$true)] [String] $sSecurePasswordDataFilePath,
- [Parameter(Position=0,mandatory=$true)] [String] $sSQLConfigPath
- )
- [xml]$SQLConfig = Get-Content $sSQLConfigPath
- foreach( $DBConfig in $SQLConfig.SQLServerConfig)
- {
- $sSQLServer = $DBConfig.SQLServer
- $sDatabase = $DBConfig.DatabaseName
- $sUserName = $DBConfig.UserName
- $sSecurePassword = $DBConfig.Password
- $SqlQuery = $DBConfig.SQLQuery
- $CSVFileName = $DBConfig.CSVFileName
- }
- #Check if the SecurePassword Data file exists or if the contents of the Secure Password file a
- <# if(!(Test-path $sSecurePasswordDataFilePath) -or ((Get-Content $sSecurePasswordDataFilePath).Length -eq 0))
- {
- $creds=get-credential
- $SNowUser = $creds.username
- $bytes = ConvertFrom-SecureString $creds.password
- $bytes | out-file $sSecurePasswordDataFilePath
- }
- $encrypt = Get-Content $sSecurePasswordDataFilePath -ErrorAction Stop #>
- $sPassword = ConvertTo-SecureString -string $sSecurePassword -ErrorAction Stop
- # Create the SqlCredential object
- $sqlCred = New-Object System.Data.SqlClient.SqlCredential($sUserName,$sPassword)
- ## - Connect to SQL Server using non-SMO class 'System.Data':
- $SqlConnection = New-Object System.Data.SqlClient.SqlConnection;
- $SqlConnection.ConnectionString = "Server=$SQLServer;Database=$Database"
- #;Integrated Security = True
- $SqlCmd = New-Object System.Data.SqlClient.SqlCommand;
- $SqlCmd.CommandText = $SqlQuery;
- $SqlCmd.Connection = $SqlConnection;
- ## - Extract and build the SQL data object '$DataSetTable':
- $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter;
- $SqlAdapter.SelectCommand = $SqlCmd;
- $DataSet = New-Object System.Data.DataSet;
- $SqlAdapter.Fill($DataSet);
- $DataSetTable = $DataSet.Tables["Table"];
- $DataSetTable | Export-Csv -NoTypeInformation $CSVFileName
- $SqlConnection.Close()
- }
- I am generating the password and saving it in the SQLConfig File
- $password = read-host -prompt "Enter your Password"
- $secure = ConvertTo-SecureString $password -force -asPlainText
- $bytes = ConvertFrom-SecureString $secure
- $bytes
Add Comment
Please, Sign In to add comment