首页 > 系统相关 >通过PowershellPlus示例脚本学习PowerShell-CheckDB

通过PowershellPlus示例脚本学习PowerShell-CheckDB

时间:2023-11-18 19:00:45浏览次数:132  
标签:CheckDB false Position 示例 ## Accept wildcard input PowershellPlus

## =====================================================================
## 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

相关文章

  • netcore 间隔时长修改参数 示例
    usingSystem;usingSystem.Threading.Tasks;classProgram{privatestaticintyourParameter=0;staticasyncTaskMain(){while(true){//等待5分钟awaitTask.Delay(TimeSpan.FromMinutes(5));//在这......
  • Mutiny Uni阻塞操作变响应式的方法示例
    以下代码片段为模拟一个这样的操作:在多台服务器上下载文件列表内的文件。其中,获取服务器、获取文件列表、在服务器执行下载操作均为阻塞方法。importcn.hutool.core.collection.CollUtil;importio.quarkus.test.junit.QuarkusTest;importio.smallrye.mutiny.Multi;impor......
  • SQL(Structured Query Language)简介和常见 SQL 命令示例
    简介SQL(StructuredQueryLanguage)是一种用于访问和操作关系型数据库的标准语言。它是一个功能强大的语言,用于执行各种数据库操作,包括检索数据、插入新记录、更新记录、删除记录、创建数据库、创建新表、设置权限以及执行存储过程和视图等。以下是SQL的一些重要方面:SQL的目的......
  • SQL(Structured Query Language)简介和常见 SQL 命令示例
    简介SQL(StructuredQueryLanguage)是一种用于访问和操作关系型数据库的标准语言。它是一个功能强大的语言,用于执行各种数据库操作,包括检索数据、插入新记录、更新记录、删除记录、创建数据库、创建新表、设置权限以及执行存储过程和视图等。以下是SQL的一些重要方面:SQL的目......
  • linux Libevent 轻量级事件通知库API简介和示例
    1.事件处理框架libevent是一个C语言编写,轻量级开源高性能事件框架。事件驱动,支持多种IO多路复用(如epoll),支持注册优先级等//头文件#include<event2/event.h>//创建一个事件处理框架structevent_base*event_base_new(void);//销毁一个事件处理框架voidevent_ba......
  • 一些WQL(WMI Query Language) 查询示例
    目录WQL介绍一些WQL查询示例怎么执行WQL查询?WMIC在PowerShell里输入命令WQL介绍WQL(WMIQueryLanguage)是一种SQL的变体,用于查询和设置Windows管理工具(WMI,WindowsManagementInstrumentation)的信息。WMI是Windows操作系统的一部分,提供了一个统一的方式来获取系统管理......
  • netcore net 递归查询示例
    ///<summary>///查询项目列表///</summary>///<paramname="userModel"></param>///<returns></returns>publicasyncTask<List<GetProjectListOutput>>GetProjectList......
  • Golang内存泄漏的代码示例
    以下是几个可能导致内存泄漏的Go代码示例:资源未关闭:funcreadFile(){ file,err:=os.Open("filename.txt") iferr!=nil{ //错误处理 return } //使用file进行读取操作 //...}在上述代码中,readFile函数打开了一个文件,但没有在使用完后调用file.Close()来关闭文......
  • Golang select语句代码示例
    在Go语言中,select语句用于多路选择(multiplexing),允许在多个通信操作中选择可用的操作进行执行。select语句的语法如下:select{case<-channel1://当channel1有数据可读时执行的代码casedata:=<-channel2://当channel2有数据可读时执行的代码,同时将读取的数据存储在......
  • 通过PowerShellPlus示例脚本学习PowerShell-读取VMware主机信息
    ##=====================================================================##Title:GetVI-HostList##Description:RetrieveVMwareHosts##Author:Idera##Date:9/11/2008##Input:-VIserver:VirtualInfrastructureserver##......