User Rights Assignment - Adding a Username using powershell

Using the Windows GUI, I would be doing this:

gpedit.msc
      Computer Configuration / Windows Settings / Security Settings / Local Policies
                              Audit Policy
                                    Each Policy: Enable Success and Failure
                              THEN:
User Rights Assignment
Manage Auditing and Security Log  … add “[username]”

Now I want to automate doing this using powershell.  I've found the script by Tony Pombo but it seems unwieldy for such a simple task and I've had lots of problems getting the commands to be recognized at all.

Suggestions?
LVL 27
Fred MarshallPrincipalAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subash SundharanIT Infrastructure Architect Commented:
The best way to manage these settings is using GPO. Create a Group policy and assign it to a group of computers or and OU.

Regarding the PowerShell option :
Using the Tony's module is pretty simple..
You need to download the module it a folder (In our example module is placed in C:\temp)
After that open PowerShell console using Run As Administrator Option. Sun the following commands to assign the privileges for user.
Import-Module C:\temp\UserRights.ps1
Grant-UserRight -Computer ComputerA -Account Domain\UserA -Right SeSecurityPrivilege

Open in new window

Let me know if you get any error.
1
Fred MarshallPrincipalAuthor Commented:
Subsun:  Thanks!  How would you change that approach for workstations on a peer-to-peer network?  No "group of computers" possibility that I know of.

It would be great to do as you suggest and run these commands all from my own workstation!
If need be, I can run them on the target workstation as I have access to them all.
0
Subash SundharanIT Infrastructure Architect Commented:
For computers In workgroup, to add local users, we don't have to specify the domain part.
Grant-UserRight -Computer ComputerA -Account UserA -Right SeSecurityPrivilege

Open in new window

I don't have any workgroup computer to test it, but I am pretty sure that it should work. I will test this as soon as I get a chance and let you know result.
1
How the Cloud Can Help You as an MSSP

Today, every Managed Security Service Provider (MSSP) needs a platform to deliver effective and efficient security-as-a-service to their customers. Scale, elasticity and profitability are a few of the many features that a Cloud platform offers. Register today to learn more!

Subash SundharanIT Infrastructure Architect Commented:
gpedit.msc
       Computer Configuration / Windows Settings / Security Settings / Local Policies
                               Audit Policy
                                     Each Policy: Enable Success and Failure
BTB, for enable auditing, you can use auditpol, Example.
auditpol /set /category:"System","Account Management","Account Logon","Logon/Logoff","Policy Change" /failure:enable /success:enable

Open in new window

Since the computers are not in domain, you probably have to use different credentials for each computer. If all the computer have same credential (Example, if all computers have same password for their administrator account), then it'll be easy to create a script which you can run from a work station.
1
Fred MarshallPrincipalAuthor Commented:
Subsun:  Yes that works!  Thank you!
The only nit remaining is that the "username" shows up in the settings as an indecipherable *S-1-5-21-2973359201 .........
I hate to think of living with that because how might one check the rights vs. usernames later on then?
Maybe there's a way to read and translate?  That would be better than nothing.
0
Fred MarshallPrincipalAuthor Commented:
re: auditpol
I have a script for that already .. sorry I didn't mention it.
But so far, it has to run on the target workstation.
Now, if it could be run on my workstation and target various others (as for UserRights) then that would be great!
0
Subash SundharanIT Infrastructure Architect Commented:
What if you use ComputerA\UserA instead of UserA
Grant-UserRight -Computer ComputerA -Account ComputerA\UserA -Right SeSecurityPrivilege

Open in new window

Does this shows the correct user name instead of SID (S-1-5-21-2973359201)
0
Fred MarshallPrincipalAuthor Commented:
Grant-UserRight -Computer WIN10PRO3500 -Account WIN10PRO3500\Computerguy -Right SecurityPr
ivilege

Exception calling "AddPrivilege" with "2" argument(s): "Some or all identity references could not be translated."
At C:\Users\Public\Scripts\UserRights.ps1:523 char:17
+                 $lsa.AddPrivilege($Acct,$Priv)
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : IdentityNotMappedException
0
Subash SundharanIT Infrastructure Architect Commented:
I didn't get a chance to test this. I will update as soon as I can..
0
Fred MarshallPrincipalAuthor Commented:
thanks!!
0
Subash SundharanIT Infrastructure Architect Commented:
OK.. As per my testing in order to add the local user the command has to run locally..

