2017-01-25 1 views
0

다음 코드 :JScript.NET : 열거 WMI 컬렉션 JScript.NET에서

%windir%\Microsoft.NET\Framework64\v4.0.30319\jsc.exe /platform:x64 wmi.js 

및 실행하지만으로 WMI 호출을 변경 :

col=wmi.ExecQuery("SELECT * From Win32_Process", "WQL", 32); 

wmi.js 
------ 
var wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2"), 
    col =null, prc=null; 
col=wmi.ExecQuery("SELECT * From Win32_Process", "WQL", 32); 
//col=wmi.InstancesOf("Win32_Process"); 
var e = new Enumerator(col); 
for (; !e.atEnd(); e.moveNext()){ 
    prc = e.item(); 
    print(prc.CommandLine); 
} 

과 함께 컴파일

컴파일이 작동하는 동안 실행은 다음을 제공합니다.

If successful, the method returns an SWbemObjectSet

는 또한, WSH의 JScript는 InstancesOf 수집 및 ExecQuery 모두 열거 할 수 있습니다 : 왜, 모두 InstancesOfExecQuery 문서를 말한다위한부터 91,363,210
Unhandled Exception: System.InvalidCastException: 
Unable to cast COM object of type 'System.__ComObject' to interface type 'System.Collections.IEnumerable'. 
This operation failed because the QueryInterface call on the COM component for the interface with IID '{496B0ABE-CDEE-11D3-88E8-00902754C43A}' failed due to the following error: 
'No such interface supported (Exception from HRESULT: 0x80004002       

이해가 안 돼요.

답변

1

먼저 wbemFlagForwardOnly 플래그를 제거하면 ExecQuery가 예상대로 작동하는 개체를 반환합니다.

var wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2") 
    , col =null, prc=null; 

col=wmi.ExecQuery("SELECT * From Win32_Process"); 
//col=wmi.InstancesOf("Win32_Process"); 

var e = new Enumerator(col); 
for (; !e.atEnd(); e.moveNext()){ 
    prc = e.item(); 
    print(prc.CommandLine); 
} 

설명을 위해 어둠 속에서 촬영 한 것입니다. (나는 매일 Jscript.NET에서 작동하지 않으며 전문가도 아닙니다.) https://msdn.microsoft.com/en-us/library/ms974547.aspx에서

는 : 오류에서

"WMI는 SWbemObjectSet에서 개체에 대한 참조를 유지하지 않습니다 때문에 앞으로 만 열거는 기본 열거보다 훨씬 빠르게 수행" :

" 'System .__ ComObject'형식의 COM 개체를 인터페이스 형식 'System.Collections.IEnumerabl'으로 캐스팅 할 수 없습니다. 이자형."

컬렉션을 열거 자로 변환하면 캐스팅되는 개체에 대한 참조가 필요합니다. wbemFlagForwardOnly 플래그를 사용하면 참조 전달이 없어 캐스팅이 실패합니다.

이렇게 읽었습니다. 가치있는 것을 위해 가져 가라.

조사 할 때 흥미로운 점은 jsc/csc에서 exe를 실행하는 경우 wscript/cscript를 사용하여이 열거 자에 오류가없는 것입니다.

또한 VBScript에는 이러한 플래그로 열거하는 데 문제가없는 것 같습니다. 예를 확인하고 https://msdn.microsoft.com/en-us/library/ms525775(v=vs.90).aspx을 비교하십시오.

관련 문제