當(dāng)作為 sysadmin 固定服務(wù)器角色成員的用戶喚醒調(diào)用 xp_cmdshell 時(shí),將在運(yùn)行 sql server 服務(wù)的安全上下文中執(zhí)行 xp_cmdshell。當(dāng)用戶不是 sysadmin 組的成員時(shí),xp_cmdshell 將模擬使用 xp_sqlagent_proxy_account 指定的 sql server 代理程序的代理帳戶。如果代理帳戶不能用,則 xp_cmdshell 將失敗。這只是針對(duì)于 microsoft® windows nt® 4.0 和 windows 2000。在 windows 9.x 上,沒有模擬,且 xp_cmdshell 始終在啟動(dòng) sql server 的 windows 9.x 用戶的安全上下文下執(zhí)行。
說(shuō)明 在早期版本中,獲得 xp_cmdshell 執(zhí)行權(quán)限的用戶在 mssqlserver 服務(wù)的用戶帳戶上下文中運(yùn)行命令??梢酝ㄟ^(guò)配置選項(xiàng)配置 sql server,以便對(duì) sql server 無(wú) sa 訪問權(quán)限的用戶能夠在 sqlexecutivecmdexec windows nt 帳戶的上下文中運(yùn)行 xp_cmdshell。在 sql server 7.0 中,該帳戶稱為 sqlagentcmdexec?,F(xiàn)在,不是 sysadmin 固定服務(wù)器角色成員的用戶將在該帳戶上下文中運(yùn)行命令,而無(wú)需再進(jìn)行配置更改。
exec master..xp_cmdshell 'dir *.exe' b. 使用 windows nt net 命令 下例顯示 xp_cmdshell 在存儲(chǔ)過(guò)程中的使用。下例先用 net send 通知用戶 sql server 即將關(guān)閉,然后用 net pause 暫停服務(wù)器,最后用 net stop 關(guān)閉服務(wù)器。
create proc shutdown10asexec xp_cmdshell 'net send /domain:sql_users ''sql server shutting down in 10 minutes. no more connections allowed.', no_outputexec 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_outputwaitfor delay '00:04:00'exec xp_cmdshell 'net send /domain:sql_users ''sql server shutting down in 1 minute. log off now.', no_outputwaitfor delay '00:01:00'exec xp_cmdshell 'net stop sqlserver', no_output c. 不返回輸出 下例使用 xp_cmdshell 執(zhí)行命令字符串,且不向客戶端返回輸出。
use masterexec xp_cmdshell 'copy c: qldumps/pubs.dmp / erver2/backups qldumps', no_output d. 使用返回狀態(tài) 在下例中,xp_cmdshell 擴(kuò)展存儲(chǔ)過(guò)程也給出了返回狀態(tài)。返回代碼值存儲(chǔ)在變量 @result 中。