Created
January 22, 2019 23:04
-
-
Save bsquidwrd/edcf1b8e898b2b07a3ec0a26b206c96f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$sqlServerIP = "xxx.xxx.xxx.xxx" | |
$sqlServerDatabase = "DST18000XXXX" | |
$sqlServerUsername = "someone" | |
$passwordFilePath = "C:\Data\test.txt" | |
# This checks if the $passwordFilePath exists | |
# if not, go ahead and prompt the user for it | |
# (usually only done on first run) | |
if (-not(Test-Path $passwordFilePath)) { | |
$credentials = Get-Credential -Message "Enter the SQL Users password" -Username $sqlServerUsername | |
$credentials.password | ConvertFrom-SecureString | Set-Content $passwordFilePath | |
} | |
# Get the encrypted password and convert it to a secure string | |
$encrypted = Get-Content $passwordFilePath | ConvertTo-SecureString | |
# Turn the secure string and the $sqlServerUsername into powershell credentials object | |
$sqlServerCredentials = New-object System.Management.Automation.PsCredential($sqlServerUsername, $encrypted) | |
# Query the $sqlServerIP | |
$result = (Invoke-SQLcmd -serverinstance $sqlServerIP -Credential $sqlServerCredentials -Database $sqlServerDatabase -Query "select top 5 * from STU" -OutputSqlErrors $false -QueryTimeout 0) | |
$result.ID |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment