首页 > 数据库 >通过PowerShellPlus示例脚本学习PowerShell-通过WIndows集成验证登录SQLServer

通过PowerShellPlus示例脚本学习PowerShell-通过WIndows集成验证登录SQLServer

时间:2023-11-20 13:32:16浏览次数:25  
标签:示例 WIndows xxx SQLServer Write ## Host Connect ipAddress

## =====================================================================
## Title       : Connect-MSSQL-IPWindowsAuth
## Description : Connect to SQL Server using IP address, instance and 
##                  Windows authentication
## Author      : Idera
## Date        : 1/28/2009
## Input       : -ipAddress < xxx.xxx.xxx.xxx | xxx.xxx.xxx.xxx\instance >
##               -verbose 
##               -debug	
## Output      : Database names and owners
## Usage			: PS> .\Connect-MSSQL-IPWindowsAuth -ipAddress 127.0.0.1 -verbose -debug
## Notes 		: 
##	Tag			: MSSQL, connect, IP, Windows Authentication
## Change Log	:
##   4/1/2009 - Revised SMO Assemblies
##   4/1/2009 - Added input prompts and write-verbose statements
## =====================================================================
 
param
(
  	[string]$ipAddress = "$(Read-Host 'IP Address' [e.g. 127.0.0.1])",
	[switch]$verbose = $true,
	[switch]$debug = $false
)

function main()
{
	if ($verbose) {$VerbosePreference = "Continue"}
	if ($debug) {$DebugPreference = "Continue"}

	Write-Verbose "Connect to SQL Server using IP address, instance and Windows authentication..."
	Connect-MSSQL-IPWindowsAuth $ipAddress
}

function Connect-MSSQL-IPWindowsAuth($ipAddress)
{
	trap [Exception] 
	{
		write-error $("TRAPPED: " + $_.Exception.Message);
		continue;
	}
	
	# Load-SMO assemblies
	[void][reflection.assembly]::LoadWithPartialName( "Microsoft.SqlServer.Management.Common" );
	[void][reflection.assembly]::LoadWithPartialName( "Microsoft.SqlServer.SmoEnum" );
	[void][reflection.assembly]::LoadWithPartialName( "Microsoft.SqlServer.Smo" );
	[void][reflection.assembly]::LoadWithPartialName( "Microsoft.SqlServer.SmoExtended " );
	
	# Instantiate a server object
	# TIP: using PowerShell "`" to signify line continuation
	Write-Debug "Creating SMO Server object..."
	
	$Smo = "Microsoft.SqlServer.Management.Smo."
	$smoServer = new-object ($Smo + 'server') "$ipAddress"

	# Use Windows Authentication by setting LoginSecure to TRUE
	Write-Debug "Setting Windows Authentication mode..."
	$smoServer.ConnectionContext.set_LoginSecure($TRUE)
	
	# Clear the screen
	# TIP: cls will clear the PowerShell console
	cls
	Write-Host Your connection string contains these values:
	Write-Host
	$smoServer.ConnectionContext.ConnectionString.Split(";")
	Write-Host
	
	# List information about the databases
	Write-Host "Databases on " $ipAddress
	Write-Host
	foreach ($db in $smoServer.Databases) 
	{
		write-host "Database Name : " $db.Name
		write-host "Owner         : " $db.Owner
		write-host
	}
}

main

本脚本是使用IP地址和Windows集成验证登录SQLServer数据库。脚本中没有新的Cmdlets。集成验证的设置是行58行。

标签:示例,WIndows,xxx,SQLServer,Write,##,Host,Connect,ipAddress
From: https://blog.51cto.com/u_3353175/8487805

相关文章

  • 简单统计SQLSERVER用户数据表大小(包括记录总数和空间占用情况)
    在SQLSERVER,简单的组合sp_spaceused和sp_MSforeachtable这两个存储过程,可以方便的统计出用户数据表的大小,包括记录总数和空间占用情况,非常实用,在SqlServer2K和SqlServer2005中都测试通过。/*1.execsp_spaceused'表名'      (SQL统计数据,大量事务操作后可能不准)2.exe......
  • Knative Eventing Parallel Flow 示例
    环境说明◼PingSource负责生成event◼Parallel中有两个Branch◆第一个分支接受时间为偶数的事件◆第二个分支接受时间为奇数的事件◼所有分支的最终结果均发往ksvc/event-display,内容格式化CloudEvent存储入日志创建名称空间#kubectlcreatensparallel-demo......
  • springboot 控制序列化反序列化示例(接口返回数据处理/接口接收数据处理)
    1.返回Long转JSONpackagecom.mingx.drone.config;importcom.fasterxml.jackson.core.JsonGenerator;importcom.fasterxml.jackson.databind.JsonSerializer;importcom.fasterxml.jackson.databind.SerializerProvider;importjava.io.IOException;/***@Descript......
  • [oeasy]python001_先跑起来_python_三大系统选择_windows_mac_linux
    先跑起来......
  • Knative Eventing Sequence Flow 示例
    环境说明◼PingSource负责生成event◼Event由Sequence中的各Step顺次处理◆各Step都运行一个appender应用◆分别向收到的数据尾部附加自定义的专有数据项◼最终结果发往ksvc/event-display环境示意图创建名称空间#kubectlcreatenssequence-demonamespace/seq......
  • 8.5 Windows驱动开发:内核注册表增删改查
    注册表是Windows中的一个重要的数据库,用于存储系统和应用程序的设置信息,注册表是一个巨大的树形结构,无论在应用层还是内核层操作注册表都有独立的API函数可以使用,而在内核中读写注册表则需要使用内核装用API函数,如下将依次介绍并封装一些案例,实现对注册表的创建,删除,更新,查询等操作......
  • 查看SQLServer平均最耗资源时间的SQL语句
    SELECT(total_elapsed_time/execution_count)/1000N'平均时间ms',total_elapsed_time/1000N'总花费时间ms',total_worker_time/1000N'所用的CPU总时间ms',total_physical_reads......
  • 8.1 Windows驱动开发:内核文件读写系列函数
    在应用层下的文件操作只需要调用微软应用层下的API函数及C库标准函数即可,而如果在内核中读写文件则应用层的API显然是无法被使用的,内核层需要使用内核专有API,某些应用层下的API只需要增加Zw开头即可在内核中使用,例如本章要讲解的文件与目录操作相关函数,多数ARK反内核工具都具有对......
  • Windows部署Python环境
    下载Python解释器进入Python官网。在Downloads下,选择Windows。找到自己需要的Python版本,点击进行下载。双击运行Python解释器安装包。选中Addpython.exetoPATH,然后单击Customizeinstallation进行自定义安装。注意,一定要选择Addpython.exetoPATH将python命令加......
  • Windows 快捷键使用
    Ctrl+A全选Ctrl+S保存Ctrl+X剪切Ctrl+Z撤销Ctrl+E打开我的电脑Dos打开cmd开始+系统+命令提示符Win+R输入CMD打开控制台在任意的文件夹下按住shift+鼠标右键点击打开命令行窗口资源管理器的地址栏前面加cmd路径管理员方式运行常用的Dos命令#盘符切换字......