Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Lookup registry value of a key that contains the SID of logged on user

Status
Not open for further replies.

scottsk79

Technical User
Feb 7, 2012
1
US
It has been nearly 10 years since I have had to do any type of scripting and am having troubles putting two scripts together in a for each loop.
I need to find the SID of the currently logged on user and then use that SID in the path of the registry value I am looking for. I have been able to do this with the following code:

Code:
$List = get-content C:\Powershell\Scripts\wsns.txt
$objUser = New-Object System.Security.Principal.NTAccount("us", "sxskiba")
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
$strSID.Value
Get-regvalue -computername $List -hive "Users" -key "$strSID\Software\Microsoft\Windows\CurrentVersion\Internet Settings" -value "AutoConfigURL" | Export-csv C:\Powershell\Scripts\output.csv -notypeinformation

However, this code has the domain and username explicitly defined. I need the domain and username to be found with powershell given a list of workstations. I have this script that will find the currently logged on domain\user

Code:
get-content C:\Powershell\Scripts\wsns.txt | ForEach-Object {gwmi -computer $_ -class win32_computerSystem} | fl Name, UserName | out-file C:\Powershell\Scripts\output.csv

How do I put these two together in a for each loop to work through a list of workstations? Desired output would be a csv with the computer name, logged on username, and value of the AutoConfigURL key.
 
Untested but give this a try.

Code:
get-content C:\Powershell\Scripts\wsns.txt | 
ForEach-Object {gwmi -computer $_ -class win32_computerSystem} | fl Name, UserName | 
% {
$List = get-content C:\Powershell\Scripts\wsns.txt
$objUser = New-Object System.Security.Principal.NTAccount($_.Name, $_.UserName)
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
$strSID.Value
Get-regvalue -computername $List -hive "Users" -key "$strSID\Software\Microsoft\Windows\CurrentVersion\Internet Settings" -value "AutoConfigURL" | Export-csv C:\Powershell\Scripts\output.csv -notypeinformation 
}

I hope that helps.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
I can see one minor issue there - the export-csv is inside the for-each loop. You can only export-csv once. You can't append (a big complaint from PowerShell guys like me), so export-csv is gonna complain the file exists. If you overwrite the file, then it's only ever going to have the last iteration of data.

What you'd probably need to do is just append the data to a text file. You could then read it all in at the end and export to csv in one move. YMMV.

Do you have your Tek-Tips.com Swag? I've got mine!

Stop by the new Tek-Tips group at LinkedIn.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top