0

I am preparing a script. What I want to do is to get the user SID from Active Directory and use it in another command.

I tried to do this command but couldn't run it

$username = Read-Host -Prompt 'Erdem.OV'
$SID = Get-ADUser -Identity $username | select-object SID
$PSExec = "C:\Windows\System32\PsExec.exe"
$hostname = Read-Host -Prompt 'hostname'

$command = 'cmd /c "reg add "HKEY_USERS\"$SID"\SOFTWARE\Policies\Microsoft\Internet Explorer\Control Panel" /v ConnectionsTab /t REG_DWORD /d 0"'

Start-Process -Filepath "$PsExec" -ArgumentList "\\$hostname $command"

Thanks.

virtue
  • 13
  • 4
  • 1
    What happened? Did your wrist cramp? Did your computer catch on fire? Or did you actually manage to run the code, but it threw an error? If it's the latter, please post the error message :) – Mathias R. Jessen Sep 01 '22 at 14:17
  • 1
    `... |select-object -ExpandProperty SID`? – iRon Sep 01 '22 at 14:24
  • Hi, there is no error message. :) But it didn't work – virtue Sep 01 '22 at 14:36
  • Most frequent powershell question ever. – js2010 Sep 01 '22 at 18:21
  • In short: [`Select-Object`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/select-object) (`select`) by default returns _a `[pscustomobject]` instance_ that has the _requested properties_ - even when you're only asking for a _single_ property. To get only that property's _value_, use `-ExpandProperty $propertyName` instead - see the [linked duplicate](https://stackoverflow.com/q/48807857/45375) for details and alternatives, notably the ability to simply use `(...).$propertyName` – mklement0 Sep 04 '22 at 18:19

2 Answers2

0

simply do this:

(Get-ADUser -Identity $username).sid.value
Toni
  • 1,738
  • 1
  • 3
  • 11
  • Thanks, script still not working and there is no error message but it gives the true value. I guess it's not copy to the right location in the $SID path. "HKEY_USERS\"$SID"\SOFTWARE\ – virtue Sep 01 '22 at 14:53
0

It worked like this. :)

$username = Read-Host -Prompt 'Username'
$SID = (Get-ADUser -Identity $username).sid.value
$PSExec = "C:\Windows\System32\PsExec.exe"
$hostname = Read-Host -Prompt 'hostname'

$command = 'cmd /c'
$command2 = '"reg add "HKEY_USERS\'
$command3 = '\SOFTWARE\Policies\Microsoft\Internet Explorer\Control Panel" /v ConnectionsTab /t REG_DWORD /d 0"'

Start-Process -Filepath "$PsExec" -ArgumentList "\\$hostname $command $command2$SID$command3"

Thanks everyone.

virtue
  • 13
  • 4