I tried to extract various information of remote PC from Python by WMI Library

Introduction

It was necessary to collect various information from hundreds of client PCs, and I enjoyed writing Python code, so this is a memo at that time. By the way, it is assumed to be in an AD domain environment.

Preparation

Remote PC

Check if the following services are running on the remote PC. If it doesn't seem to be running, set it to start automatically according to Group Policy or force.

AD group policy settings

Refer to the following page and enable RPC connection of remote PC using Group Policy. Enable remote management of Windows Firewall

To reflect the group policy immediately, enforce it on the remote PC side.

cmd.exe


gpupdate /force

Python preferences

Install the modules required for WMI on the remote connection side (Python code execution environment).

pip install pypiwin32
pip install wmi

Sample code

sample.py


# coding: utf-8

import wmi

NODE = "host-1.example.local"
USER = "Administrator@MYDOMAIN"
PASS = "P@ssW0rd!"

##WMI client initialization (when connecting to a local PC)
#conn = wmi.WMI()
##WMI client initialization (when connecting to a remote PC)
conn = wmi.WMI(NODE, user=USER, password=PASS)

#Get computer name
# Win32_OperatingSystem => http://www.wmifun.net/library/win32_operatingsystem.html
obj = conn.Win32_OperatingSystem()[0]
print("Hostname: %s" % obj.CSName)

##Check the free space of C drive(GB unit, up to 2 decimal places)
## Win32_LogicalDisk class=> http://www.wmifun.net/library/win32_logicaldisk.html
obj = conn.Win32_LogicalDisk(DeviceID='C:')[0]
free = float(obj.FreeSpace) / 1024 / 1024 / 1024
print('FreeSpace: {:.2f}'.format(free))

##Confirm user account currently logged on
## Win32_ComputerSystem class=> http://www.wmifun.net/library/win32_computersystem.html
obj = conn.Win32_ComputerSystem()[0]
print("LogonUser: %s" % obj.UserName)

##Confirmation of PC serial number (serial number)
## Win32_ComputerSystemProduct class=> http://www.wmifun.net/library/win32_computersystemproduct.html
obj = conn.Win32_ComputerSystemProduct()[0]
print("SerialNo: %s" % obj.IdentifyingNumber)

##Command execution
## Win32_Process class=> http://www.wmifun.net/library/win32_process.html
CMD = "notepad.exe"
CUD = None
##Separator character for full path(\)Further escape
#CMD = "C:\\Users\\Public\\Desktop\\example.exe"
#CUD = "C:\\Users\\Public\\Desktop"
SW_SHOWNORMAL = 1

p_startup = conn.Win32_ProcessStartup.new()
p_startup.ShowWindow = SW_SHOWNORMAL
pid, result = conn.Win32_Process.Create(
    CommandLine=CMD,
    CurrentDirectory=CUD,
    ProcessStartupInformation=p_startup
)
if result == 0:
    print "ProcessId: %d" % pid
else:
    raise RuntimeError, "Problem creating process: %d" % result

Execution result

% python sample.py
Hostname: HOST-1
FreeSpace: 18.53
LogonUser: MYDOMAIN\user001
SerialNo: JPA12345LF
ProcessId: 3784

Other

--When executing an application on a remote PC, the screen is not displayed for interactive applications (Notepad, etc.). This is a security specification. (> = Windows XP SP3?) --To use WMI, you need to connect with a local administrator account or a domain administrator account. (= Access Denied)

reference

wmi Tutorial WMI Library

Recommended Posts

I tried to extract various information of remote PC from Python by WMI Library
I tried to get various information from the codeforces API
[Python] I tried to get various information using YouTube Data API!
I tried to communicate with a remote server by Socket communication with Python.
I tried to verify and analyze the acceleration of Python by Cython
I tried to get the movie information of TMDb API with Python
[Introduction to Docker] I tried to summarize various Docker knowledge obtained by studying (Windows / Python)
I tried to extract features with SIFT of OpenCV
I tried to summarize how to use matplotlib of python
I wanted to use the Python library from MATLAB
I tried to visualize the spacha information of VTuber
[Python] I tried to get Json of squid ring 2
I tried to summarize the string operations of Python
I tried to make a function to retrieve data from database column by column using sql with sqlite3 of python [sqlite3, sql, pandas]
I tried to find the entropy of the image with python
I tried to extract characters from subtitles (OpenCV: tesseract-ocr edition)
I tried to get the location information of Odakyu Bus
I tried to create API list.csv in Python from swagger.yaml
I tried using the Python library from Ruby with PyCall
I tried to make various "dummy data" with Python faker
I tried various methods to send Japanese mail with Python
I want to start a lot of processes from python
[Python] I tried to visualize the follow relationship of Twitter
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
I tried to implement blackjack of card game in Python
I tried to touch Python (installation)
I tried to make a regular expression of "amount" using Python
I want to extract an arbitrary URL from the character string of the html source with python
I tried to make a regular expression of "time" using Python
I tried to create a list of prime numbers with python
I tried to make a regular expression of "date" using Python
I tried to verify the speaker identification by the Speaker Recognition API of Azure Cognitive Services with Python. # 1
I tried to fix "I tried stochastic simulation of bingo game with Python"
I tried to verify the speaker identification by the Speaker Recognition API of Azure Cognitive Services with Python. # 2
I tried to summarize the contents of each package saved by Python pip in one line
I tried to visualize Boeing of violin performance by pose estimation
I tried to solve the shift scheduling problem by various methods
I tried to extract players and skill names from sports articles
Various ways to create an array of numbers from 1 to 10 in Python.
I tried to improve the efficiency of daily work with Python
I tried to automatically collect images of Kanna Hashimoto with Python! !!
PhytoMine-I tried to get the genetic information of plants with Python
[Python] I tried to reproduce the emergency escape program to return from the world to return from the modified world of "The disappearance of Haruhi Suzumiya"
I tried to open the latest data of the Excel file managed by date in the folder with Python
A super introduction to Django by Python beginners! Part 2 I tried using the convenient functions of the template
Python programming: I tried to get company information (crawling) from Yahoo Finance in the US using BeautifulSoup4
I tried to deliver mail from Node.js and Python using the mail delivery service (SendGrid) of IBM Cloud!
[Data science basics] I tried saving from csv to mysql with python
I tried to extract characters from subtitles (OpenCV: Google Cloud Vision API)
I tried to implement Bayesian linear regression by Gibbs sampling in python
I tried to get the authentication code of Qiita API with Python.
I tried to automatically extract the movements of PES players with software
(Python) I tried to analyze 1 million hands ~ I tried to estimate the number of AA ~
I tried to find the optimal path of the dreamland by (quantum) annealing
I tried to extract and illustrate the stage of the story using COTOHA
I tried various patterns of date strings to be entered in pandas.to_datetime
I tried fMRI data analysis with python (Introduction to brain information decoding)
I tried putting various versions of Python + OpenCV + FFmpeg environment on Mac
I tried to streamline the standard role of new employees with Python
I tried to verify the result of A / B test by chi-square test
[Lambda] I tried to incorporate an external module of python via S3