运行VB脚本时出错800A0401?

时间:2014-06-11 06:42:13

标签: vbscript diskspace

我想从我的Windows服务器检查另一台Windows远程服务器的磁盘空间。

在谷歌搜索时,我找到了以下代码here(添加参考:System.Management dll):

using System.Management;
private void CalculateFreeUsed(string srvname)
{
    try
    {
        // Connection credentials to the remote computer, not needed if the logged account has access
        ConnectionOptions oConn = new ConnectionOptions();
        srvname=192.168.238.77 // take it default for testing
        oConn.Username = "MatinXie";
        oConn.Password = "acd";
        string strNameSpace = @"\\";

        if (srvname != "")
            strNameSpace += srvname;
        else
            strNameSpace += ".";

        strNameSpace += @"\root\cimv2";

        ManagementScope oMs = new ManagementScope(strNameSpace, oConn);

        //get Fixed disk state

        ObjectQuery oQuery = new ObjectQuery("select FreeSpace,Size,Name from Win32_LogicalDisk where DriveType=3");

        //Execute the query
        ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);

        //Get the results
        ManagementObjectCollection oReturnCollection = oSearcher.Get();

        //loop through found drives and write out info
        double D_Freespace = 0;
        double D_Totalspace = 0;
        foreach (ManagementObject oReturn in oReturnCollection)
        {
            // Disk name
            MessageBox.Show("Name : " + oReturn["Name"].ToString());
            // Free Space in bytes
            string strFreespace = oReturn["FreeSpace"].ToString();
            D_Freespace = D_Freespace + System.Convert.ToDouble(strFreespace);
            // Size in bytes
            string strTotalspace = oReturn["Size"].ToString();
            D_Totalspace = D_Totalspace + System.Convert.ToDouble(strTotalspace);
        }
    }


    catch
    {
        MessageBox.Show("Failed to obtain Server Information. The node you are trying to scan can be a Filer or a node which you don't have administrative priviledges. Please use the UNC convention to scan the shared folder in the server", "Server Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

现在我不知道如何运行它。如何在这里添加主要功能。但是,当我双击此脚本时,我收到错误800A0401。 有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

错误800a0401转换为“预期结束语句”。您收到此错误,因为您的代码是VB.net,但您尝试在VBScript解释器中运行它。

通过VBScript查询来自远程计算机的磁盘信息可以这样做:

computer = "192.168.238.77"

Set wmi = GetObject("winmgmts://" & computer & "/root/cimv2")

qry = "SELECT * FROM Win32_LogicalDisk WHERE DriveType=3"
For Each disk In wmi.ExecQuery(qry)
  WScript.Echo disk.Name & vbTab & disk.Size & vbTab & disk.FreeSpace
Next

为此,运行脚本的帐户必须在远程主机上具有本地管理员权限。否则,您需要为具有所需权限的帐户提供凭据,这会使代码稍微复杂一些(有关详细信息,请参阅here):

computer = "192.168.238.77"
username = "DOMAIN\user"
password = "pass"

Set locator = CreateObject("WbemScripting.SWbemLocator")
Set wmi = locator.ConnectServer(computer, "root\cimv2", username, password)
wmi.Security_.ImpersonationLevel = 3

qry = "SELECT * FROM Win32_LogicalDisk WHERE DriveType=3"
For Each disk In wmi.ExecQuery(qry)
  WScript.Echo disk.Name & vbTab & disk.Size & vbTab & disk.FreeSpace
Next