DMARC-Filter: OpenDMARC Filter v1.4.2 delorie.com 63SGUeRq280948 Authentication-Results: delorie.com; dmarc=pass (p=none dis=none) header.from=cygwin.com Authentication-Results: delorie.com; spf=pass smtp.mailfrom=cygwin.com DKIM-Filter: OpenDKIM Filter v2.11.0 delorie.com 63SGUeRq280948 Authentication-Results: delorie.com; dkim=pass (1024-bit key, unprotected) header.d=cygwin.com header.i=@cygwin.com header.a=rsa-sha256 header.s=default header.b=hFppy2SX X-Recipient: archive-cygwin AT delorie DOT com DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 38F534BA9009 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com; s=default; t=1777393837; bh=uVp4e6NdB1u1wYzX3Mthm7HpKWsSmJnLNMXacs92rS4=; h=References:In-Reply-To:Date:Subject:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=hFppy2SXfaSJ+GwTCr5g37WqORQT8z/PGMk5VZe6C+w+fN/i5O3Xt2X4Fcwa/MPEC 1E5EhT4y8kHWPhwwk3TcBwvq0PNTM03wLLWWGSsmNGxRSF2og4ZBBrirtKdk3Vp8ww 7z/cz2gcRbcp4T8NntYKBwrSNHKKcJrmqkQhtXyI= X-Original-To: cygwin AT cygwin DOT com Delivered-To: cygwin AT cygwin DOT com DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0061E4BA23EE ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0061E4BA23EE ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1777393788; cv=pass; b=S4edsfnvsd6ZtFj/CQQRbAi0iTa2TQI9l9k86W+0yXXNPpaGAVVHtPjtBQExJHVyjWrqKRdHrDPcSUalcjq4driwOLVhLm2id9GviOXSYBxKxXfebx895Zxb2H6835nCYt0vVFdPmgfP8IJIyJrVRt7EMWxy6zlREZT7gdmy1oM= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1777393788; c=relaxed/simple; bh=UoFuC/Y7u9U/pOpqnKczvWJYIi6qzpvka1bVPPE6dnk=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=TklO6wXeRJzwy/Bulg+dRun2umkAUIZY/ud+V+klw0wuQ3G/+K/otRPU5lsH8+AhE3no35JMsRBTsil3pD1XfPl2XO50mtassUMfI8Q3G/ZUKw3JnwxqunM5sx32drkyJYaBXkCalp6DLFF3hu1n4dzSONwG88G2BZFwxbKooJI= ARC-Authentication-Results: i=2; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0061E4BA23EE ARC-Seal: i=1; a=rsa-sha256; t=1777393787; cv=none; d=google.com; s=arc-20240605; b=eyxdK2zOChB0M3clXqE0oNImKOqSJIAmvED9eqrvt3j3/NtIHweNVOUacLdRhkhzLi wI9Vn/nV7zSeslLE11Q+hlOm47XsH/tPUI2ayOiTJ4fqwKoRyzLr9z4vfv9URLj4ru3t byUIuXV8ruxiRScmqZAniUnC0qrWYgcVagaFdfX8PJyFIBkQ+9ZIWtj3kd4oRCmLkgnc 3VSr2I4t48opQP9uU1mfasJXWq/3y35OsBJ5UdJ8f0xtEDNIV7gxQrbY8jiz7be5NHey Xx8xQQs8pTGOvmnOYqdWjPDYYcnHC7frXF3O0jTEkv6lzE/ScEjtQy4Mln2yYxRDqLsM 6Ukw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=to:subject:message-id:date:from:reply-to:in-reply-to:references :mime-version:dkim-signature; bh=zraGNl66/gRhUxdnR7nET3+nJmI+CN5jHgS+tzB8jzU=; fh=UKNFaOBO97U3RYl3PEse//nArTqr7SLJjCCEBz281Ew=; b=eNrDiC9QHEh2gX+01vR3R8QDoFozWtjz+jKxFzxX88NBuoIF5B0ETgKRJQMvtydg0f 1vt3qinJNzFcGlGlb6EOddyflXOCcuYzgdPJA0WVd4YP2P4nNzOgF8tlVk8XRfxzSKiE kUyTQbdJNcPqoYwmG1mtWMNY5W/p1qlHg8Ecp/kDiTeB63T5+2Wku8pmpq1tVX+Yn30a Hpo+0O//edq4TaCXVSVH/uUmyUhCJQ9iZOKIBVeZk6AnhqutmFr4z61QgBJc+YdK5Bxs EO0qCypDgjUd6QsIC6XirDKi6vjEiH6tD6Q8Pp2TS+yGv8kG3R2WYLeSjs71C5Z9Gke2 zT6g==; darn=cygwin.com ARC-Authentication-Results: i=1; mx.google.com; arc=none X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777393787; x=1777998587; h=to:subject:message-id:date:from:reply-to:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=zraGNl66/gRhUxdnR7nET3+nJmI+CN5jHgS+tzB8jzU=; b=rCOkzv2ybhno2G4dqykB7T8Z6LpPWg4nbYPR4APB70WV4JUbBtYfx78TWgbZv/Vl4W HPDbNKZd+HvRuVnAAUELTrt/yJSrJmYLGpoLqq40J137iyQAn1SztJJST5h0G+hz9DZQ zCo5uy7R7tULnYT4UkK+md9h0L1grn6KOHAxVgLoms5Wur3INgNPeeHoCbbTYGsSMDi8 ynYB1ksmhOzSJBWUayzQiSNroJdHet/zAAcRKMUM7JL4UwJct5fP8H486YABIHXWBbQW fKP8AN5xEtuGDfbvomCtfFGRuGjZT6V9QxjSJgBc5CaTxWuyCe7eHopR0n+KuYaJAQ2I RE8g== X-Gm-Message-State: AOJu0Ywq+kerWEAg2n4NQK+4p7EaJn5WoK8jnghhXlPmkzRYq0QvYq6r /HuuSh9Uhp4m4gEnyyjZKPM3ltVMF/KcFOHqMACTOySMZGOOi89EibiecNLUXXFS2y1/q0ckEjr dhvDjGohVXxd32ck7NijhTlEEQh60FtTuwdV4XDk= X-Gm-Gg: AeBDievHyZPQFfG8Zym3Jt4pxNe0zD9u1XY0FoeUAWkRmo8cY7BXUZfbQfAdnwVSNfY lixdEU3tgjPKVWIJJqaf9e6QgxyYkH5ZLpX+NKN/zJ6U0fkBC8Y65hvzoGlNboSCFwlyHVfyNpd /NAHFDqskDcfI0TDI9YoIquivvBb1Uhjax/igoQ/BSxYTC4OAFF3aEW37UOdk3Rk6GOfjoL1PGf 2fDiBy+DCIoFjUeAVFiLIJRWjfWGc0Ftq1ahF+PbXN5B1DhYSNW31kia5iCzD2OgbB5/QXTVIae xN/xVIHt2zeZEPkRdVszWCp0ibcF8XZ0+gPprOtGQS3aj4rXIc54EGHjR7lvWvN2Ya1STjS6eez BpqMQZQ== X-Received: by 2002:a05:690e:4381:b0:650:fbe:3b45 with SMTP id 956f58d0204a3-65beedaa74emr2796510d50.23.1777393787266; Tue, 28 Apr 2026 09:29:47 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 28 Apr 2026 10:29:20 -0600 X-Gm-Features: AVHnY4LBI_6emVuQxEhLNMmZxtq_QSXaELIhmgeUhEDEkvhUw7Xz7VUhXq0s_Rw Message-ID: Subject: Re: Getting Windows "MACHINE SID" without fork() & exec()? To: cygwin AT cygwin DOT com X-Content-Filtered-By: Mailman/MimeDel 2.1.30 X-BeenThere: cygwin AT cygwin DOT com X-Mailman-Version: 2.1.30 List-Id: General Cygwin discussions and problem reports List-Archive: List-Post: List-Help: List-Subscribe: , From: William Stewart via Cygwin Reply-To: bstewart AT iname DOT com Cc: William Stewart Content-Type: text/plain; charset="utf-8" Sender: "Cygwin" Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from base64 to 8bit by delorie.com id 63SGUeRq280948 On Tue, Apr 28, 2026 at 9:03 AM Takeshi Nishimura wrote: > Why do you need the machine SID? > > Multiple reasons: > Automated machine identification for cluster, looking up the localised > Windows account names for builtin users, license information > gathering, ... Here's a PowerShell way that calls P/Invoke. If you're spawning a powershell.exe (or pwsh.exe) process every time to get this, then naturally it will be slow. Probably line breaks in the below will be wrong and will need to be fixed manually. -----[snip]----- # Get-ComputerSID.ps1 #requires -version 5.1 [CmdletBinding()] param( [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName)] [String[]] $ComputerName = [Environment]::MachineName ) begin { if ( [Environment]::OSVersion.Platform -ne [PlatformID]::Win32NT ) { throw "Windows platform required" } Add-Type -MemberDefinition @" // [B374924426674F5EAACF740F97B4D2C5.NetApi32+USER_MODALS_INFO_2] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct USER_MODALS_INFO_2 { public string usrmod2_domain_name; public IntPtr usrmod2_domain_id; } // [B374924426674F5EAACF740F97B4D2C5.NetApi32]::NetApiBufferFree() [DllImport("netapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern uint NetApiBufferFree(IntPtr Buffer); // [B374924426674F5EAACF740F97B4D2C5.NetApi32]::NetUserModalsGet() [DllImport("netapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern uint NetUserModalsGet( string servername, uint level, out IntPtr bufptr ); "@ -Namespace B374924426674F5EAACF740F97B4D2C5 -Name NetApi32 function GetComputerSID { [CmdletBinding()] param( [Parameter(Position = 0,Mandatory)] [String] $computerName ) $bufPtr = [IntPtr]::Zero try { $result = [B374924426674F5EAACF740F97B4D2C5.NetApi32]::NetUserModalsGet( $computerName, # servername 2, # level [Ref] $bufPtr # bufptr ) if ( $result -eq 0 ) { $umi = [Runtime.InteropServices.Marshal]::PtrToStructure($bufPtr, [Type] [B374924426674F5EAACF740F97B4D2C5.NetApi32+USER_MODALS_INFO_2]) New-Object Security.Principal.SecurityIdentifier($umi.usrmod2_domain_id) } else { $msg = ([ComponentModel.Win32Exception] ($result -as [Int])).Message Write-Error ("Error {0} connecting to '{1}' - {2}" -f $result,$computerName,$msg) } } finally { if ( $bufPtr -ne [IntPtr]::Zero ) { [Void] [B374924426674F5EAACF740F97B4D2C5.NetApi32]::NetApiBufferFree($bufPtr) } } } } process { foreach ( $ComputerNameItem in $ComputerName ) { $SID = GetComputerSID $ComputerNameItem if ( $null -ne $SID ) { [PSCustomObject] @{ "ComputerName" = $ComputerNameItem "SID" = $SID } } } } -----[snip]---- One advantage here is the support for the PowerShell pipeline, so you can pipe computer names to the script and it will output objects containing the computer names and SIDs. -- Problem reports: https://cygwin.com/problems.html FAQ: https://cygwin.com/faq/ Documentation: https://cygwin.com/docs.html Unsubscribe info: https://cygwin.com/ml/#unsubscribe-simple