What is the operating system on your computers? is it windows 8 or 10?
0
Fred MarshallPrincipalAuthor Commented:
All are Windows 10 Pro.
0
Subash SundharanIT Infrastructure Architect Commented:
I guess you will have remote Powershell enabled. Can you test command
Invoke-Command -ComputerName <RemotecompterNamehere> -ScriptBlock {Ipconfig} -Credential (Get-Credential)

Open in new window

Command will prompt to enter credentials, enter remote computer credentials.
1
Fred MarshallPrincipalAuthor Commented:
Very interesting!
So it might look like this:

Invoke-Command -ComputerName 192.168.1.33 -ScriptBlock {Grant-UserRight -Computer ComputerA -Account UserA -Right SeSecurityPrivilege} -Credential (Get-Credential)

I'm getting an error:
[192.168.1.33] Connecting to remote server 192.168.1.33 failed with the following error message : The WinRM client cannot process the request. If the authentication scheme is different from Kerberos, or if the client computer is not joined to a domain, then HTTPS transport must be used or the destination machine must be added to the TrustedHosts configuration setting. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated. You can get more information about that by running the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help topic.
    + CategoryInfo          : OpenError: (192.168.1.33:String) [], PSRemotingTransportException
    + FullyQualifiedErrorId : ServerNotTrusted,PSSessionStateBroken

Maybe the amount of preparation on the target machines is greater than just running the commands locally???
0
Subash SundharanIT Infrastructure Architect Commented:
Okie.. It's really a pain to have remote PowerShell on workgroup computers. but once enabled, you can remotely run PowerShell commands and automation will be easy.. But still you will have to deal with access errors as each server you need a different credential..
Ref : https://blogs.msdn.microsoft.com/wmi/2009/07/24/powershell-remoting-between-two-workgroup-machines/
If it's less number of machines, you can save the module in a share.. example \\10.0.0.1\script\UserRights.ps1
and try runing following commands on locally on computers..
cmd /c 'auditpol.exe /set /category:"System","Account Management","Account Logon","Logon/Logoff","Policy Change" /failure:enable /success:enable'
Copy "\\10.0.0.1\script\UserRights.ps1" "$($Env:Temp)"
Import-Module "$($Env:Temp)\UserRights.ps1"
Grant-UserRight -Account UserA -Right SeSecurityPrivilege

Open in new window

Any reason for computers are not added to domain?
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fred MarshallPrincipalAuthor Commented:
Any reason for computers are not added to domain?
... like there is no server OS machine..
I know this is a stretch for peer-to-peer but it's been working well for us for some time.  
Some of the issues we're dealing with here (i.e. the script) is not only to make the setup more efficient but to make repeated setups more efficient because recent MS Windows 10 updates have been erasing our firewall settings (amongst a one or two other things).  So I'm trying to roll it all up into one script.

Had you not enticed me into the remote idea I'd have been done by now!  :-)
Putting the module in a share is a great idea in moving forward!  The idea was bouncing around in the back of my head but hadn't been fully formed.  
I really appreciate the excellent suggestions and help!

The last hurdle is DCOM settings in a separate question:
https://www.experts-exchange.com/questions/28973373/DCOMCNFG-Settings-with-Powershell.html
I've not worked on any of the suggestions yet much.
0
Fred MarshallPrincipalAuthor Commented:
Really excellent help!!
0
Subash SundharanIT Infrastructure Architect Commented:
I can see few experts has responded to DECOM question, is it not working?
0
Fred MarshallPrincipalAuthor Commented:
DCOM
I've not worked on any of the suggestions yet much.
0
Subash SundharanIT Infrastructure Architect Commented:
I had an old script which I was using for similar requirement. I have made it in to following function, which should work locally, try and let me know if you get any error..
Add-DCOMPermission -User UserName

Open in new window

