CyberPolice

An epic windows securing and hardening script
Log | Files | Refs | README

ManageUsersFromFile.ps1 (4531B)


      1 Write-Host "The CYBER POLICE are managing users by file..." -ForegroundColor Gray
      2 $thispath = Split-Path -parent $MyInvocation.MyCommand.Definition
      3 $path = Split-Path -parent $thispath
      4 $configpath = "$path/ConfigFiles"
      5 $output = Get-Content $path/CyberPoliceOutput/path.txt
      6 $userMgmtFilePath = ""
      7 $accounts = Get-Wmiobject Win32_UserAccount -filter 'LocalAccount=TRUE' | select-object -expandproperty Name
      8 
      9 $rawAdminData = @()
     10 $rawUserData = @()
     11 $rawOtherData = @()
     12 
     13 $admins = @()
     14 $adminPasswords = @()
     15 $users = @()
     16 
     17 function EditCheck {
     18     (Get-Content $configpath/PasteUsersHere.txt) | ? { $_.trim() -ne "" } | Set-Content $configpath/PasteUsersHere.txt
     19     Write-Host "Getting contents of raw user data..." -ForegroundColor Yellow
     20     Write-Host "Start of print out." -ForegroundColor Gray
     21     foreach ($line in Get-Content $configpath/PasteUsersHere.txt) {
     22         Write-Host $line -ForegroundColor Cyan
     23     }
     24     Write-Host "End of print out." -ForegroundColor Gray
     25     Write-host "Would you like to change the contents of this file? (Default is No)" -ForegroundColor Yellow 
     26     $Readhost = Read-Host "[Y/N]" 
     27     Switch ($ReadHost) { 
     28         Y { ChangeUserFile } 
     29         N { BeginUserManagement } 
     30         Default { BeginUserManagement } 
     31     }
     32 } 
     33 
     34 function ChangeUserFile {
     35     Write-Host "Waiting for user to edit..." -ForegroundColor Gray
     36     Start-Process notepad.exe $userMgmtFilePath -Wait
     37     Write-Host "Start of print out." -ForegroundColor Gray
     38     foreach ($line in Get-Content $configpath/PasteUsersHere.txt) {
     39         Write-Host $line -ForegroundColor Cyan
     40     }
     41     Write-Host "End of print out." -ForegroundColor Gray
     42     EditCheck
     43 }
     44 function BeginUserManagement {
     45     $writeTo = "Other"
     46     Write-Host "CYBER POLICE are starting user management..."
     47     foreach ($line in Get-Content $userMgmtFilePath) {
     48         if ($line -like "*Authorized Users*") {
     49             $writeTo = "User"
     50         }
     51         elseif ($line -like "*Authorized Administrators*") {
     52             $writeTo = "Admin"
     53         }
     54         Switch ($writeTo) {
     55             User { $rawUserData = $rawUserData + $line }
     56             Admin { $rawAdminData = $rawAdminData + $line }
     57             Default { $rawOtherData = $rawOtherData + $line }
     58         }
     59     }
     60     if ($rawOtherData.count -gt 0) {
     61         Write-Host "The CYBER POLICE found some extra data in PasteUsersHere.txt!" -ForegroundColor Red
     62         Write-Host "The file may have been created poorly or there was an error in editing!" -ForegroundColor Yellow
     63         Write-Host "Misc stuff found..." -ForegroundColor Gray
     64         $first, $rawOtherData = $rawOtherData
     65         foreach ($misc in $rawOtherData) {
     66             Write-Host $misc -ForegroundColor Cyan
     67         }
     68         Write-Host "End of misc stuff." -ForegroundColor Gray
     69     }
     70     Write-Host "Admins Found..." -ForegroundColor Gray
     71     $first, $rawAdminData = $rawAdminData
     72     foreach ($admin in $rawAdminData) {
     73         if ($admin -like "*password:*") {
     74             $password = $admin.split(":")
     75             $adminPasswords = $adminPasswords + $password[1].trim()
     76         }
     77         else {
     78             $adminSplit = $admin.split(" ")
     79             $admins = $admins + $adminSplit[0]
     80         }
     81     }
     82     for ($i = 0; $i -lt $admins.count; $i++) {
     83         Add-Content $output\ManagedUserOutput\authAdmins.txt "$($admins[$i]) $($adminPasswords[$i])"
     84         Write-Host "Admin: " -ForegroundColor Gray -NoNewline
     85         Write-Host $admins[$i] -ForegroundColor Cyan -NoNewline
     86         Write-Host " Password: " -ForegroundColor Gray -NoNewline
     87         Write-Host $adminPasswords[$i] -ForegroundColor Cyan
     88     }
     89 
     90     Write-Host "End of admins found." -ForegroundColor Gray
     91     Write-Host "Users Found..."
     92     $first, $rawUserData = $rawUserData
     93     foreach ($user in $rawUserData) {
     94         $users = $users + $user
     95         Add-Content $output\ManagedUserOutput\authUsers.txt $user
     96         Write-Host $user -ForegroundColor Cyan
     97     }
     98     Write-Host "End of users found." -ForegroundColor Gray
     99 }
    100 
    101 if (!(Test-Path $configpath/PasteUsersHere.txt -PathType Leaf)) {
    102     Write-Host "Raw users from README is not available!" -ForegroundColor Red
    103     Write-Host "Creating file..." -ForegroundColor Yellow
    104     New-Item -Path $configpath/PasteUsersHere.txt -ItemType "file" -Force | Out-Null
    105 }
    106 
    107 $userMgmtFilePath = "$configpath/PasteUsersHere.txt"
    108 New-Item -path $output\ManagedUserOutput -name authAdmins.txt -type "file" -Force | Out-Null
    109 New-Item -path $output\ManagedUserOutput -name authUsers.txt -type "file" -Force | Out-Null
    110 EditCheck