Last active
February 9, 2020 05:14
-
-
Save aadennis/fb973ebe98bbb4117668a16ccab800bb to your computer and use it in GitHub Desktop.
This file contains 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
# credit: https://virtuallysober.com/2018/05/30/connecting-to-microsoft-sql-databases-using-powershell-invoke-sqlcmd/ | |
$ServerInstance = "aardvark" | |
$SQLUser = "sa" | |
$SQLPassword = "thedemo" | |
Import-Module SqlServer | |
$dbCheck = " | |
with userDatabaseSet as ( | |
SELECT | |
DB_NAME(db.database_id) dbName | |
FROM sys.databases db) | |
select dbName from userDatabaseSet | |
where dbName not in ('master','model','msdb','tempdb') | |
order by dbName | |
" | |
try { | |
$dbResult = $null | |
$dbResult = Invoke-SqlCmd -Query $dbCheck -ServerInstance $ServerInstance -Username $SQLUser -Password $SQLPassword | |
$success = $true | |
} | |
catch { | |
$success = $false | |
} | |
"ServerInstance: $ServerInstance" | |
"success: $success" | |
"dbResult:" | |
$dbResult |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment