LoginSignup
1
1

More than 5 years have passed since last update.

Windows ローカル グループのメンバーを取得する

Last updated at Posted at 2015-12-11

とりあえずこんな感じです。

get_localgroup_member.h
#include<string>
#include<vector>

class GetLocalGroupMember {
    std::string localgroup_name, filename, output_command, delete_command;
    void get_member();
    void get_localgroup_member();
    void delete_group_name_user();
public:
    GetLocalGroupMember(std::string localgroup_name);
    std::vector<std::string> member_list;
};
get_localgroup_member.cpp
#include "get_localgroup_member.h"
#include <fstream>
#include <cstdlib>

GetLocalGroupMember::GetLocalGroupMember(std::string localgroup_name) {
    this->localgroup_name = localgroup_name;
    this->filename = this->localgroup_name + ".txt";
    this->output_command = "net localgroup " + localgroup_name + " > " + this->filename;
    this->delete_command = "del /f " + this->filename;
    this->get_localgroup_member();
    this->delete_group_name_user();
}

void GetLocalGroupMember::get_member() {
    std::ifstream st(this->filename.c_str());
    std::string data;
    while (data != "コマンドは正常に終了しました。") {
        std::getline(st, data);
        if (data != "" && data != "コマンドは正常に終了しました。") this->member_list.emplace_back(data);
    }
    while (this->member_list.front() != "-------------------------------------------------------------------------------")
        this->member_list.erase(this->member_list.begin());
    this->member_list.erase(this->member_list.begin());
}

void GetLocalGroupMember::get_localgroup_member() {
    std::system(this->output_command.c_str());
    this->get_member();
    std::system(this->delete_command.c_str());
}

void GetLocalGroupMember::delete_group_name_user() {
    for (size_t i = 0; i < this->member_list.size(); i++) {
        if (this->member_list[i].find_first_of("$") != std::string::npos 
            || this->member_list[i].find_first_of("\\") != std::string::npos) {
            this->member_list.erase(this->member_list.begin() + i);
            i--;
        }
    }
}

使い方

main.cpp
#include "get_localgroup_member.h"
#include <iostream>

int main() {
    for (auto i : GetLocalGroupMember("Administrators").member_list) std::cout << i << std::endl;
    return 0;
}

クラスにしたのは単に関数内にめちゃくそ変数定義してる状態が嫌だったからです。

クラス使わずに書いたやつ

使い方を分けて書くの面倒なのでメイン関数一緒に乗っけちゃいます。

main.cpp
#include <fstream>
#include <string>
#include <vector>
#include <iostream>
#include <cstdlib>

std::vector<std::string> get_member(const char* filepath) {
    std::ifstream st(filepath);
    std::vector<std::string> re;
    std::string data;
    while (data != "コマンドは正常に終了しました。") {
        std::getline(st, data);
        if (data != "" && data != "コマンドは正常に終了しました。") re.emplace_back(data);
    }
    while (re.front() != "Administrator") re.erase(re.begin());
    return re;
}

std::vector<std::string> get_localgroup_member(std::string localgroup_name) {
    std::string filename = localgroup_name + ".txt", 
        output_command = "net localgroup " + localgroup_name + " > " + filename,
        delete_command = "del /f " + filename;
    std::system(output_command.c_str());
    std::vector<std::string> data = get_member(filename.c_str());
    std::system(delete_command.c_str());
    return data;
}

int main() {
    for (auto i : get_localgroup_member("Administrators")) std::cout << i << std::endl;
    return 0;
}

動作確認ができているローカル グループ
・Administrators
・HomeUsers
・Users
・Guests
・IIS_IUSRS
名前に空白が入ってるローカル グループの名前取得には使えません。
それより書き出さずに取得する方法ないかな…。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1