重命名Win32_ComputerSystem类的方法
Rename 方法重命名计算机。
本主题使用托管对象格式 (MOF) 语法。 有关使用此方法的详细信息,请参阅 调用方法。
语法
uint32 Rename(
[in] string Name,
[in] string Password,
[in] string UserName
);
参数
-
Name [in]
-
新计算机名称。 此参数的值不能包含控制字符、前导空格或尾随空格或下列任意字符:/ \\ [ ]。
-
密码 [in]
-
如果 UserName 参数指定了帐户名,则连接到域控制器时要使用的密码。 否则,此参数必须为 NULL。 有关 Password 和 UserName 参数的详细信息,请参阅本主题的“备注”部分。
-
UserName [in]
-
指定连接到域控制器时要使用的帐户名称的字符串。 字符串必须以 null 结尾,并且必须指定域 NetBIOS 名称和用户帐户,例如“DOMAINNAME\username”或“someone@domainname.com”,这是 UPN) (用户主体名称。 如果 UserName 参数为 NULL,则 WMI 使用调用方上下文。 有关 Password 和 UserName 参数的详细信息,请参阅本主题的“备注”部分。
返回值
如果成功,则返回 0 (零) 。 非零返回值指示错误。 如果成功,则需要重新启动。 有关其他错误代码,请参阅 WMI 错误常量 或 WbemErrorEnum。 有关常规 HRESULT 值,请参阅 系统错误代码。
-
成功 (0)
-
其他 (1 4294967295)
备注
如果你是本地管理员组的成员,则可以使用 Rename 方法重命名计算机。 但是,不能对域计算机远程使用 方法。
如果指定 了 Password 和 UserName 参数,则与 WMI 的连接必须使用 RPC_C_AUTHN_LEVEL_PKT_PRIVACY (wbemAuthenticationLevelPktPrivacy 进行脚本和 Visual Basic (VB) ) 身份验证级别。
若要连接到远程计算机并指定凭据,请使用定位符对象连接(适用于 C++ 的 IWbemLocator)和用于脚本和 VB 的 SWbemLocator。 请勿使用名字对象连接。
若要连接到本地计算机,不能指定密码或身份验证机构,例如 Kerberos。 只能在与远程计算机的连接中指定密码和颁发机构。
如果指定 Password 和 UserName 时身份验证级别过低,则 WMI 将返回 C/C++ 的WBEM_E_ENCRYPTED_CONNECTION_REQUIRED 错误,并为脚本和 VB 返回 wbemErrEncryptedConnectionRequired 。
有关详细信息,请参阅 SWbemLocator_ConnectServer、IWbemLocator::ConnectServer 和 构造名字对象字符串。
示例
以下脚本演示如何重命名本地计算机。
Name = "name"
Password = "password"
Username = "username"
Set objWMIService = GetObject("Winmgmts:root\cimv2")
' Call always gets only one Win32_ComputerSystem object.
For Each objComputer in _
objWMIService.InstancesOf("Win32_ComputerSystem")
Return = objComputer.rename(Name,Password,Username)
If Return <> 0 Then
WScript.Echo "Rename failed. Error = " & Err.Number
Else
WScript.Echo "Rename succeeded." & _
" Reboot for new name to go into effect"
End If
Next
以下 C++ 代码示例重命名计算机系统。
int set_computer_name(const string &newname/*, const string &username, const string &password*/)
{
HRESULT hres;
// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------
// Note: If you are using Windows 2000, you need to specify -
// the default authentication credentials for a user by using
// a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
// parameter of CoInitializeSecurity ------------------------
hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
if (FAILED(hres))
{
cout << "Failed to initialize security. Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
{
cout << "Failed to create IWbemLocator object."
<< " Err code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
// Step 4: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
IWbemServices *pSvc = NULL;
// Connect to the root\cimv2 namespace with
// the current user and obtain pointer pSvc
// to make IWbemServices calls.
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
0, // Locale. NULL indicates current
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices proxy
);
if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x"
<< hex << hres << endl;
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
/*cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;*/
// Step 5: --------------------------------------------------
// Set security levels on the proxy -------------------------
hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres))
{
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----
// For example, get the name of the operating system
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_ComputerSystem"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres))
{
cout << "Query for operating system name failed."
<< " Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
// Step 7: -------------------------------------------------
// Get the data from the query in step 6 -------------------
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
if(0 == uReturn)
{
break;
}
// set up to call the Win32_ComputerSystem::Rename method
BSTR MethodName = SysAllocString(L"Rename");
BSTR ClassName = SysAllocString(L"Win32_ComputerSystem");
IWbemClassObject* pClass = NULL;
hres = pSvc->GetObject(ClassName, 0, NULL, &pClass, NULL);
IWbemClassObject* pInParamsDefinition = NULL;
hres = pClass->GetMethod(MethodName, 0,
&pInParamsDefinition, NULL);
IWbemClassObject* pClassInstance = NULL;
hres = pInParamsDefinition->SpawnInstance(0, &pClassInstance);
// Create the values for the in parameters
wstring val;
BSTR NewName;
val.assign(newname.begin(), newname.end());
NewName = SysAllocString(val.c_str());
VARIANT varName;
varName.vt = VT_BSTR;
varName.bstrVal = NewName;
// Store the value for the in parameters
hres = pClassInstance->Put(L"Name", 0,
&varName, 0);
wprintf(L"Set computer name to %s\n", V_BSTR(&varName));
VARIANT var;
CIMTYPE pType;
LONG pFlavor;
pclsObj->Get(L"__PATH",0,&var,&pType,&pFlavor);
// Execute Method
IWbemClassObject* pOutParams = NULL;
hres = pSvc->ExecMethod(var.bstrVal, MethodName, 0,
NULL, pClassInstance, &pOutParams, NULL);
if (FAILED(hres))
{
cout << "Could not execute method. Error code = 0x"
<< hex << hres << endl;
VariantClear(&varName);
SysFreeString(ClassName);
SysFreeString(MethodName);
SysFreeString(NewName);
pClass->Release();
pInParamsDefinition->Release();
pOutParams->Release();
return 1; // Program has failed.
}
// To see what the method returned,
// use the following code. The return value will
// be in &varReturnValue
VARIANT varReturnValue;
hres = pOutParams->Get(_bstr_t(L"ReturnValue"), 0,
&varReturnValue, NULL, 0);
// Clean up
//--------------------------
VariantClear(&varName);
VariantClear(&varReturnValue);
SysFreeString(ClassName);
SysFreeString(MethodName);
SysFreeString(NewName);
pClass->Release();
pInParamsDefinition->Release();
pOutParams->Release();
return 0;
}
// Cleanup
// ========
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
pclsObj->Release();
CoUninitialize();
return 0; // Program successfully completed.
要求
要求 | 值 |
---|---|
最低受支持的客户端 |
Windows Vista |
最低受支持的服务器 |
Windows Server 2008 |
命名空间 |
Root\CIMV2 |
MOF |
|
DLL |
|