我正在使用ManagementClass在远程计算机上执行exe。
var serverPath = new ManagementPath
{
Server = "SERVERNAME",
NamespacePath = "\\ROOT\\CIMV2",
ClassName = "Win32_Process"
};
var connection = new ConnectionOptions
{
Username = _Domain + @"\" + _UserName,
Password = _Pass,
Impersonation = ImpersonationLevel.Impersonate,
Authentication = AuthenticationLevel.Packet,
EnablePrivileges = true,
Timeout = new TimeSpan(0, 0, 15)
};
var ms = new ManagementScope(serverPath, connection);
var mprocess = new ManagementClass(ms, new ManagementPath("Win32_Process"), new ObjectGetOptions());
var inParams = mprocess.GetMethodParameters("Create");
inParams["CommandLine"] = @"F:\Utils\Directory\program.exe";
inParams["CurrentDirectory"] = @"F:\Utils\Directory\";
mprocess.InvokeMethod("Create", inParams, null);
我的应用程序出现以下错误:
System.Data.SqlClient.SqlException (0x80131904): Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at KPlusSynchronizer.Tyche.StoreIntoDatabase(String tycheConnection, String sp, XDocument xml, Boolean logXml, String path, String user)
at KPlusSynchronizer.Synchronizer.Synchronize(String tycheTable, String tycheSp, String kplusSp, String path, String user)
当我使用与ConnectionOptions中相同的用户名和密码在服务器上手动登录并手动启动exe时,它可以正常工作。
在尝试连接之前,我还尝试过使用ImpersonateUser(使我成为管理员,与用户名和密码相同)。
有人能看到我错过的东西吗?
答案 0 :(得分:0)
我查看了我们的代码库。我们用WMI完成同样的事情
.....
var strWmiPath = string.Format(@"\\{0}\{1}", m_remoteHost, wmiNamespacePath != null ? string.Join("\\", wmiNamespacePath) : string.Empty);
m_scope = new ManagementScope(strWmiPath, options);
m_scope.Connect(); // that is missing in your version
....
var commandLine = string.Format(CultureInfo.InvariantCulture, "{0} {1}", fileName, arguments);
var remoteCmd = new ManagementClass(m_scope, new ManagementPath("Win32_Process"), null);
var inParams = remoteCmd.GetMethodParameters("Create");
inParams["CommandLine"] = commandLine;
inParams["CurrentDirectory"] = null;
inParams["ProcessStartupInformation"] = null;
remoteCmd.InvokeMethod("Create", inParams, null);