SQLの窓

2015年02月25日


VBScript : WMI と NT Provider(ADSI) をそれぞれ使用して、共有を『登録』『一覧』『削除』するサンプル

処理が失敗して共有がもし残った場合は、Windows のコンピュータの管理(%windir%\system32\compmgmt.msc /s)から『共有の停止』を実行します

サンプルでは、WMI と ADSI の両方で処理を記述していますが、WMI のほうが今では推奨されると思います。ADSI のドキュメントは解りにくい上にちゃんとしたものが無いので、これ以上の事をしようとすると苦労すると思います。但し、ローカルユーザを自動的に大量に登録する必要がある場合は、NT Provider は比較的簡単かもしれません。

関連する Microsoft のドキュメント

ADSI を使用してファイル共有を管理する方法
How Can I Share a Folder on a Remote Computer?
Win32_Share class
<JOB>
<OBJECT id="WshShell" progid="WScript.Shell" />
<OBJECT id="Fso" progid="Scripting.FileSystemObject" />
<SCRIPT language="VBScript">

Crun

' **************************************
' スクリプトが存在するディレクトリを取得
' **************************************
strScriptPath = WScript.ScriptFullName
Set obj = Fso.GetFile( strScriptPath )
Set obj = obj.ParentFolder
strScriptPath = obj.Path

' **************************************
' 共有作成(WMI)
' **************************************
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set objShare = objWMIService.Get("Win32_Share")
Call objShare.Create( strScriptPath, "ここが共有名ですA", 0 )

' **************************************
' 共有一覧(WMI)
' **************************************
Set colShares = objWMIService.ExecQuery("Select * from Win32_Share")
Wscript.Echo "【共有一覧】"
Wscript.Echo "----------------------------------------------------"
For Each Share In colShares
	Wscript.Echo RpadB(Share.Name, " ", 20) & " : " & Share.Path
Next
Wscript.Echo

' **************************************
' 共有削除(WMI)
' **************************************
Set colShares = objWMIService.ExecQuery("Select * from Win32_Share where Name = 'ここが共有名ですA'")
For Each Share In colShares
	Share.Delete
Next
Wscript.Echo

' **************************************
' 共有作成(WinNT Provider)
' **************************************
Set colShares = GetObject("WinNT://./LanmanServer")
Set objShare = colShares.Create("fileshare", "ここが共有名ですB")
objShare.Path = strScriptPath
objShare.SetInfo

' **************************************
' 共有一覧(WinNT Provider)
' **************************************
Wscript.Echo "【共有一覧】"
Wscript.Echo "----------------------------------------------------"
For Each Share In colShares
	Wscript.Echo RpadB(Share.Name, " ", 20) & " : " & Share.Path
Next
Wscript.Echo

' **************************************
' 共有削除(WinNT Provider)
' **************************************
colShares.Delete "fileshare", "ここが共有名ですB"
Wscript.Echo

' 終了確認
Wscript.Echo "処理が終了しました"

' **************************************
' Cscript.exe で実行を強制
' Cscript.exe の実行終了後 pause で一時停止
' **************************************
Function Crun( )

	Dim str

	str = WScript.FullName
	str = Right( str, 11 )
	str = Ucase( str )
	if str <> "CSCRIPT.EXE" then
		str = WScript.ScriptFullName

		strParam = " "
		For I = 0 to Wscript.Arguments.Count - 1
			if instr(Wscript.Arguments(I), " ") < 1 then
				strParam = strParam & Wscript.Arguments(I) & " "
			else
				strParam = strParam & Dd(Wscript.Arguments(I)) & " "
			end if
		Next
		Call WshShell.Run( "cmd.exe /c cscript.exe " & Dd(str) & strParam & " & pause", 3 )
		WScript.Quit
	end if

End Function
' **************************************
' 文字列を " で囲む関数
' **************************************
Function Dd( strValue )

	Dd = """" & strValue & """"

End function

' **************************************
' 文字列の右側をスペースで埋める
' **************************************
Function RpadB( strValue, str, nLen )

	Dim strWork,nLen2

	strWork = Left( strValue & String(nLen,str), nLen )
	nLen2 = nLen

	Do While ByteLen( strWork ) > nLen
		nLen2 = nLen2 - 1
		if nLen2 <= 0 then
			Exit Do
		end if
		strWork = Left( strValue & String(nLen,str), nLen2 )
	Loop
	RpadB = strWork

End function
' **************************************
' 文字列のバイト計算
' **************************************
Function ByteLen( strTarget )

	Dim i,nLen,nRet,strMoji,nAsc

	nRet = 0

	nLen = Len( strTarget )

	For i = 1 to nLen
		nRet = nRet + 2
		strMoji = Mid( strTarget, i, 1 )
		nAsc = Asc( strMoji )
		if &H0 <= nAsc and nAsc <= &H80 then
			nRet = nRet - 1
		end if
		if &HA0 <= nAsc and nAsc <= &HDF then
			nRet = nRet - 1
		end if
		if &HFD <= nAsc and nAsc <= &HFF then
			nRet = nRet - 1
		end if
	Next

	ByteLen = nRet

End Function
</SCRIPT>
</JOB>


実行結果

Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

【共有一覧】
----------------------------------------------------
ADMIN$               : C:\Windows
C$                   : C:\
D$                   : D:\
IPC$                 :
print$               : C:\Windows\system32\spool\drivers
temp                 : C:\Users\lightbox\AppData\Local\Temp
Users                : C:\Users
ここが共有名ですA    : C:\user\vbs


【共有一覧】
----------------------------------------------------
print$               : C:\Windows\system32\spool\drivers
temp                 : C:\Users\lightbox\AppData\Local\Temp
Users                : C:\Users
ここが共有名ですB    : C:\user\vbs


処理が終了しました
続行するには何かキーを押してください . . .
※ 実行終了後、共有は削除されています
posted by lightbox at 2015-02-25 16:00 | VBS + WMI | このブログの読者になる | 更新情報をチェックする
container 終わり



フリーフォントで簡単ロゴ作成
フリーフォントでボタン素材作成
フリーフォントで吹き出し画像作成
フリーフォントではんこ画像作成
ほぼ自由に利用できるフリーフォント
フリーフォントの書体見本とサンプル
画像を大きく見る為のウインドウを開くボタンの作成

CSS ドロップシャドウの参考デモ
イラストAC
ぱくたそ
写真素材 足成
フリーフォント一覧
utf8 文字ツール
右サイド 終わり
base 終わり