Last active
January 24, 2025 03:57
-
-
Save Mayyhem/ef01ebf30b2779603bb7e0db83bc04f1 to your computer and use it in GitHub Desktop.
Query SCCM site server remote registry for the currently logged on user account
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-SiteServerCurrentUser { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$true)] | |
[string]$ComputerName, | |
[Parameter(Mandatory=$false)] | |
[string]$SubKeyPath = "SOFTWARE\Microsoft\SMS\CurrentUser" | |
) | |
# Access the remote registry | |
try { | |
# Connect to the remote registry's LocalMachine hive | |
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $ComputerName) | |
# Open the target subkey | |
$key = $reg.OpenSubKey($SubKeyPath) | |
if ($key) { | |
# Get all value names in the subkey | |
$valueNames = $key.GetValueNames() | |
if ($valueNames.Count -eq 0) { | |
Write-Output "`nNo values found in the subkey" | |
} | |
else { | |
Write-Output "`nValues in ${subKeyPath}:" | |
foreach ($valueName in $valueNames) { | |
$value = $key.GetValue($valueName) | |
# If value name is empty, it's the default value | |
$displayName = if ($valueName -eq '') {'(Default)'} else {$valueName} | |
Write-Output " $displayName : $value" | |
} | |
} | |
# Close the key | |
$key.Close() | |
} | |
else { | |
Write-Output "Registry key '$SubKey' does not exist on ${ComputerName}." | |
} | |
# Close the registry connection | |
$reg.Close() | |
} | |
catch { | |
Write-Output "Failed to access registry on ${ComputerName}. Error: $_" | |
} | |
} | |
# Example | |
# Get-SiteServerCurrentUser -ComputerName ps1-pss |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment