// // Example location script for 2BrightSparks SyncBackPro: Machine Inventory. // // SBLang=Pascal // // http://www.2BrightSparks.com/ // November 2026 // // Machine Inventory is a read-only location whose files describe this PC: // // Computer.txt Windows version, processor, memory, BIOS // InstalledPrograms.csv Everything in Settings > Apps // Updates.csv Installed Windows updates // Services.csv Services and how they start // Drivers.csv Drivers and how they start // ScheduledTasks.csv Scheduled tasks and what they run // Startup.csv Programs that run when you sign in // Environment.txt System and user environment variables // Drives.csv Drives, their labels and sizes // NetworkAdapters.csv Network adapters and their MAC addresses // hosts.txt A copy of the Windows hosts file // // The files are made fresh each time a profile looks at the location, but a // file keeps its date & time until its contents actually change. So a profile // copies only what has changed, and with versioning turned on you build up a // history of how this PC's setup has changed over time. // // Using it // -------- // Make a new Mirror profile (or Backup, to keep files the inventory no longer // makes) that copies FROM Machine Inventory, with the path \, TO a folder such // as D:\Inventory\PC1\. A location script is always the destination of a // profile, so SyncBack sets it up as "Mirror, destination to source" for you. // Turn on versioning to keep the older copies of each file. // // Settings // -------- // The inventory is made in %LOCALAPPDATA%\SyncBack Inventory\. To make it // somewhere else, set the folder on the Machine Inventory page of the profile // setup. // // It needs Windows PowerShell (included in Windows), and no Insider Access. // var gFSO, gRoot, gMade; const READ_ONLY = 'Machine Inventory is read-only'; // // This is a location script // function Description(var ScriptType): String; begin Result:='Machine Inventory'; ScriptType:=SCRIPTTYPE_LOCATION + SCRIPTTYPE_CONFIG; end; function Install(Interactive); begin Result:=''; end; procedure Uninstall; begin end; function FilesToExport(Interactive, Cnt); begin Result:=''; end; //----------------------------------------------------------------------------- // Making the inventory //----------------------------------------------------------------------------- // // The PowerShell script that writes the files. It only uses double quotes so // it can sit in Pascal strings, and it sorts everything and leaves out things // that change by themselves (whether a service is running right now, free // space, DHCP leases) so that a file only changes when the setup does. // function InventoryScript; var L; begin L:=TStringList.Create; try L.Add('param([string]$Out)'); L.Add('$ErrorActionPreference = "SilentlyContinue"'); L.Add('function Save($Name, $Data) { $Data | Out-File -LiteralPath (Join-Path $Out $Name) -Encoding utf8 -Width 4096 }'); L.Add('function Csv($Data) { $Data | ConvertTo-Csv -NoTypeInformation }'); L.Add('$os = Get-CimInstance Win32_OperatingSystem'); L.Add('$cs = Get-CimInstance Win32_ComputerSystem'); L.Add('$cpu = Get-CimInstance Win32_Processor | Select-Object -First 1'); L.Add('$bios = Get-CimInstance Win32_BIOS'); L.Add('Save "Computer.txt" @('); L.Add(' "Computer: $($cs.Name)",'); L.Add(' "Manufacturer: $($cs.Manufacturer)",'); L.Add(' "Model: $($cs.Model)",'); L.Add(' "Windows: $($os.Caption) $($os.Version) (build $($os.BuildNumber))",'); L.Add(' "Windows installed: $($os.InstallDate.ToString("yyyy-MM-dd"))",'); L.Add(' "Processor: $($cpu.Name.Trim()) ($($cpu.NumberOfCores) cores, $($cpu.NumberOfLogicalProcessors) threads)",'); L.Add(' ("Memory: {0:N1} GB" -f ($cs.TotalPhysicalMemory / 1GB)),'); L.Add(' "BIOS: $($bios.Manufacturer) $($bios.SMBIOSBIOSVersion)")'); L.Add('$keys = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*",'); L.Add(' "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*",'); L.Add(' "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*"'); L.Add('Save "InstalledPrograms.csv" (Csv (Get-ItemProperty $keys | Where-Object DisplayName |'); L.Add(' Select-Object DisplayName, DisplayVersion, Publisher, InstallDate |'); L.Add(' Sort-Object DisplayName, DisplayVersion, Publisher -Unique))'); L.Add('Save "Updates.csv" (Csv (Get-HotFix | Select-Object HotFixID, Description,'); L.Add(' @{n="InstalledOn"; e={ if ($_.InstalledOn) { $_.InstalledOn.ToString("yyyy-MM-dd") } }} | Sort-Object HotFixID))'); L.Add('Save "Services.csv" (Csv (Get-CimInstance Win32_Service |'); L.Add(' Select-Object Name, DisplayName, StartMode, StartName, PathName | Sort-Object Name))'); L.Add('Save "Drivers.csv" (Csv (Get-CimInstance Win32_SystemDriver |'); L.Add(' Select-Object Name, DisplayName, StartMode, PathName | Sort-Object Name))'); L.Add('Save "ScheduledTasks.csv" (Csv (Get-ScheduledTask | Select-Object TaskPath, TaskName,'); L.Add(' @{n="Enabled"; e={ $_.Settings.Enabled }},'); L.Add(' @{n="Runs"; e={ ($_.Actions | ForEach-Object { ("$($_.Execute) $($_.Arguments)").Trim() }) -join "; " }} |'); L.Add(' Sort-Object TaskPath, TaskName))'); L.Add('Save "Startup.csv" (Csv (Get-CimInstance Win32_StartupCommand |'); L.Add(' Select-Object Name, Command, Location, User | Sort-Object Location, Name, Command))'); L.Add('$envs = foreach ($scope in "Machine", "User") {'); L.Add(' $v = [Environment]::GetEnvironmentVariables($scope)'); L.Add(' foreach ($k in ($v.Keys | Sort-Object)) { "[$scope] $k=$($v[$k])" } }'); L.Add('Save "Environment.txt" $envs'); L.Add('Save "Drives.csv" (Csv (Get-CimInstance Win32_LogicalDisk | Select-Object DeviceID, VolumeName, FileSystem,'); L.Add(' @{n="SizeGB"; e={ [math]::Round($_.Size / 1GB, 1) }} | Sort-Object DeviceID))'); L.Add('Save "NetworkAdapters.csv" (Csv (Get-CimInstance Win32_NetworkAdapter -Filter "PhysicalAdapter=True" |'); L.Add(' Select-Object Name, Manufacturer, MACAddress | Sort-Object Name, MACAddress))'); L.Add('Copy-Item -LiteralPath "$env:SystemRoot\System32\drivers\etc\hosts" -Destination (Join-Path $Out "hosts.txt")'); Result:=L.Text; finally L.Free; end; end; // Where the inventory is made: the folder on the Machine Inventory page of // the profile setup, else the default function FindRoot; var Shell; begin Result:=Trim(SBVariables.GetProperty('InventoryRoot', '', FALSE)); Shell:=CreateOleObject('WScript.Shell'); if (Result = '') then Result:='%LOCALAPPDATA%\SyncBack Inventory\'; Result:=IncludeTrailingPathDelimiter(Shell.ExpandEnvironmentStrings(Result)); Shell:=Unassigned; end; procedure ForceDir(D); var P; begin D:=ExcludeTrailingPathDelimiter(D); if (D = '') or DirectoryExists(D) then Exit; P:=ExtractFilePath(D); if (P <> '') and (ExcludeTrailingPathDelimiter(P) <> D) then ForceDir(P); CreateDir(D); end; // Empty a folder of files procedure ClearFolder(D); var Folder, FileItem, FilesEnum; begin if not gFSO.FolderExists(D) then Exit; Folder:=gFSO.GetFolder(D); FilesEnum:=TEnumVariant.Create(Folder.Files); try while FilesEnum.ForEach(FileItem) do gFSO.DeleteFile(FileItem.Path, TRUE); finally FilesEnum.Free; end; end; // // Make the inventory in \new\, then move each file into \current\ // only if it is different, so an unchanged file keeps its date & time. Files // the inventory no longer makes are removed from current\. // function MakeInventory; var NewDir, CurDir, Ps1, Cmd, RetVal, ErrMsg, Folder, FileItem, FilesEnum, Cur, Made; begin NewDir:=gRoot + 'new\'; CurDir:=gRoot + 'current\'; ForceDir(NewDir); ForceDir(CurDir); ClearFolder(NewDir); Ps1:=gRoot + 'MakeInventory.ps1'; WriteStringToFile(Ps1, InventoryScript); // Pass the folder without its trailing backslash: "C:\x\" would quote the " Cmd:='/hide powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "' + Ps1 + '" -Out "' + ExcludeTrailingPathDelimiter(NewDir) + '"'; RetVal:=0; ErrMsg:=''; if not SBSystem.Exec(Cmd, 300, RetVal, ErrMsg) then begin Result:='Cannot make the inventory: ' + ErrMsg; Exit; end; if not gFSO.FileExists(NewDir + 'Computer.txt') then begin Result:='Cannot make the inventory (PowerShell exit code ' + VarToStr(RetVal) + ')'; Exit; end; Made:=TStringList.Create; try // Move in what has changed Folder:=gFSO.GetFolder(NewDir); FilesEnum:=TEnumVariant.Create(Folder.Files); try while FilesEnum.ForEach(FileItem) do begin Made.Add(UpperCase(FileItem.Name)); Cur:=CurDir + FileItem.Name; if gFSO.FileExists(Cur) and (SBSystem.CRC32(Cur) = SBSystem.CRC32(FileItem.Path)) then gFSO.DeleteFile(FileItem.Path, TRUE) else begin if gFSO.FileExists(Cur) then gFSO.DeleteFile(Cur, TRUE); gFSO.MoveFile(FileItem.Path, Cur); end; end; finally FilesEnum.Free; end; // Remove what was not made this time Folder:=gFSO.GetFolder(CurDir); FilesEnum:=TEnumVariant.Create(Folder.Files); try while FilesEnum.ForEach(FileItem) do if (Made.IndexOf(UpperCase(FileItem.Name)) < 0) then gFSO.DeleteFile(FileItem.Path, TRUE); finally FilesEnum.Free; end; finally Made.Free; end; Result:=''; end; // The real file for a location path such as \Services.csv (blank if there is // no such file: the location has no folders) function RealName(P); begin Result:=''; if (P = '') or (P = '\') then Exit; P:=ExtractFileName(P); if (P <> '') and gFSO.FileExists(gRoot + 'current\' + P) then Result:=gRoot + 'current\' + P; end; //----------------------------------------------------------------------------- // Connecting //----------------------------------------------------------------------------- function LocConnect(MainThread); begin gFSO:=CreateOleObject('Scripting.FileSystemObject'); gRoot:=FindRoot; gMade:=FALSE; ForceDir(gRoot); if DirectoryExists(gRoot) then Result:='' else Result:='Cannot create the folder ' + gRoot; end; function LocDisconnect; begin gFSO:=Unassigned; Result:=''; end; // // Called before SyncBack lists the location: make the inventory (once per // connection, so the Differences window does not make it again) // function LocScanPrepare; begin Result:=''; if not gMade then begin SBSystem.UpdateFileStatus('Making the machine inventory...'); Result:=MakeInventory; gMade:=(Result = ''); end; end; function LocConnected; begin Result:=''; end; function LocReconnect; begin Result:=''; end; //----------------------------------------------------------------------------- // Reading //----------------------------------------------------------------------------- // Only the root folder exists function LocDirExists(FullPath); begin if (FullPath = '') or (FullPath = '\') then Result:='' else Result:='Folder does not exist'; end; function LocFileExists(FullPath); begin if (RealName(FullPath) <> '') then Result:='' else Result:='File does not exist'; end; function LocScanList(FullPath); var Folder, FileItem, FilesEnum; begin Result:=''; if not ((FullPath = '') or (FullPath = '\')) then Exit; if not gFSO.FolderExists(gRoot + 'current\') then Exit; Folder:=gFSO.GetFolder(gRoot + 'current\'); FilesEnum:=TEnumVariant.Create(Folder.Files); try while FilesEnum.ForEach(FileItem) do if not SBLocation.AddFileEx(FileItem.Name, '', VarToInt64(FileItem.Size), FileItem.Attributes, FileItem.DateLastModified, FileItem.DateCreated, '') then Exit; finally FilesEnum.Free; end; end; function LocGet(fromFName, toFName); var Real, r; begin Real:=RealName(fromFName); if (Real = '') then begin Result:='File does not exist'; Exit; end; r:=CopyFile(Real, toFName); if (r = 0) then Result:='' else Result:=SysErrorMessage(r); end; function LocCRC32(FullPath; var CRC32); begin if (RealName(FullPath) = '') then begin Result:='File does not exist'; Exit; end; CRC32:=SBSystem.CRC32(RealName(FullPath)); Result:=''; end; function LocGetAttributes(Filename; var Attrs); begin if (Filename = '') or (Filename = '\') then Attrs:=16 else if (RealName(Filename) <> '') then Attrs:=gFSO.GetFile(RealName(Filename)).Attributes else begin Result:='File does not exist'; Exit; end; Result:=''; end; function LocFreeSpace; begin Result:=-1; end; function LocAbilities; begin Result:=CAN_USEATTRIBUTES + CAN_HAVEEMPTYPATH + CAN_USECRC32; end; //----------------------------------------------------------------------------- // Writing: none of it is allowed //----------------------------------------------------------------------------- function LocMakeDir(FullPath); begin if (FullPath = '') or (FullPath = '\') then Result:='' else Result:=READ_ONLY; end; function LocRemoveDir(FullPath); begin Result:=READ_ONLY; end; function LocRenameDir(FromPath, ToPath); begin Result:=READ_ONLY; end; function LocRenameFile(FromName, ToName); begin Result:=READ_ONLY; end; function LocDeleteFile(FullPath; var DoesNotExist); begin DoesNotExist:=(RealName(FullPath) = ''); if DoesNotExist then Result:='' else Result:=READ_ONLY; end; function LocSetModDateTime(FullPath, ModDateTime); begin Result:=READ_ONLY; end; function LocSetCreateDateTime(FullPath, CreateDateTime); begin Result:=READ_ONLY; end; function LocSetAttributes(Filename, Attrs); begin Result:=READ_ONLY; end; function LocPutEx(fromFName, toFName, fromAttrs, fromDateTime, fromCreateDateTime, fromFileSize, fromNTFSSec, DoSafeCopy; var SafeFName); begin SafeFName:=''; Result:=READ_ONLY; end; //----------------------------------------------------------------------------- // The Machine Inventory page in the profile setup //----------------------------------------------------------------------------- // Only in profiles that use Machine Inventory as their location function ConfigWantSetupNode(IsGroup); begin Result:=not IsGroup and SBProfile.UsesScript; end; function ConfigNodeCaption; begin Result:='Machine Inventory'; end; function ConfigCanRevert; begin Result:=TRUE; end; function ConfigCanClose; begin Result:=TRUE; end; procedure ConfigFactoryDefaults; begin SBVariables.DeleteProperty('InventoryRoot'); end; procedure ConfigSetupDisplay; begin SBProfile.AddLabel('Files that describe this PC, made fresh each time the profile runs.', 20); SBProfile.AddEdit('Folder where the inventory is made (blank for %LOCALAPPDATA%\SyncBack Inventory\)', 260, FALSE, FALSE, 1); end; function ConfigLoadSettings; begin Result:=''; SBProfile.SetEdit(SBVariables.GetProperty('InventoryRoot', '', FALSE), 1); end; // Store a setting in the profile. One that has never been set and is still // the default is left unset, so just looking at this page and pressing Cancel // does not count as a change to the profile. procedure SaveSetting(Name, Value, Default); begin if (SBVariables.GetProperty(Name, '!NOTSET!', FALSE) = '!NOTSET!') and (Value = Default) then Exit; SBVariables.SetProperty(Name, Value); end; function ConfigSaveSettings(Silent); begin Result:=''; SaveSetting('InventoryRoot', Trim(SBProfile.GetEdit(1)), ''); end; procedure ConfigUpdateConditionals; begin end;