---
title: "xp_cmdshell"
author: "wuhen86"
date: "2009-12-02T12:16:00Z"
tags: []
slug: "xp-cmdshell"
draft: false
lastmod: "2009-12-02T12:16:00Z"
id: 747046
url: "https://wuhen86.iteye.com/blog/747046"
---

###### 语法

**`xp_cmdshell`** {`'command_string'`}`[, no_output`]

###### 参数

**'command\_string'**

是在操作系统命令行解释器上执行的命令字符串。*command\_string* 的数据类型为 **varchar(255)** 或 **nvarchar(4000)**，没有默认值。*command\_string* 不能包含一对以上的双引号。如果由 *command\_string* 引用的文件路径或程序名称中有空格，则需要使用一对引号。如果使用嵌入空格不方便，可考虑使用 FAT 8.3 文件名作为解决办法。

**no\_output**

是可选参数，表示执行给定的 *command\_string*，但不向客户端返回任何输出。

###### 返回代码值

0（成功）或 1（失败）

###### 结果集

执行下列 **xp\_cmdshell** 语句将返回当前目录的目录列表。

```
xp_cmdshell 'dir *.exe'
```

行以 **nvarchar(255)** 列的形式返回。

执行下列 **xp\_cmdshell** 语句将返回随后的结果集：

```
xp_cmdshell 'dir *.exe', NO_OUTPUT
```

下面是结果：

```
The command(s) completed successfully.
```

###### 注释

**xp\_cmdshell** 以同步方式操作。在命令行解释器命令执行完毕之前，不会返回控制。

当授予用户执行权限时，用户能在 Microsoft Windows NT® 命令行解释器上执行运行 Microsoft SQL Server™ 的帐户有权执行的任何操作系统命令。

默认情况下，只有 **sysadmin** 固定服务器角色的成员才能执行此扩展存储过程。但是，也可以授予其他用户执行此存储过程的权限。

当作为 **sysadmin** 固定服务器角色成员的用户唤醒调用 **xp\_cmdshell** 时，将在运行 SQL Server 服务的安全上下文中执行 **xp\_cmdshell**。当用户不是 **sysadmin** 组的成员时，**xp\_cmdshell** 将模拟使用 **xp\_sqlagent\_proxy\_account** 指定的 SQL Server 代理程序的代理帐户。如果代理帐户不能用，则 **xp\_cmdshell** 将失败。这只是针对于 Microsoft® Windows NT® 4.0 和 Windows 2000。在 Windows 9.x 上，没有模拟，且 **xp\_cmdshell** 始终在启动 SQL Server 的 Windows 9.x 用户的安全上下文下执行。

**说明**  在早期版本中，获得 **xp\_cmdshell** 执行权限的用户在 MSSQLServer 服务的用户帐户上下文中运行命令。可以通过配置选项配置 SQL Server，以便对 SQL Server 无 **sa** 访问权限的用户能够在 **SQLExecutiveCmdExec** Windows NT 帐户的上下文中运行 **xp\_cmdshell**。在 SQL Server 7.0 中，该帐户称为 **SQLAgentCmdExec**。现在，不是 **sysadmin** 固定服务器角色成员的用户将在该帐户上下文中运行命令，而无需再进行配置更改。

###### 权限

**xp\_deletemail** 的执行权限默认授予 **sysadmin** 固定服务器角色的成员，但可以授予其他用户。

**重要**  如果为 MSSQLServer 服务选用的 Windows NT 帐户不是本地管理员组的成员，则非 **sysadmin** 固定服务器角色成员的用户将无法执行 **xp\_cmdshell**。

###### 示例

###### A. 返回可执行文件列表

下例显示执行目录命令的 **xp\_cmdshell** 扩展存储过程。

```
EXEC master..xp_cmdshell 'dir *.exe'
```

###### B. 使用 Windows NT net 命令

下例显示 **xp\_cmdshell** 在存储过程中的使用。下例先用 **net send** 通知用户 SQL Server 即将关闭，然后用 **net pause** 暂停服务器，最后用 **net stop** 关闭服务器。

```
Create PROC shutdown10
AS
EXEC xp_cmdshell 'net send /domain:SQL_USERS ''SQL Server shutting down
in 10 minutes. No more connections allowed.', no_output
EXEC xp_cmdshell 'net pause sqlserver'
WAITFOR DELAY '00:05:00'
EXEC xp_cmdshell 'net send /domain: SQL_USERS ''SQL Server shutting down
in 5 minutes.', no_output
WAITFOR DELAY '00:04:00'
EXEC xp_cmdshell 'net send /domain:SQL_USERS ''SQL Server shutting down
in 1 minute. Log off now.', no_output
WAITFOR DELAY '00:01:00'
EXEC xp_cmdshell 'net stop sqlserver', no_output
```

###### C. 不返回输出

下例使用 **xp\_cmdshell** 执行命令字符串，且不向客户端返回输出。

```
USE master
EXEC xp_cmdshell 'copy c:\sqldumps\pubs.dmp \\server2\backups\sqldumps',
NO_OUTPUT
```

###### D. 使用返回状态

在下例中，**xp\_cmdshell** 扩展存储过程也给出了返回状态。返回代码值存储在变量 **@result** 中。

```
DECLARE @result int
EXEC @result = xp_cmdshell 'dir *.exe'
IF (@result = 0)
PRINT 'Success'
ELSE
PRINT 'Failure'
```

###### E. 将变量内容写入文件

下例将当前目录内容写入当前服务器目录下名为 dir\_out.txt 的文件中。

```
DECLARE @cmd sysname, @var sysname
SET @var = 'dir /p'
SET @cmd = 'echo ' + @var + ' > dir_out.txt'
EXEC master..xp_cmdshell @cmd
```