重新命名Win32_ComputerSystem類別的方法
Rename方法會重新命名電腦。
本主題使用 Managed 物件格式 (MOF) 語法。 如需使用此方法的詳細資訊,請參閱 呼叫 方法。
語法
uint32 Rename(
[in] string Name,
[in] string Password,
[in] string UserName
);
參數
-
名稱 [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。 請勿使用 Moniker 連線。
若要連線到本機電腦,您無法指定密碼或驗證授權單位,例如 Kerberos。 您只能指定與遠端電腦連線的密碼和授權單位。
如果指定 Password 和 UserName 時驗證層級太低,則 WMI 會針對 C/C++ 傳回 WBEM_E_ENCRYPTED_CONNECTION_REQUIRED 錯誤,以及針對腳本和 VB 傳回 wbemErrEncryptedConnectionRequired 。
如需詳細資訊,請參閱 SWbemLocator_ConnectServer、IWbemLocator::ConnectServer和 建構 Moniker 字串。
範例
下列腳本示範如何重新命名本機電腦。
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 |
|