Académique Documents
Professionnel Documents
Culture Documents
Download the latest version of this PowerShell wallchart and read the accompanying in-depth article
from Simple-Talk at http://bit.ly/MZmOX9
Profile Locations
Module Repositories
$env:windir \System32\WindowsPowerShell\v1.0\Modules
$HOME \Documents\WindowsPowerShell\Modules
Load
Import-Module
Add-PSSnapin
[-ServerInstance <PSObject>]
[-Database <String>]
[-EncryptConnection]
[-Username <String>]
[-Password <String>]
[[-Query] <String>]
[-QueryTimeout <Int32>]
[-ConnectionTimeout <Int32>]
[-ErrorLevel <Int32>]
[-SeverityLevel <Int32>]
[-MaxCharLength <Int32>]
[-MaxBinaryLength <Int32>]
[-AbortOnError]
[-DedicatedAdministratorConnection]
[-DisableVariables]
[-DisableCommands]
[-HostName <String>]
[-NewPassword <String>]
[-Variable <String[]>]
[-InputFile <String>]
[-OutputSqlErrors]
[-SuppressProviderConte xtWarning]
[-IgnoreProviderContext]
[<CommonParameters>]
Description
SQL Server data store root
Network root
Instances on selected machine
Top-level instance objects
Databases in selected instance
Node
\
\SQL
\SQL\machine
\SQL\machine\instance
\SQL\machine\instance\Databases
Top-level DB objects
\SQL\machine\instance\Databases\database
Default Properties
Name, Root, Description
MachineName
InstanceName
list of object names
Name, Status, RecoveryModel,
CompatLvl, Collation, Owner
list of object names
\SQL\machine\instance\Databases\database\Tables
\SQL\machine\instance\Databases\database\Views
\SQL\machine\instance\Databases\database\Roles
\SQL\machine\instance\Databases\database\Triggers
....Smo.Table
....Smo.View
....Smo.DatabaseRole
....Smo.Trigger
Top-level nodes: \SQL, \SQLPolicy, \SQLRegistration, \Utility, \DAC, \DataCollection, \IntegrationServices, \SQLAS
Database nodes: ApplicationRoles, Assemblies, AsymmetricKeys, Certificates, DatabaseAuditSpecifications, Defaults, ExtendedProperties, ExtendedStoredProcedures, FileGroups,
FullTextCatalogs, FullTextStopLists, LogFiles, PartitionFunctions, PartitionSchemes, PlanGuides, Roles, Rules, Schemas, ServiceBroker, StoredProcedures, SymmetricKeys, Synonyms,
Tables, Triggers, UserDefinedAggregates, UserDefinedDataTypes, UserDefinedFunctions, UserDefinedTableTypes, UserDefinedTypes, Users, Views, XmlSchemaCollections
Command sequence
sql -Query select db_name()
sql -Query select db_name()-Server .\sqlexpress
sql -Query select * from clents| Out-GridView
sql select * from clents | Format-Table AutoSize
sql select * from clents | Format-List
Key Commands
Cmdlet
Get-Location
Set-Location
Get-ChildItem
Get-Item
Rename-Item
Remove-Item
Canonical
alias
gl
sl
gci
gi
rni
ri
Other aliases
Description
pwd
cd, chdir
dir, ls
ren
del, erase, rd, rm, rmdir
Command sequence
Set-Location SQLSERVER:\
Set-Location SQLSERVER:\SQL
Get-ChildItem SQLSERVER:\SQL\machine
Get-ChildItem SQLSERVER:\SQL\machine\instance\Databases
gci SQLSERVER:\SQL\machine\instance\Databases\database\Tables
gci sandboxDB:\Tables
gci sandboxDB:\Tables | where {$_.Schema -eq "dbo"}
gci sandboxDB:\Tables | Format-Table Force *
Full database
| Format-List
| Format-Table
$env:windir\system32\ WindowsPowerShell \v1.0\Modules\*. format.ps1xml
$HOME\Documents\WindowsPowerShell\Modules\*.format.ps1xml
$env:windir\system32\ WindowsPowerShell \v1.0\*.format.ps1xml
Description
All tables (output to console)
All tables (output to file)
All tables (separate batches)
Selected tables
Single table
All tables with their indexes
Use $object.PSStandardMembers.DefaultDisplayPropertySet;
otherwise, display all properties
Nodes display either in PowerShells list format or table format automatically, unless you
explicitly specify otherwise.
Executing Queries
Description
Query with default context
Query specifying server
Query to interactive grid
Output in table format
Output in list format
Invoke-Sqlcmd
Nodes reveal only a portion of their properties by default; see Key Commands for more.
Executing Queries
Command sequence
gci sandboxDB:\Tables | % { $_.Script() }
gci sandboxDB:\Tables | % { $_.Script() } | Set-Content C:\create.sql
gci sandboxDB:\Tables | % { $_.Script() + "GO" }
gci sandboxDB:\Tables | ? { $_.name -match "big.*" } | % { $_.Script() }
(gci sandboxDB:\Tables | ? { $_.name -eq "xyz_table" }).Script()
gci sandboxDB:\Tables | % {
$_.Script() + "GO"
$_.Indexes | % { $_.Script() + "GO" }
}
$myScriptFile = filepath
$myDbInstance = Get-Item nodepath
$mydb = $myDbInstance.Databases["dbName"]
$mydb.Script() | Out-File $myScriptFile
$scrp = new-object ('Microsoft.SqlServer.Management.Smo.Scripter') ($myDbInstance)
$scrp.Options.AppendToFile = $True
$scrp.Options.FileName = $myScriptFile
# Other options here
$scrp.Script([Microsoft.SqlServer.Management.Smo.SqlSmoObject[]]$mydb.Tables)
Root
---SQLSERVER:\sql\localhost\SQLEXPRESS\Databases
SQLSERVER:\
References
Learning PowerShell, PowerShell in SQL Server 2012 / 2008,
SQL Server PowerShell Help, Using SQL Server Cmdlets
Key Terms
SMO
SQLPS
SQL
Allows you to interact with the hierarchy of SQL Server
Server
objects just as a native PowerShell file system provider
Provider allows you to interact with files. You can navigate
through SQL Server space using paths to nodes
analogously to paths to files.
SQLPSX