GUID(全局唯一標識符)是 128 位標識符,理論上全球唯一。Windows 腳本宿主 (WSH) 可通過 Scriptlet.TypeLib COM 對象輕鬆生成 GUID,並結合 Internet Explorer 自動化將其複製到剪貼板。
' gen_guid.vbs
Set TypeLib = CreateObject("Scriptlet.TypeLib")
WScript.Echo TypeLib.Guid
運行:cscript //nologo gen_guid.vbs
' gen_guid_clip.vbs
Set TypeLib = CreateObject("Scriptlet.TypeLib")
strGUID = TypeLib.Guid
' 利用 IE 的 clipboardData 對象
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate("about:blank")
objIE.Visible = False
' 等待導航完成
While objIE.Busy : WScript.Sleep 50 : Wend
objIE.Document.ParentWindow.ClipboardData.SetData "text", strGUID
objIE.Quit
WScript.Echo "GUID 已複製到剪貼板:" & strGUID
PowerShell 內建 New-Guid,無需 COM 組件:
# 顯示
[Guid]::NewGuid()
# 複製到剪貼板
[Guid]::NewGuid() | Set-Clipboard
# 批量生成
1..10 | ForEach-Object { [Guid]::NewGuid() }
<?php
// PHP 7+ 使用 random_bytes + bin2hex
function generate_guid() {
$data = random_bytes(16);
// 設置版本位 (4) 和變體位 (10xx)
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // Version 4
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // Variant 1
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
echo generate_guid(); // 例: 550e8400-e29b-41d4-a716-446655440000
// 或使用 Ramsey UUID 庫 (composer require ramsey/uuid)
use Ramsey\Uuid\Uuid;
echo Uuid::uuid4()->toString();
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
│ │ │ │
│ │ │ └─ Variant (N: 8/9/a/b)
│ │ └──── Version (M: 1-5, 常見 4=隨機)
│ └─────── Time/Version 相關
└─────────────── Time Low
random_bytes 生成)Scriptlet.TypeLib 需 Windows 系統註冊相關 DLL,現代 Windows 默認可用random_bytes/openssl_random_pseudo_bytes)