|
|
|
|||||||
|
ICT-Hotlist TopicHow to install the SQL-Server PowerShell CMDLets on Windows and performs some test T-SQL queriesYou will need the following components that depend on each other and are downloaded in the wrong order by default. The components can be downloaded at the Microsoft ® SQL Server ® 2014 Feature Pack page. These operating systems are supported:
Install the Microsoft SQL Server PowerShell module.
Reboot the computerCheck and load the SQL server PowerShell module.Now start Powershell or ISE and type:
Get-Module -ListAvailable
Now you see output that looks similar to:
Directory: C:\Program Files\Microsoft SQL Server\120\Tools\PowerShell\Modules
Next you have to import the sqlps module.
ModuleType Version Name ExportedCommands ---------- ------- ---- ---------------- Manifest 1.0 SQLPS {Backup-SqlDatabase, Add-SqlAvailabilityDatabas...
Import-Module "sqlps" -Verbose
You can now see the cmdlets and properties that are imported:
VERBOSE: Importing cmdlet 'Add-SqlAvailabilityDatabase'.
VERBOSE: Importing cmdlet 'Add-SqlAvailabilityGroupListenerStaticIp'. VERBOSE: Importing cmdlet 'Add-SqlFirewallRule'. VERBOSE: Importing cmdlet 'Backup-SqlDatabase'. VERBOSE: Importing cmdlet 'Convert-UrnToPath'. WARNING: The names of some imported commands from the module 'sqlps' include unapproved verbs that might make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the Verbose parameter. For a list of approved verbs, type Get-Verb. VERBOSE: The 'Decode-SqlName' command in the sqlps' module was imported, but because its name does not include an approved verb, it might be difficult to find. For a list of approved verbs, type Get-Verb. VERBOSE: Importing cmdlet 'Decode-SqlName'. VERBOSE: Importing cmdlet 'Disable-SqlAlwaysOn'. VERBOSE: Importing cmdlet 'Enable-SqlAlwaysOn'. VERBOSE: The 'Encode-SqlName' command in the sqlps' module was imported, but because its name does not include an approved verb, it might be difficult to find. For a list of approved verbs, type Get-Verb. VERBOSE: Importing cmdlet 'Encode-SqlName'. VERBOSE: Importing cmdlet 'Get-SqlCredential'. VERBOSE: Importing cmdlet 'Get-SqlDatabase'. VERBOSE: Importing cmdlet 'Get-SqlInstance'. VERBOSE: Importing cmdlet 'Get-SqlSmartAdmin'. VERBOSE: Importing cmdlet 'Invoke-PolicyEvaluation'. VERBOSE: Importing cmdlet 'Invoke-Sqlcmd'. VERBOSE: Importing cmdlet 'Join-SqlAvailabilityGroup'. VERBOSE: Importing cmdlet 'New-SqlAvailabilityGroup'. VERBOSE: Importing cmdlet 'New-SqlAvailabilityGroupListener'. VERBOSE: Importing cmdlet 'New-SqlAvailabilityReplica'. VERBOSE: Importing cmdlet 'New-SqlBackupEncryptionOption'. VERBOSE: Importing cmdlet 'New-SqlCredential'. VERBOSE: Importing cmdlet 'New-SqlHADREndpoint'. VERBOSE: Importing cmdlet 'Remove-SqlAvailabilityDatabase'. VERBOSE: Importing cmdlet 'Remove-SqlAvailabilityGroup'. VERBOSE: Importing cmdlet 'Remove-SqlAvailabilityReplica'. VERBOSE: Importing cmdlet 'Remove-SqlCredential'. VERBOSE: Importing cmdlet 'Remove-SqlFirewallRule'. VERBOSE: Importing cmdlet 'Restore-SqlDatabase'. VERBOSE: Importing cmdlet 'Resume-SqlAvailabilityDatabase'. VERBOSE: Importing cmdlet 'Set-SqlAuthenticationMode'. VERBOSE: Importing cmdlet 'Set-SqlAvailabilityGroup'. VERBOSE: Importing cmdlet 'Set-SqlAvailabilityGroupListener'. VERBOSE: Importing cmdlet 'Set-SqlAvailabilityReplica'. VERBOSE: Importing cmdlet 'Set-SqlCredential'. VERBOSE: Importing cmdlet 'Set-SqlHADREndpoint'. VERBOSE: Importing cmdlet 'Set-SqlNetworkConfiguration'. VERBOSE: Importing cmdlet 'Set-SqlSmartAdmin'. VERBOSE: Importing cmdlet 'Start-SqlInstance'. VERBOSE: Importing cmdlet 'Stop-SqlInstance'. VERBOSE: Importing cmdlet 'Suspend-SqlAvailabilityDatabase'. VERBOSE: Importing cmdlet 'Switch-SqlAvailabilityGroup'. VERBOSE: Importing cmdlet 'Test-SqlAvailabilityGroup'. VERBOSE: Importing cmdlet 'Test-SqlAvailabilityReplica'. VERBOSE: Importing cmdlet 'Test-SqlDatabaseReplicaState'. VERBOSE: Importing cmdlet 'Test-SqlSmartAdmin'. VERBOSE: Importing function 'SQLSERVER:'. VERBOSE: Importing variable 'm'. Run a SQL Stored Procedure with PowerShellTry your first query with a SQL stored procedure to retrieve the sizes of selected databases on your own computer running SQL-Server 2014 Express:
# This Powershell script is designed to demonstrate the use
This SQL-Server Stored Procedure output is filtered (Where clause) and sorted to produce the following output in a PowerShell GridView# of Microsoft SQL Server stored produres from Windows PowerShell # (C)Copyright 2015 - 2024 vanSoest.it by Johan van Soest Invoke-SQLCmd -Query "sp_databases" -Database master -ServerInstance localhost\sqlexpress | Where {$_.DATABASE_NAME -in ("master", "model", "msdb", "ReBaUpFA", "tempdb")} | Sort-Object DATABASE_SIZE -Descending | Out-GridView PowerShell SQL Server Stored Procedure Result A Free T-SQL Query with PowerShellUse PowerShell and a free T-SQL query to get the name, create_date, compatibility_level and collation_name of selected databases in a PowerShell GridView
# This Powershell script is designed to demonstrate the use
This free query produces the following output in a table grid# of Microsoft SQL Server free T-SQL queries from Windows PowerShell # (C)Copyright 2015 - 2024 vanSoest.it by Johan van Soest Invoke-SQLCmd -Query "Select * From sys.databases" -Database master -ServerInstance localhost\sqlexpress | Where {$_.name -in ("master", "model", "msdb", "ReBaUpFA", "tempdb")} | Select-Object name, create_date, compatibility_level, collation_name | Out-GridView PowerShell SQL Server Free Query Result
You may vote your opinion about this article:
Scripts and programming examples disclaimerUnless stated otherwise, the script sources and programming examples provided are copyrighted freeware. You may modify them, as long as a reference to the original code and hyperlink to the source page is included in the modified code and documentation. However, it is not allowed to publish (copies of) scripts and programming examples on your own site, blog, vlog, or distribute them on paper or any other medium, without prior written consent.Many of the techniques used in these scripts, including but not limited to modifying the registry or system files and settings, impose a risk of rendering the Operating System inoperable and loss of data. Make sure you have verified full backups and the associated restore software available before running any script or programming example. Use these scripts and programming examples entirely at your own risk. All liability claims against the author in relation to material or non-material losses caused by the use, misuse or non-use of the information provided, or the use of incorrect or incomplete information, are excluded. All content is subject to change and provided without obligation. |