3流プログラマのメモ書き

元開発職→社内SE→派遣で営業支援の三流プログラマのIT技術メモ書き。 このメモが忘れっぽい自分とググってきた技術者の役に立ってくれれば幸いです。(jehupc.exblog.jpから移転中)

(.Net)共有フォルダにアクセスしているセッションを取得する

Windows共有フォルダにアクセスしているセッション情報を .NET アプリケーションから取得する方法です。

クライアントPCから実行し、共有フォルダを提供しているサーバPCのセッションを取得します。

コマンドでするなら、net session で取得することができますが、共有フォルダを提供しているコンピュータでしか実行できません。また、整形して出力するので長いユーザ名だと切れてしまいそうです。(クライアントは文字列途中で切れてます)

ということで WMI で取得することにしました。

WMI Code Creator を使うと簡単にセッション取得のコードが生成できます。

C#だと下記のような感じです。

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Management;

 

namespace WMISample{

public class MyQuerySample : System.Windows.Forms.Form{

private System.Windows.Forms.Label userNameLabel;

private System.Windows.Forms.TextBox userNameBox;

private System.Windows.Forms.TextBox passwordBox;

private System.Windows.Forms.Label passwordLabel;

private System.Windows.Forms.Button OKButton;

private System.Windows.Forms.Button cancelButton;

private System.ComponentModel.Container components = null;

 

public MyQuerySample(){

InitializeComponent(); }

 

protected override void Dispose( bool disposing ) {

if( disposing ) {

if (components != null) {

components.Dispose(); }

}

base.Dispose( disposing );

}

 

private void InitializeComponent() {

//省略

}

 

[STAThread]

static void Main() {

Application.Run(new MyQuerySample());

}

 

private void OKButton_Click(object sender, System.EventArgs e) {

try {

ConnectionOptions connection = new ConnectionOptions();

connection.Username = userNameBox.Text;

connection.Password = passwordBox.Text;

connection.Authority = "ntlmdomain:hogedomain";

 

ManagementScope scope = new ManagementScope(

"\\\\fileserver\\root\\CIMV2", connection);

scope.Connect();

 

ObjectQuery query= new ObjectQuery(

"SELECT * FROM Win32_ServerConnection");

 

ManagementObjectSearcher searcher =

new ManagementObjectSearcher(scope, query);

 

foreach (ManagementObject queryObj in searcher.Get()){

Console.WriteLine("-----------------------------------");

Console.WriteLine("Win32_ServerConnection instance");

Console.WriteLine("-----------------------------------");

Console.WriteLine("ActiveTime: {0}", queryObj["ActiveTime"]);

Console.WriteLine("Caption: {0}", queryObj["Caption"]);

Console.WriteLine("ComputerName: {0}", queryObj["ComputerName"]);

Console.WriteLine("ConnectionID: {0}", queryObj["ConnectionID"]);

Console.WriteLine("Description: {0}", queryObj["Description"]);

Console.WriteLine("InstallDate: {0}", queryObj["InstallDate"]);

Console.WriteLine("Name: {0}", queryObj["Name"]);

Console.WriteLine("NumberOfFiles: {0}", queryObj["NumberOfFiles"]);

Console.WriteLine("NumberOfUsers: {0}", queryObj["NumberOfUsers"]);

Console.WriteLine("ShareName: {0}", queryObj["ShareName"]);

Console.WriteLine("Status: {0}", queryObj["Status"]);

Console.WriteLine("UserName: {0}", queryObj["UserName"]);

}

Close();

}catch(ManagementException err) {

MessageBox.Show("An error occurred while querying for WMI data: " + err.Message);

}catch(System.UnauthorizedAccessException unauthorizedErr) {

MessageBox.Show("Connection error (user name or password might be incorrect): " + unauthorizedErr.Message);

}

}

 

private void cancelButton_Click(object sender, System.EventArgs e) {

Close();

}

}

}

VBScriptだと下記のようになります。(文字数制限の関係でハイライトOFFです。)

strComputer = "fileserver"

strDomain = "hogedomain"

Wscript.StdOut.Write "Please enter your user name:"

strUser = Wscript.StdIn.ReadLine

Set objPassword = CreateObject("ScriptPW.Password")

Wscript.StdOut.Write "Please enter your password:"

strPassword = objPassword.GetPassword()

Wscript.Echo

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")

Set objWMIService = objSWbemLocator.ConnectServer(strComputer, _

"root\CIMV2", _

strUser, _

strPassword, _

"MS_409", _

"ntlmdomain:" + strDomain)

Set colItems = objWMIService.ExecQuery( _

"SELECT * FROM Win32_ServerConnection",,48)

For Each objItem in colItems

Wscript.Echo "-----------------------------------"

Wscript.Echo "Win32_ServerConnection instance"

Wscript.Echo "-----------------------------------"

Wscript.Echo "ActiveTime: " & objItem.ActiveTime

Wscript.Echo "Caption: " & objItem.Caption

Wscript.Echo "ComputerName: " & objItem.ComputerName

Wscript.Echo "ConnectionID: " & objItem.ConnectionID

Wscript.Echo "Description: " & objItem.Description

Wscript.Echo "InstallDate: " & objItem.InstallDate

Wscript.Echo "Name: " & objItem.Name

Wscript.Echo "NumberOfFiles: " & objItem.NumberOfFiles

Wscript.Echo "NumberOfUsers: " & objItem.NumberOfUsers

Wscript.Echo "ShareName: " & objItem.ShareName

Wscript.Echo "Status: " & objItem.Status

Wscript.Echo "UserName: " & objItem.UserName

Next

参考:

net sessionコマンド - 管理者必見! ネットワーク・コマンド集:ITpro net session コマンドの説明です。

[WMI] 共有フォルダのセッションを取得するサンプル: Win32_ServerConnection - WMI Sample

[WMI] Win32_ServerConnection クラス - WMI Library

@IT:Win32 APIやDLL関数を呼び出すには? Win32APIを呼び出す方法です。