Principais conclusões
- Eficiência do PowerShell: Utiliza o PowerShell para criar e gerenciar contas de usuários locais com eficiência.
- Recursos avançados: Inclui agendamento para ativar/desativar contas e adicionar usuários a grupos de administradores.
- Segurança automatizada: Gera senhas seguras automaticamente, aumentando a segurança da conta.
- Gerenciamento de tempo: Economiza tempo no gerenciamento de contas de usuários, o que é particularmente útil para MSPs.
- Script personalizável: Permite a personalização por meio de parâmetros definidos pelo usuário.
- Agendar tarefas: Capaz de agendar tarefas para ativar ou desativar contas em horários específicos.
- Gerenciamento de grupos: Facilita a adição de usuários a grupos específicos, incluindo administradores.
- Adaptável para criação em massa: Embora tenha sido projetado para contas individuais, pode ser adaptado para a criação de usuários em massa.
- Implicações de segurança: Destaca a necessidade de controles de acesso rigorosos e auditorias regulares.
- NinjaOne Compatibilidade: Pode ser integrado ao NinjaOne para gerenciamento centralizado de TI.
No cenário digital atual, o gerenciamento eficiente das contas de usuário é crucial para os profissionais de TI. O PowerShell, uma poderosa linguagem de script e shell de linha de comando, oferece uma maneira dinâmica de lidar com essas tarefas. Esta postagem do blog explora um script do PowerShell que não apenas cria contas de usuário locais, mas também oferece opções de agendamento e gerenciamento de grupos, mostrando sua utilidade em vários ambientes de TI.
Histórico
Esse script do PowerShell se destaca por sua capacidade de criar contas de usuário locais com recursos avançados, como agendamento de datas de ativação/desativação e adição de usuários ao grupo de administradores locais. Essas funcionalidades são particularmente relevantes para profissionais de TI e provedores de serviços gerenciados (MSPs) que gerenciam um grande número de contas de usuários e precisam garantir o acesso seguro e em tempo hábil aos sistemas.
O roteiro:
#Requires -Version 5.1
<#
.SYNOPSIS
Create a local user account with options to enable and disable at specific dates, and add to local admin group. Saves randomly generated password to a custom field.
.DESCRIPTION
You can specify when the account will be enabled and/or disabled.
You can have the account be added as a member of the local Administrators group.
PARAMETER: -UserNameToAdd "JohnTSmith" -Name "John T Smith"
Create use with the name JohnTSmith and display name of John T Smith.
.EXAMPLE
-UserNameToAdd "JohnTSmith" -Name "John T Smith"
## EXAMPLE OUTPUT ##
User JohnTSmith has been created successfully.
User JohnTSmith was added to the local Users group.
PARAMETER: -UserNameToAdd "JohnTSmith" -Name "John T Smith" -DateAndTimeToEnable "Monday, January 1, 2020 1:00:00 PM"
Create use with the name JohnTSmith and display name of John T Smith.
The user will start out disabled.
A scheduled task will be create to enable the user after "Monday, January 1, 2020 1:00:00 PM"
.EXAMPLE
-UserNameToAdd "JohnTSmith" -Name "John T Smith" -DateAndTimeToEnable "Monday, January 1, 2020 1:00:00 PM"
## EXAMPLE OUTPUT ##
User JohnTSmith has been created successfully.
User JohnTSmith was added to the local Users group.
Created Scheduled Task: Enable User JohnTSmith
User JohnTSmith will be able to login after Monday, January 1, 2020 1:00:00 PM.
PARAMETER: -UserNameToAdd "JohnTSmith" -Name "John T Smith" -DisableAfterDays 10
Create use with the name JohnTSmith and display name of John T Smith.
The user will be disabled after 10 days after the user's creation.
.EXAMPLE
-UserNameToAdd "JohnTSmith" -Name "John T Smith" -DisableAfterDays 10
## EXAMPLE OUTPUT ##
User JohnTSmith has been created successfully.
User JohnTSmith was added to the local Users group.
PARAMETER: -UserNameToAdd "JohnTSmith" -Name "John T Smith" -AddToLocalAdminGroup
Create use with the name JohnTSmith and display name of John T Smith.
User will be added as a member of the local Administrators group.
.EXAMPLE
-UserNameToAdd "JohnTSmith" -Name "John T Smith" -AddToLocalAdminGroup
## EXAMPLE OUTPUT ##
User JohnTSmith has been created successfully.
User JohnTSmith was added to the local Users group.
User JohnTSmith was added to the local Administrators group.
.OUTPUTS
None
.NOTES
Minimum OS Architecture Supported: Windows 10, Windows Server 2016
Release Notes: Initial Release
By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/terms-of-use.
Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms.
Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party.
Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider.
Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations.
Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks.
Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script.
EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).
#>
[CmdletBinding()]
param (
[Parameter()]
[String]$UserNameToAdd,
[Parameter()]
[String]$Name,
[Parameter()]
[String]$PasswordCustomField,
[Parameter()]
[int]$PasswordLength,
[Parameter()]
[DateTime]$DateAndTimeToEnable,
[Parameter()]
[int]$DisableAfterDays,
[Parameter()]
[Switch]$AddToLocalAdminGroup,
[Parameter()]
$PasswordOptions
)
begin {
function Test-IsElevated {
$id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$p = New-Object System.Security.Principal.WindowsPrincipal($id)
$p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
}
function New-SecurePassword {
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[int]$Length = 16,
[Parameter(Mandatory = $false)]
[bool]$IncludeSpecialCharacters = $true
)
# .NET class for generating cryptographically secure random numbers
$cryptoProvider = New-Object System.Security.Cryptography.RNGCryptoServiceProvider
$SpecialCharacters = if ($IncludeSpecialCharacters) { '!@#$%&-' }
$passwordChars = "abcdefghjknpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ0123456789$SpecialCharacters"
$password = for ($i = 0; $i -lt $Length; $i++) {
$byte = [byte[]]::new(1)
$cryptoProvider.GetBytes($byte)
$charIndex = $byte[0] % $passwordChars.Length
$passwordChars[$charIndex]
}
return $password -join ''
}
function New-LocalUserFromNinja {
param(
[string]$Username,
[string]$Name,
[string]$PasswordCustomField,
[DateTime]$EnableDate,
[int]$DisableAfterDays,
[switch]$AddToLocalAdminGroup
)
# Generate a secure localUserPassword
$Password = New-SecurePassword -Length $PasswordLength -IncludeSpecialCharacters $true
if ($Username -and $Name) {
# Check if the user already exists
if (-not (Get-LocalUser -Name $Username -ErrorAction SilentlyContinue)) {
# Create new local user
$UserSplat = @{
Name = "$Username"
FullName = "$Name"
Password = ConvertTo-SecureString -String $($Password -join '') -AsPlainText -Force
Description = "User account created on $(Get-Date)"
PasswordNeverExpires = $false
}
if ($EnableDate -and $EnableDate -gt (Get-Date)) {
$UserSplat['Disabled'] = $true
}
if (-not $EnableDate -and $DisableAfterDays) {
$UserSplat['AccountExpires'] = $(Get-Date).AddDays($DisableAfterDays)
}
elseif ($DisableAfterDays) {
$UserSplat['AccountExpires'] = $(Get-Date $EnableDate).AddDays($DisableAfterDays)
}
if ($env:passwordOptions -like 'Password Never Expires' -or $PasswordOptions -like 'Password Never Expires') {
$UserSplat['PasswordNeverExpires'] = $true
}
New-LocalUser @UserSplat
if ($env:passwordOptions -like 'User Must Change Password' -or $PasswordOptions -like 'User Must Change Password') {
net.exe user $Username /logonpasswordchg:yes
}
# Write it to a secure custom field
if ((Get-LocalUser -Name $Username -ErrorAction SilentlyContinue)) {
Write-Host "User $Username has been created successfully."
if ($PasswordCustomField -like "null") {
Write-Host "CustomField not specified."
Write-Host "Password set to: $Password"
}
else {
Ninja-Property-Set -Name "$PasswordCustomField" -Value "$Password"
Write-Host "Password saved to $PasswordCustomField Custom Field."
}
}
else {
throw "Failed to create User $Username."
}
Add-LocalGroupMember -Group $(Get-LocalGroup -Name "Users") -Member $Username
Write-Host "User $UserName was added to the local Users group."
# If date to enable account is specified, disable account until then
if ($EnableDate) {
if ($EnableDate -gt (Get-Date)) {
# Schedule a job to enable the user at the specified date
$TaskSplat = @{
Description = "Ninja Automation Enable User $Username"
Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -WindowStyle Hidden -Command & {Enable-LocalUser -Name `"$Username`"}"
Trigger = New-ScheduledTaskTrigger -Once -At $EnableDate
Principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount
}
try {
New-ScheduledTask @TaskSplat | Register-ScheduledTask -User "System" -TaskName "Enable User $Username" | Out-Null
if ($(Get-ScheduledTask -TaskName "Enable User $Username")) {
Write-Host "Created Scheduled Task: Enable User $Username"
}
else {
throw "Failed to find scheduled task with the name 'Enable User $Username'"
}
}
catch {
Write-Error $_
throw "Failed to create Enable User scheduled task."
}
Write-Host "User $Username will be able to login after $EnableDate."
}
}
else {
Write-Host "No Enable Date is Set, $UserName is able to login now."
}
# Add to local admin group if specified
if ($AddToLocalAdminGroup) {
Add-LocalGroupMember -Group $(Get-LocalGroup -Name "Administrators") -Member $Username
if (-not (Get-LocalGroupMember -Group $(Get-LocalGroup -Name "Administrators") -Member $Username)) {
throw "Failed to add user to local Administrators group."
}
Write-Host "User $UserName was added to the local Administrators group."
}
}
else {
Write-Host "User $Username already exists."
}
}
else {
throw "Username and Name are required to create a local account."
}
}
}
process {
if ($env:usernameToAdd -and $env:usernameToAdd -like "null") {
Write-Error "usernameToAdd($env:usernameToAdd) parameter is invalid."
exit 1
}
if ($env:name -and $env:name -like "null") {
Write-Error "name($env:name) parameter is invalid."
exit 1
}
if ($env:passwordCustomField -and $env:passwordCustomField -like "null") {
Write-Error "passwordCustomField($env:passwordCustomField) parameter is invalid."
exit 1
}
if (-not (Test-IsElevated)) {
Write-Error -Message "Access Denied. Please run with Administrator privileges."
exit 1
}
$params = @{
Username = if ($PSBoundParameters.ContainsKey("UserNameToAdd")) { $UserNameToAdd }else { $env:usernameToAdd }
Name = if ($PSBoundParameters.ContainsKey("Name")) { $Name }else { $env:name }
}
# Conditionally add EnableDate
if ($env:dateAndTimeToEnable -and $env:dateAndTimeToEnable -notlike "null") {
$params["EnableDate"] = Get-Date "$env:dateAndTimeToEnable"
}
elseif ($PSBoundParameters.ContainsKey("DateAndTimeToEnable") -and $DateAndTimeToEnable) {
$params["EnableDate"] = $DateAndTimeToEnable
}
# Conditionally add DisableAfterDays
if ($env:disableAfterDays -notlike "null") {
$params["DisableAfterDays"] = $env:disableAfterDays
}
elseif ($PSBoundParameters.ContainsKey("DisableAfterDays")) {
$params["DisableAfterDays"] = $DisableAfterDays
}
# Conditionally add AddToLocalAdminGroup
if ([Convert]::ToBoolean($env:addToLocalAdminGroup)) {
$params["AddToLocalAdminGroup"] = $true
}
elseif ($PSBoundParameters.ContainsKey("AddToLocalAdminGroup")) {
$params["AddToLocalAdminGroup"] = $AddToLocalAdminGroup
}
# Conditionally add AddToLocalAdminGroup
if ($env:passwordCustomField -notlike "null") {
$params["PasswordCustomField"] = $env:passwordCustomField
}
elseif ($env:passwordCustomField -like "null") {
Write-Error "passwordCustomField: is Required"
exit 1
}
elseif ($PSBoundParameters.ContainsKey("PasswordCustomField")) {
$params["PasswordCustomField"] = $PasswordCustomField
}
if ($env:passwordLength -notlike "null") {
$PasswordLength = $env:passwordLength
}
elseif (-not $passwordLength) {
$PasswordLength = 20
}
try {
New-LocalUserFromNinja @params
}
catch {
Write-Error $_
exit 1
}
}
end {
}
Acesse mais de 300 scripts no NinjaOne Dojo
Detalhamento
O script é estruturado em várias seções principais, cada uma executando uma tarefa específica:
- Definição do parâmetro: Parâmetros como UserNameToAdd, Name e DateAndTimeToEnable são declarados, permitindo a personalização da entrada do usuário.
- Funções: Funções críticas como Test-IsElevated, New-SecurePassword e New-LocalUserFromNinja são definidas. O Test-IsElevated verifica se há privilégios administrativos, o New-SecurePassword gera uma senha segura e o New-LocalUserFromNinja encapsula a funcionalidade principal de criação e gerenciamento de usuários.
- Criação e gerenciamento de usuários: O script verifica se o usuário especificado existe, cria um novo usuário se não existir e define propriedades como senha, descrição e expiração da conta. Ele também lida com a adição de usuários a grupos e agenda tarefas para ativar/desativar contas.
Casos de uso em potencial
Imagine um MSP gerenciando a TI de uma empresa com equipe temporária. Eles podem usar esse script para automatizar a criação de contas para novos funcionários, definindo datas específicas de ativação e desativação, economizando tempo e reduzindo erros manuais.
Comparações
Tradicionalmente, o gerenciamento de contas de usuário pode envolver processos manuais ou scripts básicos que não possuem recursos avançados, como geração de senhas seguras ou ativação/desativação programada. Esse script oferece uma abordagem mais sofisticada, segura e eficiente em termos de tempo.
Implicações
O uso desse script aumenta a segurança e a eficiência, mas também ressalta a necessidade de controles de acesso e monitoramento rigorosos, pois a criação automatizada de contas pode ser um possível vetor de acesso não autorizado se não for gerenciada adequadamente.
Recomendações
As práticas recomendadas incluem a revisão regular do script, o uso em conjunto com políticas de auditoria robustas e a garantia de que somente o pessoal autorizado tenha acesso ao script e às suas funcionalidades.
Considerações finais
O NinjaOne, uma plataforma conhecida por simplificar as tarefas de gerenciamento de TI, pode complementar esses scripts fornecendo uma interface centralizada e fácil de usar para gerenciar esses processos automatizados. A incorporação de scripts do PowerShell em um ambiente NinjaOne pode aumentar significativamente a eficiência operacional e a segurança no gerenciamento de TI.