2008-08-13 [長年日記]
_ [vista][vbscript][work] Windows VistaでのWSH(VBScript)の管理者権限への昇格方法
Windows Vistaになってからクライアントでちょっとレジストリを書き換えたい・・・なんていう場合でもユーザーアカウント制御(UAC)のブロックに阻まれて困っていました。XP以前と同じように普通にWScript.ShellのRegWriteで書き込もうとしても失敗してしまいます。管理者権限を持っているアカウントで実行してもダメなんです。たとえば以下のようなコードですね。
1: Set WshShell=WScript.CreateObject("WScript.Shell")
2: WshShell.RegWrite "HKLM\SOFTWARE\Test\value", "test", "REG_SZ"
どうすれば良いのかと悩んだ末に妥協案として考えたのがregedit.exeを使う方法です。あらかじめ読み込ませたいregファイルを用意しておき、スクリプト中でregedit.exeを呼ぶことでレジストリへの書き込みが一応成功します。ただし、UACが有効な環境ですので、プロンプトが出ます。
これ自体は当たり前なのですが、以下のような点で困りものです。
- あらかじめファイルを用意せずに動的に値を決定するような場合、regファイルをスクリプト内でつど作成する必要がある。
- レジストリに書き込みに行く度に毎回ユーザーがクリックしなければならない。
- 単純にめんどくさい・・・。
できればユーザーの権限昇格のクリックは1度にしたい・・・そんな方法は無いものかと調べていたら以下のブログで紹介されていました。
do while WScript.Arguments.Count=0 and WScript.Version>=5.7
'Check if Vista
Set wmi = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set os = wmi.ExecQuery ("SELECT * FROM Win32_OperatingSystem")
For Each value in os
if left(value.Version,3)<6.0 then exit do 'Exit if not Vista
Next
'Run this script as admin.
Set sha=CreateObject("Shell.Application")
sha.ShellExecute "wscript.exe",""""+WScript.ScriptFullName+""" vista","","runas"
WScript.Quit
loop
Shell.ApplicationオブジェクトのShellExecuteメソッドで実現可能ということです。
実際に試してみたところ、きちんと昇格でき、レジストリへの書き込みもうまくいきました。昇格の確認画面が出てきます。
昇格後はXP以前と同じようにWScript.ShellのRegWriteでレジストリへの書き込みが実施できます。
これはすばらしい、詳しく知りたい!ということでMSの公式ドキュメントを探しているのですが見つかりません。書くべきとしては以下の場所だと思うのですが。
もうちょっと探したところ以下のブログに記述がありました。
The first annoyance was that there was no method to elevate an application from the command line or from the Run dialog box. So after asking around within Microsoft, I came across a sample script from John Stephens (a Software Design Engineer at Microsoft) that provided the information I needed. It turns out that if you pass the verb "runas" to either the ShellExecute API or to its COM equivalent, the ShellExecute method of Shell.Application, the application launched will prompt for elevation (see the sidebar for details).
MSが公式に公開している手法ではないということでしょうか??
いずれにせよこれでVista用にもスクリプトが普通に書けるようになった気がします。ちょっと幸せな気分になりました。
_ [work][thinking] 思うところはあるがここには書かない
転職する気でいるという話を聞く。人が増えたこともあって前にも増してよく聞く話になりました。思うところはありますが、ここには書かないことにします。よく聞くね、という話。
- 458 http://kashi57move.blogspot.jp/2011/02/wshwscript....
- 352 http://brainremix.blogspot.jp/2010/05/vbscript-and...
- 232 http://kashi57move.blogspot.com/2011/02/wshwscript...
- 158 http://brainremix.blogspot.com/2010/05/vbscript-an...
- 148 http://static.ak.facebook.com/connect/xd_arbiter.p...
- 58 http://taiseiko.blog.so-net.ne.jp/2010-06-17
- 53 http://d.hatena.ne.jp/rowlock/20091010/1255194472
- 41 http://oshiete.goo.ne.jp/qa/4916879.html
- 17 http://okwave.jp/qa/q4916879.html
- 12 http://ziddy.japan.zdnet.com/qa4916879.html
- 12 http://kashi57move.blogspot.jp/2011/02/wshwscript....
- 10 http://b.hatena.ne.jp/entry/ebi.dyndns.biz/diary/2...
- 8 http://jehupc.exblog.jp/13156287/
- 7 http://taiseiko.blog.so-net.ne.jp/archive/c2300351...
- 6 http://static.ak.facebook.com/connect/xd_arbiter.p...
- 6 http://search.yahoo.co.jp/search?p=vbs 管理者権限&searc...
- 5 http://okwave.jp/qa/q6742679.html
- 4 google検索(vbs vista)
- 4 google検索(ShellExecute runas)
- 4 google検索(uac vbs)
- 4 http://www.google.co.jp/hws/search?hl=ja&q=管理者権限 w...
- 4 http://www.bing.com/search?q=レジストリ 管理者権限 runas&for...
- 4 http://taiseiko.blog.so-net.ne.jp/_pages/user/ipho...
- 4 http://search.yahoo.co.jp/search?p=管理者権限 vbs&ei=UT...
- 4 http://jehupc.exblog.jp/blog.asp?tag=WSH&p=2&srl=1...
- 4 http://d.hatena.ne.jp/rowlock/
- 3 google検索(VBScript ユーザー権限)
- 3 google検索(0)
- 3 google検索(VBA regwrite 権限)
- 3 google検索(ShellExecute runas vbs xp)
- 3 google検索(VBスプリクト レジストリ 権限)
- 3 google検索(vbs windows7 管理者権限)
- 3 google検索(vbs uac)
- 3 google検索(vbs shellexecute)
- 3 google検索(vbs 昇格)
- 3 google検索(管理者権限 vbscript)
- 3 http://www.google.co.jp/hws/search?hl=ja&q=WScript...
- 3 http://www.google.co.jp/
- 3 http://www.bing.com/search?q=windows7 vbs 管理者権限&FO...
- 3 http://taiseiko.blog.so-net.ne.jp/archive/201006-1...
- 3 http://static.ak.facebook.com/connect/xd_arbiter.p...
- 3 http://static.ak.facebook.com/connect/xd_arbiter.p...
- 3 http://search.yahoo.co.jp/search?p=vbs uac&search....
- 3 http://search.yahoo.co.jp/search?p=vbs ユーザーアカウント制御...
- 3 http://search.yahoo.co.jp/search?p=vb6 管理者権限で実行&aq...
- 3 http://search.yahoo.co.jp/search?p=Windows7 レジストリ ...
- 3 http://questionbox.jp.msn.com/qa4916879.html
- 3 http://oshiete.goo.ne.jp/qa/6742679.html
- 3 http://fruitmail.excite.co.jp/search.gw?search=She...
- 3 http://cormura.net/search/vbscript 管理者権限
- 3 http://brainremix.blogspot.jp/2010/05/vbscript-and...
- 2 google検索(vbスクリプト 管理者権限)
- 2 http://www.google.com/m?q=windows2000 vbscript 管理者...
- 2 http://www.google.com/m?q=管理者権限 vbs&client=ms-oper...
- 2 google検索(f)
- 2 google検索(2)
- 2 google検索(wshshell 管理者)
- 2 google検索(wscript ShellExecute)
- 2 google検索(wscript 権限)
- 2 google検索(winmgmts 別の 権限)
- 2 google検索(windows7 vbs 管理者として実行)
- 2 google検索(windows7 ユーザ権限 VBS)
- 2 google検索(windows 7 vbs 管理者権限)
- 2 google検索(vbscript 管理者権限で実行)
- 2 google検索(vbs wscript runas ShellExecute)
- 2 google検索(vbs windows 7)
- 2 google検索(vbs uac)
- 2 google検索(vbs runas ShellExecute)
- 2 google検索(vbs 管理者権限で実行)
- 2 google検索(shellexecute vbs)
- 2 google検索(runas vbscript)
- 2 google検索(cscript vista ファイルに書き込めない)
- 2 google検索(Win7 レジストリ書き込み WSH)
- 2 google検索(Vista VBA Wscript.Shell)
- 2 google検索(VISTA UAC VBS exe実行)
- 2 google検索(VBScript 実行 権限 認識)
- 2 google検索(VBS ShellExecute)
- 2 google検索(ShellExecute vbs)
- 2 google検索(権限の昇格 管理者)
- 2 google検索(vbs 管理者権限)
- 2 google検索(wscript ShellExecute)
- 2 google検索(vista regedit vbs)
- 2 google検索(vista 管理者権限 スクリプト)
- 2 google検索(vbs 管理者権限で実行)
- 2 google検索(vbs 管理者として実行 vista)
- 2 google検索(vbs 管理者として実行)
- 2 google検索(sha.ShellExecute "wscript.exe",""""+WScript.ScriptFullName+""" vista","","runas")
- 2 google検索(Vista vbs カスタマイズ)
- 2 google検索(VBScript 管理者 権限 昇格)
- 2 google検索(ShellExecute runas)
- 2 google検索(ShellExecute runas)
- 2 google検索(Createobject("Wscript.Shell") 権限昇格)
- 2 google検索(権限 VBS )
- 2 google検索(wscript.shell exec 管理者)
- 2 google検索(�������i VBS)
- 2 google検索(CreateObject("Shell.Application") 権限)
- 2 http://www.google.co.jp/hws/search?hl=ja&q=vbs 管理者...
- 2 http://www.google.co.jp/hws/search?hl=ja&q=ShellEx...
- 2 http://www.bing.com/search?q=wsh exe 管理者権限 実行&src=...
- 2 http://www.bing.com/search?q=windows7 vbscript&qs=...