다음을 통해 공유


Win32_ComputerSystem 클래스의 이름 바꾸기 메서드

Rename 메서드는 컴퓨터의 이름을 바꿉니다.

이 항목에서는 MOF(Managed Object Format) 구문을 사용합니다. 이 메서드를 사용하는 방법에 대한 자세한 내용은 메서드 호출을 참조하세요.

구문

uint32 Rename(
  [in] string Name,
  [in] string Password,
  [in] string UserName
);

매개 변수

Name [in]

새 컴퓨터 이름입니다. 이 매개 변수의 값에는 컨트롤 문자, 선행 또는 후행 공백 또는 / \\ [ ]의 문자가 포함될 수 없습니다.

암호 [in]

UserName 매개 변수가 계정 이름을 지정하는 경우 도메인 컨트롤러에 연결할 때 사용할 암호입니다. 그렇지 않으면 이 매개 변수는 NULL이어야 합니다. 암호UserName 매개 변수에 대한 자세한 내용은 이 항목의 설명 섹션을 참조하세요.

UserName [in]

도메인 컨트롤러에 연결할 때 사용할 계정 이름을 지정하는 문자열입니다. 문자열은 null로 종료되어야 하며 도메인 NetBIOS 이름 및 사용자 계정(예: UPN(사용자 계정 이름)인 "DOMAINNAME\username" 또는 "someone@domainname.com")을 지정해야 합니다. UserName 매개 변수가 NULL인 경우 WMI는 호출자의 컨텍스트를 사용합니다. 암호UserName 매개 변수에 대한 자세한 내용은 이 항목의 설명 섹션을 참조하세요.

반환 값

성공하면 0을 반환합니다. 0이 아닌 반환 값은 오류를 나타냅니다. 성공하면 다시 부팅이 필요합니다. 추가 오류 코드는 WMI 오류 상수 또는 WbemErrorEnum을 참조하세요. 일반적인 HRESULT 값은시스템 오류 코드를 참조하세요.

성공 (0)

기타 (1 4294967295)

설명

로컬 관리자 그룹의 구성원인 경우 Rename 메서드를 사용하여 컴퓨터 이름을 바꿀 수 있습니다. 그러나 도메인 컴퓨터에 대해 메서드를 원격으로 사용할 수 없습니다.

PasswordUserName 매개 변수를 지정하는 경우 WMI에 대한 연결은 RPC_C_AUTHN_LEVEL_PKT_PRIVACY(스크립트 및 VB(Visual Basic)에 대한 wbemAuthenticationLevelPktPrivacy) 인증 수준을 사용해야 합니다.

원격 컴퓨터에 연결하고 자격 증명을 지정하려면 C++용 IWbemLocator인 로케이터 개체 연결과 스크립트 및 VB에 SWbemLocator를 사용합니다. 모니커 연결을 사용하지 마세요.

로컬 컴퓨터에 연결하려면 암호 또는 인증 기관(예: Kerberos)을 지정할 수 없습니다. 원격 컴퓨터에 대한 연결에서만 암호 및 권한을 지정할 수 있습니다.

PasswordUserName을 지정할 때 인증 수준이 너무 낮으면 WMI는 C/C++에 대한 WBEM_E_ENCRYPTED_CONNECTION_REQUIRED 오류를 반환하고 스크립트 및 VB에 대해 wbemErrEncryptedConnectionRequired를 반환합니다.

자세한 내용은 SWbemLocator_ConnectServer,IWbemLocator::ConnectServerMoniker 문자열 생성을 참조하세요.

예제

다음 스크립트는 로컬 컴퓨터의 이름을 바꾸는 방법을 보여줍니다.

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
CIMWin32.mof
DLL
CIMWin32.dll

추가 정보

Win32_ComputerSystem

WMI 작업: 계정 및 도메인