Skip to content

Instantly share code, notes, and snippets.

@ned1313
Last active April 7, 2023 16:27
Show Gist options
  • Save ned1313/9143039 to your computer and use it in GitHub Desktop.
Save ned1313/9143039 to your computer and use it in GitHub Desktop.
Grant user array log on as a service right in PowerShell
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
}
@pmcm
Copy link

pmcm commented Jul 1, 2015

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

  •     $tempinf = $tempinf.Replace <<<< ($Sids,$newSids)
    
    • CategoryInfo : InvalidOperation: (Replace:String) [], RuntimeException
    • FullyQualifiedErrorId : MethodNotFound"
      Can you help or give me some advice?

@gandrusz
Copy link

I agree with pmcm, there is a definitely a bug.

@RIKIKU
Copy link

RIKIKU commented Feb 16, 2016

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