Please wait,
Processing your request...

    0%
  Business logo VanSoest.it  
  ... | Selecteer de Nederlandse taal | Print this page. | Linkedin page of Johan van Soest | User: Guest | Login

Postcard image. Click this to mail to Johan

WebHalla
 Content
  Management
   System


ICT-Hotlist Topic

Back to the ICT-Hotlist...
Don't miss latest updates Follow @WebHalla

How to install the SQL-Server PowerShell CMDLets on Windows and performs some test T-SQL queries

You 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:
  • Windows 7
  • Windows 8
  • Windows 8.1
  • Windows Server 2008 R2
  • Windows Server 2008 R2 SP1
  • Windows Server 2012
  • Windows Server 2012 R2
More information about supported softwareversions.

Install the Microsoft SQL Server PowerShell module.

  • Required : Microsoft ® System CLR Types for Microsoft SQL Server ® 2014 (SQLSysClrTypes.msi)
    Download and run ENU\x64\SQLSysClrTypes.msi
  • Required : Microsoft ® SQL Server ® 2014 Shared Management Objects(SharedManagementObjects.msi)
    Download and run ENU\x64\SharedManagementObjects.msi
  • Required : Microsoft ® Windows PowerShell Extensions for Microsoft SQL Server ® 2014 (PowerShellTools.msi)
    Download and run ENU\x64\PowerShellTools.msi

Reboot the computer

Check 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


ModuleType Version    Name                                ExportedCommands                                   ---------- -------    ----                                ----------------                                   Manifest   1.0        SQLPS                               {Backup-SqlDatabase, Add-SqlAvailabilityDatabas...
Next you have to import the sqlps module.
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 PowerShell

Try 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
# of Microsoft SQL Server stored produres from Windows PowerShell
# (C)Copyright 2015 - 2019 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
This SQL-Server Stored Procedure output is filtered (Where clause) and sorted to produce the following output in a PowerShell GridView
PowerShell SQL Server Stored Procedure Result
PowerShell SQL Server Stored Procedure Result

A Free T-SQL Query with PowerShell

Use 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
# of Microsoft SQL Server free T-SQL queries from Windows PowerShell
# (C)Copyright 2015 - 2019 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
This free query produces the following output in a table grid
PowerShell SQL Server Free Query Result
PowerShell SQL Server Free Query Result
You may vote your opinion about this article:


Topic created  :  01-05-2017
Topic last edited  :  26-07-2019

Scripts and programming examples disclaimer

Unless 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.
Generated by WebHalla™ Version 0.1.e.3 : Friday 18-10-2019 © Copyright 1995-2019 ing. Johan P.G. van Soest CIPM Certified Privacy Information Manager
Response Form    Cookie- and Privacy statement
Weather in Waalre by OpenWeatherMap logo overcast clouds
Temperature 11.29 °C overcast clouds
Humidity 76 % overcast clouds
Air pressure 1007 hPa overcast clouds
Wind speed 4.1 m/s overcast clouds
Wind direction South South overcast clouds
Updated:2019-10-18 21:46:03 overcast clouds

Weather Cache is 1 minute(s) old.