{"record":{"id":"2c28802f14772c4f","repo":"puppetlabs/puppet","slug":"failed-to-get-computer-name","errorCode":null,"errorMessage":"Failed to get computer name","messagePattern":"Failed to get computer name","errorType":"exception","errorClass":"Puppet::Util::Windows::Error","httpStatus":null,"severity":"error","filePath":"lib/puppet/util/windows/adsi.rb","lineNumber":57,"sourceCode":"      Puppet::Util::Windows::ADSI.connect(computer_uri).Create(resource_type, name)\n    end\n\n    def delete(name, resource_type)\n      Puppet::Util::Windows::ADSI.connect(computer_uri).Delete(resource_type, name)\n    end\n\n    # taken from winbase.h\n    MAX_COMPUTERNAME_LENGTH = 31\n\n    def computer_name\n      unless @computer_name\n        max_length = MAX_COMPUTERNAME_LENGTH + 1 # NULL terminated\n        FFI::MemoryPointer.new(max_length * 2) do |buffer| # wide string\n          FFI::MemoryPointer.new(:dword, 1) do |buffer_size|\n            buffer_size.write_dword(max_length) # length in TCHARs\n\n            if GetComputerNameW(buffer, buffer_size) == FFI::WIN32_FALSE\n              raise Puppet::Util::Windows::Error, _(\"Failed to get computer name\")\n            end\n\n            @computer_name = buffer.read_wide_string(buffer_size.read_dword)\n          end\n        end\n      end\n      @computer_name\n    end\n\n    def computer_uri(host = '.')\n      \"WinNT://#{host}\"\n    end\n\n    def wmi_resource_uri(host = '.')\n      \"winmgmts:{impersonationLevel=impersonate}!//#{host}/root/cimv2\"\n    end\n\n    # This method should *only* be used to generate WinNT://<SID> style monikers","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/util/windows/adsi.rb#L39-L75","documentation":"Raised on Windows when the Win32 GetComputerNameW call fails while Puppet::Util::Windows::ADSI.computer_name caches the NetBIOS machine name used to build WinNT:// ADsPaths for user and group management. It raises Puppet::Util::Windows::Error, so the message is suffixed with the GetLastError details.","triggerScenarios":"Any Windows code path touching ADSI (user/group resources, principal resolution) when GetComputerNameW returns FALSE — for example the RPC/workstation subsystem not being ready during very early boot or service startup, or the buffer/length contract failing.","commonSituations":"Rare in practice; seen on unhealthy or freshly-booting machines, or second-hand inside stack traces from ADSI operations.","solutions":["Retry once the machine is fully booted; sanity-check with `hostname` in cmd.","Check the System event log for RPC or Workstation-service failures around the time of the error.","If reproducible on a healthy machine, report to Puppet with the win32 error code included in the exception message."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"begin\n  name = Puppet::Util::Windows::ADSI.computer_name\nrescue Puppet::Util::Windows::Error\n  name = Socket.gethostname # best-effort fallback\nend","preventionTips":["Wrap ADSI bootstrapping code on Windows with a fallback path.","Retry once after boot in service-start code.","Log the win32 code from the message for triage."],"tags":["puppet","windows","win32","ffi","computer-name","adsi"],"backgroundTag":"win32-api-call-failed","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}