Last active
April 7, 2023 16:27
-
-
Save ned1313/9143039 to your computer and use it in GitHub Desktop.
Grant user array log on as a service right in PowerShell
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 Grant-LogOnAsService{ | |
param( | |
[string[]] $users | |
) | |
#Get list of currently used SIDs | |
secedit /export /cfg tempexport.inf | |
$curSIDs = Select-String .\tempexport.inf -Pattern "SeServiceLogonRight" | |
$Sids = $curSIDs.line | |
$sidstring = "" | |
foreach($user in $users){ | |
$objUser = New-Object System.Security.Principal.NTAccount($user) | |
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier]) | |
if(!$Sids.Contains($strSID) -and !$sids.Contains($user)){ | |
$sidstring += ",*$strSID" | |
} | |
} | |
if($sidstring){ | |
$newSids = $sids + $sidstring | |
Write-Host "New Sids: $newSids" | |
$tempinf = Get-Content tempexport.inf | |
$tempinf = $tempinf.Replace($Sids,$newSids) | |
Add-Content -Path tempimport.inf -Value $tempinf | |
secedit /import /db secedit.sdb /cfg ".\tempimport.inf" | |
secedit /configure /db secedit.sdb | |
gpupdate /force | |
} | |
else{ | |
Write-Host "No new sids" | |
} | |
del ".\tempimport.inf" -force -ErrorAction SilentlyContinue | |
del ".\secedit.sdb" -force -ErrorAction SilentlyContinue | |
del ".\tempexport.inf" -force | |
} |
I agree with pmcm, there is a definitely a bug.
It's not broken, Github inserts special characters into the code if you copy it from the website. Just click "Download Zip"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi , I'm trying to use this code and getting a error on this line:
$tempinf = $tempinf.Replace($Sids,$newSids)
"Method invocation failed because [System.Object[]] doesn't contain a method named 'Replace'.
At C:\Users\pmcm\Desktop\NTRight.ps1:22 char:36
Can you help or give me some advice?