Upravit

Sdílet prostřednictvím


Creating Active Server Pages for WMI

Microsoft Active Server Pages (ASP) can create dynamic webpages by including both server-side and client-side scripts. ASP pages can be much faster than client HTML pages because most of the work is done on the server. You can also use ASP pages to display information about remote computers to other computers that do not have Windows Management Instrumentation (WMI) installed.

The following procedure describes how to use WMI with ASP.

To use WMI with ASP

  1. Write an ASP page (.asp) that uses WMI, and place it in a directory accessible to your web server.

    ASP scripts for WMI can be developed with several scripting languages, including VBScript. You can construct the WMI script part of an ASP page exactly as you construct any other script that uses WMI, with one important restriction: you cannot use asynchronous WMI methods within ASP pages. Note also that any calls to GetObject or CreateObject must be in the server-side code. For more information, see Scripting API for WMI.

  2. Set up authentication configuration for Internet Information Services (IIS). For more information, see Configuring IIS 5 and Later for WMI ASP Scripting.

  3. Disable anonymous access, and enable Windows Integrated Authentication for the ASP file. You can configure these settings for your ASP page by using the IIS snap-in located in the Administrative Tools folder of the Control Panel.

WMI ASP Page Example

The following example uses Windows Management Instrumentation (WMI) within an Active Server Page (ASP) to display the IP address and default IP gateway settings for the server from which this script is executed.

<%@ LANGUAGE="VBSCRIPT"%>
<HTML>
<HEAD>
<TITLE>WMI ASP Example:
    Read Default Gateway and IP Address information </TITLE>
</HEAD>

<BODY>

<%
On Error Resume Next
set IPConfigSet = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!root\cimv2").ExecQuery" _
    & "("SELECT IPAddress, DefaultIPGateway "" _ 
    & " FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=TRUE")
%>

<%If Err <> 0 Then %>
    <%if err.number = -2147217405 then%>
        <p>Error 0x80041003: Access Denied: 
           Check permissions and file security for this ASP file.</p>
    <%else%>
        <p>Error description: <%=Err.description%> 
           error number <%=Err.number%></p>
    <%end if%>

<%end if %>

<%for each IPConfig in IPConfigSet%>

    <%if Not IsNull(IPConfig.IPAddress) then %>
        <%for i=LBound(IPConfig.IPAddress) 
            to UBound(IPConfig.IPAddress)%>
            <p>IP Address: <%=IPConfig.IPAddress(i)%></p>
        <%next%>
    <%end if%>
    

    <%if Not IsNull(IPConfig.DefaultIPGateway) then %>
        <%for i=LBound(IPConfig.DefaultIPGateway) 
            to UBound(IPConfig.DefaultIPGateway)%>
            <p>Default IP Gateway: 
                <%=IPConfig.DefaultIPGateway(i)%></p>
        <%next%>
    <%end if%>
<%next%>

<%If Err <> 0 Then %>
    <p>error description: <%=Err.description%> 
       error number <%=Err.number%></p>
<%end if %>

</BODY>
</HTML>