Function Add-DCOMPermission {
PARAM([string]$User)

function get-sid {
 PARAM ($DSIdentity)
 $ID = new-object System.Security.Principal.NTAccount($DSIdentity)
 return $ID.Translate( [System.Security.Principal.SecurityIdentifier] ).toString()
}

$sid = get-sid $User

#DefaultLaunchPermission - Local Launch, Remote Launch, Local Activation, Remote Activation
$DCOMSDDLDefaultLaunchPermission = "A;;CCDCLCSWRP;;;$sid"

#DefaultAccessPermision - Local Access, Remote Access
$DCOMSDDLDefaultAccessPermision = "A;;CCDCLC;;;$sid"

#PartialMatch
$DCOMSDDLPartialMatch = "A;;\w+;;;$sid"

 # Get the respective binary values of the DCOM registry entries
 $Reg = [WMIClass]"\\$($env:COMPUTERNAME)\root\default:StdRegProv"
 $DCOMDefaultLaunchPermission = $Reg.GetBinaryValue(2147483650,"software\microsoft\ole","DefaultLaunchPermission").uValue
 $DCOMDefaultAccessPermission = $Reg.GetBinaryValue(2147483650,"software\microsoft\ole","DefaultAccessPermission").uValue

 # Convert the current permissions to SDDL
 $converter = new-object system.management.ManagementClass Win32_SecurityDescriptorHelper
 $CurrentDCOMSDDLDefaultLaunchPermission = $converter.BinarySDToSDDL($DCOMDefaultLaunchPermission)
 $CurrentDCOMSDDLDefaultAccessPermission = $converter.BinarySDToSDDL($DCOMDefaultAccessPermission)

 # Build the new permissions
 if (($CurrentDCOMSDDLDefaultLaunchPermission.SDDL -match $DCOMSDDLPartialMatch) -and ($CurrentDCOMSDDLDefaultLaunchPermission.SDDL -notmatch $DCOMSDDLDefaultLaunchPermission))
 {
   $NewDCOMSDDLDefaultLaunchPermission = $CurrentDCOMSDDLDefaultLaunchPermission.SDDL -replace $DCOMSDDLPartialMatch, $DCOMSDDLDefaultLaunchPermission
 }
 else
 {
   $NewDCOMSDDLDefaultLaunchPermission = $CurrentDCOMSDDLDefaultLaunchPermission.SDDL + "(" + $DCOMSDDLDefaultLaunchPermission + ")"
 }

 if (($CurrentDCOMSDDLDefaultAccessPermission.SDDL -match $DCOMSDDLPartialMatch) -and ($CurrentDCOMSDDLDefaultAccessPermission.SDDL -notmatch $DCOMSDDLDefaultAccessPermision))
 {
   $NewDCOMSDDLDefaultAccessPermission = $CurrentDCOMSDDLDefaultAccessPermission.SDDL -replace $DCOMSDDLPartialMatch, $DCOMSDDLDefaultAccessPermision
 }
 else
 {
   $NewDCOMSDDLDefaultAccessPermission = $CurrentDCOMSDDLDefaultAccessPermission.SDDL + "(" + $DCOMSDDLDefaultAccessPermision + ")"
 }

 # Convert SDDL back to Binary
 $DCOMbinarySDDefaultLaunchPermission = $converter.SDDLToBinarySD($NewDCOMSDDLDefaultLaunchPermission)
 $DCOMconvertedPermissionDefaultLaunchPermission = ,$DCOMbinarySDDefaultLaunchPermission.BinarySD

 $DCOMbinarySDDefaultAccessPermission = $converter.SDDLToBinarySD($NewDCOMSDDLDefaultAccessPermission)
 $DCOMconvertedPermissionsDefaultAccessPermission = ,$DCOMbinarySDDefaultAccessPermission.BinarySD

 # Apply the changes
 if ($CurrentDCOMSDDLDefaultLaunchPermission.SDDL -match $DCOMSDDLDefaultLaunchPermission)
 {
   write-host "DefaultLaunchPermission already set for $user"
 }
 else
 {
   $result = $Reg.SetBinaryValue(2147483650,"software\microsoft\ole","DefaultLaunchPermission", $DCOMbinarySDDefaultLaunchPermission.binarySD)
   if($result.ReturnValue='0'){write-host "Applied DefaultLaunchPermission for $User"}
 }

 if ($CurrentDCOMSDDLDefaultAccessPermission.SDDL -match $DCOMSDDLDefaultAccessPermision)
 {
   write-host "DefaultAccessPermission already set for $user"
 }
 else
 {
   $result = $Reg.SetBinaryValue(2147483650,"software\microsoft\ole","DefaultAccessPermission", $DCOMbinarySDDefaultAccessPermission.binarySD)
   if($result.ReturnValue='0'){write-host "Applied DefaultAccessPermission for $user"}

 }
}

Open in new window

0
Fred MarshallPrincipalAuthor Commented:
It appears that this one works!!  Thank youi!
0
Subash SundharanIT Infrastructure Architect Commented:
You are welcome!
0
Fred MarshallPrincipalAuthor Commented:
Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.