## =====================================================================
## Title : CheckDB-MSSQL-UsingADO
## Description : Run a DBCC against specified server instance and database
## Author : Idera
## Date : 9/1/2008
## Input : -serverInstance <server\instance>
## -dbName <database name>
## -verbose
## -debug
## Output :
## Usage : PS> .\CheckDB-MSSQL-UsingADO -serverInstance local -dbName master -verbose -debug
## Notes : Adapted from Jakob Bindslet script
## Tag : SQL Server, ADO, CheckDB
## Change log :
## 4/1/2009 - Added input prompts and write-verbose statements
## =====================================================================
param
(
[string]$ServerInstance = "$(Read-Host 'Analysis Server Instance' [e.g. local])",
[string]$dbName = "$(Read-Host 'Database Name' [e.g. AdventureWorks])",
[switch]$verbose = $true,
[switch]$debug = $false
)
function main()
{
if ($verbose) {$VerbosePreference = "Continue"}
if ($debug) {$DebugPreference = "Continue"}
Write-Verbose "Run a DBCC against specified server instance and database..."
CheckDB-MSSQL-UsingADO $serverInstance $dbName
}
function CheckDB-MSSQL-UsingADO($serverInstance, $dbName)
{
trap [Exception]
{
write-error $("TRAPPED: " + $_.Exception.Message);
continue;
}
$cn = new-object system.data.SqlClient.SqlConnection( `
"Data Source=$serverInstance;Integrated Security=SSPI;Initial Catalog=$dbName");
$ds = new-object System.Data.DataSet "dsCheckDB"
$query = "DBCC CHECKDB($dbName) WITH TABLERESULTS"
$da = new-object "System.Data.SqlClient.SqlDataAdapter" ($query, $cn)
$da.Fill($ds)
$dtCheckDB = new-object "System.Data.DataTable" "dsCheckDB"
$dtCheckDB = $ds.Tables[0]
$dtCheckDB | Format-Table -autosize `
-property Error, Level, State, MessageText, `
RepairLevel, Status, DbId, ObjectId, `
IndexId, PartitionId, AllocUnitId, File, `
Page, Slot, RefFile, RefPage, RefSlot, Allocation
}
main
本脚本是使用DBCC检查数据库和数据库实例。使用ADO连接到数据库运行DBCC。
要示例脚本中第52行,Format-table是新的PwerShell的Cmdlets,它是将管道中的信息按表格的格式输出到标准输出。
标准帮助信息如下:
SYNTAX
Format-Table [[-Property] <Object[]>] [-AutoSize] [-RepeatHeader] [-HideTableHeaders] [-Wrap] [-GroupBy <Object>] [
-View <string>] [-ShowError] [-DisplayError] [-Force] [-Expand {CoreOnly | EnumOnly | Both}] [-InputObject <psobjec
t>] [<CommonParameters>]
PARAMETERS
-AutoSize
Required? false
Position? Named
Accept pipeline input? false
Parameter set name (All)
Aliases None
Dynamic? false
Accept wildcard characters? false
-DisplayError
Required? false
Position? Named
Accept pipeline input? false
Parameter set name (All)
Aliases None
Dynamic? false
Accept wildcard characters? false
-Expand <string>
Required? false
Position? Named
Accept pipeline input? false
Parameter set name (All)
Aliases None
Dynamic? false
Accept wildcard characters? false
-Force
Required? false
Position? Named
Accept pipeline input? false
Parameter set name (All)
Aliases None
Dynamic? false
Accept wildcard characters? false
-GroupBy <Object>
Required? false
Position? Named
Accept pipeline input? false
Parameter set name (All)
Aliases None
Dynamic? false
Accept wildcard characters? false
-HideTableHeaders
Required? false
Position? Named
Accept pipeline input? false
Parameter set name (All)
Aliases None
Dynamic? false
Accept wildcard characters? false
-InputObject <psobject>
Required? false
Position? Named
Accept pipeline input? true (ByValue)
Parameter set name (All)
Aliases None
Dynamic? false
Accept wildcard characters? false
-Property <Object[]>
Required? false
Position? 0
Accept pipeline input? false
Parameter set name (All)
Aliases None
Dynamic? false
Accept wildcard characters? false
-RepeatHeader
Required? false
Position? Named
Accept pipeline input? false
Parameter set name (All)
Aliases None
Dynamic? false
Accept wildcard characters? false
-ShowError
Required? false
Position? Named
Accept pipeline input? false
Parameter set name (All)
Aliases None
Dynamic? false
Accept wildcard characters? false
-View <string>
Required? false
Position? Named
Accept pipeline input? false
Parameter set name (All)
Aliases None
Dynamic? false
Accept wildcard characters? false
-Wrap
Required? false
Position? Named
Accept pipeline input? false
Parameter set name (All)
Aliases None
Dynamic? false
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https://go.microsoft.com/fwlink/?LinkID=113216).
INPUTS
System.Management.Automation.PSObject
OUTPUTS
System.Object
ALIASES
ft
标签:CheckDB,false,Position,示例,##,Accept,wildcard,input,PowershellPlus
From: https://blog.51cto.com/u_3353175/8464264