[WMI] ローカルの WMI に接続する際はユーザアカウントとパスワードを指定してはいけない (80041064)

皆様お世話になっております。ういこです。
今日の御題はずばり、人生相談です。

東京都 A さんからの人生相談
スクリプトで、WMI の WbemLocator の ConnectServer でローカル コンピュータの管理者アカウントで接続してプロセスの実行を行おうとしたんです。ところが、自分のコンピュータなのに、ローカル Administrator 使って繋げに行ってるのに、彼女にこんなこといわれて拒否られてしまうんです。どうしたら仲良くつなげてもらえますか。

「コード : 80041064 ユーザー資格情報はローカル接続には使用できません (User credentials cannot be used for local connections)」

ちなみに、このスクリプトを実行する際は Runas.exe をつかってローカルの管理者アカウントで実行して、パスワードは Sendkey で知らせているようにしていました。そこで、何かタイミングでも悪いのかと思い、今度は SWbemLocator.ConnectServer() を使う際に、第三引数と第四引数のユーザ アカウントとパスワードを指定してみたんですが、状況は変わりません…。
僕の何が悪いのでしょうか。彼女に仲良くつながりたいのです。

Answer (回答者 : ういこう(仮名、主婦))
とても大変なご様子ですね。お気持ち、お察しいたします。Runas でユーザー偽装をしてても、ローカル コンピュータ内では神に等しいローカル管理者アカウントで実行してるのに、ローカルの WMI さんに接続を拒否られたらショックですよね。
「80041064」というエラーは、ローカル マシンの WMI に接続する際にアカウント情報を指定すると発生するのです。
残念ですが、ローカルの WMI さんに接続する際は、アカウント情報は指定するべきではありません。素の自分 (プロセスの実行ユーザー) で勝負してつなぎに言ってください。ユーザー アカウントとパスワードを指定するのもだめです。もし、ユーザー アカウントとパスワードを指定してる場合は、これらを NULL にしてください。
(NULL にすると、スクリプトはプロセスを起動しているユーザの権限によって動作することになります。)
そのスクリプトを起動したユーザのみが、ローカル マシンの WMI に接続可能なユーザなのです。
ユーザ名とパスワードの指定するのは、リモート WMI サービスへの接続のみに使用されるようにしましょう。

こんな風になっていませんか?
SWbemLocator.ConnectServer(strComputer, "", strUser, strPassword, , "")

これを、こんな風に Null にしてあげてくださいね。
SWbemLocator.ConnectServer(strComputer, "", Null, Null, , "")

きっと彼女も仲良く接続させてくれるはずです。
がんばって下さい!

<< 参考情報 >>
ローカルの WMI につなげるときはアカウント情報は指定しないということについて言及したドキュメント

SWbemLocator.ConnectServer Method
https://msdn.microsoft.com/en-us/library/aa393720.aspx

strUser [in, optional]
User name to use to connect. The string can be in the form of either a user name or a Domain\Username.
Leave this parameter blank to use the current security context.
The strUser parameter should only be used with connections to remote WMI servers.
If you attempt to specify strUser for a local WMI connection, the connection attempt fails. If Kerberos authentication is in use, then the username and password that is specified in strUser and strPassword cannot be intercepted on a network. You can use the UPN format to specify the strUser.

Example: "DomainName\UserName"

Windows 2000, Windows NT 4.0, and Windows Me/98/95: strUser cannot be specified in the UPN format.

Note If a domain is specified in strAuthority, then the domain must not be specified here. Specifying the domain in both parameters results in an Invalid Parameter error.

strPassword [in, optional]
String that specifies the password to use when attempting to connect.
Leave the parameter blank to use the current security context.
The strPassword parameter should only be used with connections to remote WMI servers.
If you attempt to specify strPassword for a local WMI connection, the connection attempt fails.
If Kerberos authentication is in use then the username and password that is specified in strUser and strPassword cannot be intercepted on the network.

上記を見ると、"The strUser parameter should only be used with connections to remote WMI servers. " リモートの WMI に繋げたい時にだけ指定してね、とあります。

IWbemLocator::ConnectServer
https://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/iwbemlocator_connectserver.asp

WBEM_E_LOCAL_CREDENTIALS
WMI is passing the user credential on local connection.

WMI Error Constants
https://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wmi_error_constants.asp

WBEM_E_LOCAL_CREDENTIALS
2147749988 (0x80041064) User specified a username/password/authority on a local connection.
The user must use a blank username/password and rely on default security.

80041064 の際の注意としてここにも同じことが書いてありますね。

※ RunAs、アカウント情報(ユーザアカウント名とパスワード)を指定しないとプロセスを起動したユーザアカウントで実行されるということについては下記の記事に記載しております。よかったらご覧ください♪

https://blogs.technet.com/jpilmblg/archive/2009/05/15/adsi-troubleshoot-1.aspx

それでは、営業時間になりましたのでまた~

ういこう@明日は授業参観