WMIを初めて使用しました。 WMIを通じてリモートシステムに接続するアプリケーションを作成しようとしています。ユーザーログイン、ユーザーがログオフしたり、ユーザーがアイドルになったりするのを監視するには、このアプリケーションが必要です。ログオン時に、システム上でアプリケーションが起動するプロセスを終了します。ログオフまたはアイドル時に、私はサービスを再開したいと思います。この方法では、ユーザーに影響を与えずに、マシンの使用を最大限にすることを望んでいます。私はmakeConnectionに資格情報を渡すようにしようとこだわっているPython 2.7、WMI予期しないCOMエラー-2147352567ドメインシステムに接続しているドメイン外システム
import wmi
import os
import threading
import getpass
class Application():
def __init__(self):
self.getCredentials(self)
self.getHostnames(self)
self.makeConnection(self)
self.runProgram(self)
def makeConnection(self, parent):
parent.server = parent.hostlist.pop()
print "makeConnection"
parent.wmiConnection = wmi.WMI(parent.server, user=parent.username, password=parent.password)
#For proof of concept, I'm settling for a single system for now. I can write the threading later.
def runProgram(self,parent):
print "runProgram"
parent.wmiConnection.Win32_Process.Create(CommandLine="cmd")
def getCredentials(self, parent):
parent.domain = raw_input('Domain: ')
parent.username = raw_input('Username: ')
parent.password = getpass.getpass(stream=None)
def getHostnames(self, parent):
if os.path.exists("hosts.txt"):
parent.hostslistfile = open("hosts.txt", "r")
parent.hostlist = parent.hostslistfile.readlines()
parent.hostslistfile.close()
if __name__ == "__main__":
app = Application()
():
は、私は主にPythonのWMIのv1.4.9を使用しようとすると、次のコードを持っています。ドメインに接続されているシステムからuser =とpassword =を指定せずにmakeConnection()を実行できるので、サーバーがWMIクエリを受け入れることがわかっています。結果の接続は成功します。私はdomain \ userとしてユーザーを渡そうとしますが、これをより適切に行うためのドキュメントを見つけることができません。
次のように私が受け取る実際のエラーは、次のとおりです。
Traceback (most recent call last):
File "C:\Users\Urist\Dropbox\code\foreman.py", line 35, in <module>
app = Application()
File "C:\Users\Urist\Dropbox\code\foreman.py", line 10, in __init__
self.makeConnection(self)
File "C:\Users\Urist\Dropbox\code\foreman.py", line 15, in makeConnection
parent.wmiConnection = wmi.WMI(parent.server, user=parent.username, password
=parent.password)
File "C:\Python27\lib\site-packages\wmi.py", line 1290, in connect
handle_com_error()
File "C:\Python27\lib\site-packages\wmi.py", line 241, in handle_com_error
raise klass (com_error=err)
wmi.x_wmi: <x_wmi: Unexpected COM Error (-2147352567, 'Exception occurred.', (0,
u'SWbemLocator', u'The RPC server is unavailable. ', None, 0, -2147023174), Non
e)>
は、WMIの料理にタイプミスがあるように見えます。残念ながら
def makeConnection(self, parent):
parent.passedUsername = "r\"" + parent.username + "\""
parent.server = parent.hostlist.pop()
#print parent.server, parent.passedUsername
parent.wmiConnection = wmi.WMI(parent.server, user=parent.passedUsername, password=parent.password)
:次へmakeConnection()モジュールを変更するので、私が持っている
connection = wmi.WMI("HOSTNAME", user=r"domain\user", password="password")
:しかし、最初のWMIチュートリアルの参照を使用して、私は正常に実行してリモート接続を作成することができますこれは機能しませんし、parent.passedUsernameが正確であるにもかかわらず元のエラーを返します。