PowerShell Administrative Toolkit

# PowerShell Administrative Toolkit

# Function to get system information
function Get-SystemInfo {
    $systemInfo = @{
        "OS" = (Get-CimInstance -ClassName Win32_OperatingSystem).Caption
        "Architecture" = (Get-CimInstance -ClassName Win32_OperatingSystem).OSArchitecture
        "CPU" = (Get-CimInstance -ClassName Win32_Processor).Name
        "Memory" = [math]::round((Get-CimInstance -ClassName Win32_ComputerSystem).TotalPhysicalMemory / 1GB, 2)
    }
    $systemInfo | Format-Table -AutoSize
}

# Function to create a new user
function New-User {
    param (
        [string]$Username,
        [string]$Password,
        [string]$FullName
    )
    $securePassword = ConvertTo-SecureString $Password -AsPlainText -Force
    New-LocalUser -Name $Username -Password $securePassword -FullName $FullName -Description "Created by PowerShell Toolkit"
    Add-LocalGroupMember -Group "Administrators" -Member $Username
    Write-Host "User $Username created and added to Administrators group."
}

# Function to remove a user
function Remove-User {
    param (
        [string]$Username
    )
    Remove-LocalUser -Name $Username -Force
    Write-Host "User $Username removed."
}

# Function to back up files
function Backup-Files {
    param (
        [string]$SourcePath,
        [string]$DestinationPath
    )
    Copy-Item -Path $SourcePath -Destination $DestinationPath -Recurse -Force
    Write-Host "Files from $SourcePath backed up to $DestinationPath."
}

# Function to clean old files
function Clean-OldFiles {
    param (
        [string]$Path,
        [int]$Days
    )
    $cutoffDate = (Get-Date).AddDays(-$Days)
    Get-ChildItem -Path $Path -File | Where-Object { $_.LastWriteTime -lt $cutoffDate } | Remove-Item -Force
    Write-Host "Files older than $Days days have been removed from $Path."
}

# Function to start a service
function Start-Service {
    param (
        [string]$ServiceName
    )
    Start-Service -Name $ServiceName
    Write-Host "Service $ServiceName started."
}

# Function to stop a service
function Stop-Service {
    param (
        [string]$ServiceName
    )
    Stop-Service -Name $ServiceName
    Write-Host "Service $ServiceName stopped."
}

# Function to get service status
function Get-ServiceStatus {
    param (
        [string]$ServiceName
    )
    Get-Service -Name $ServiceName | Select-Object Name, Status
}

# Function to get network information
function Get-NetworkInfo {
    Get-NetIPAddress | Select-Object InterfaceAlias, IPAddress, AddressFamily | Format-Table -AutoSize
}

# Function to set IP configuration
function Set-IPConfiguration {
    param (
        [string]$InterfaceAlias,
        [string]$IPAddress,
        [string]$SubnetMask,
        [string]$DefaultGateway
    )
    New-NetIPAddress -InterfaceAlias $InterfaceAlias -IPAddress $IPAddress -PrefixLength $SubnetMask -DefaultGateway $DefaultGateway
    Write-Host "IP configuration set for $InterfaceAlias."
}

# Function to set file permissions
function Set-FilePermissions {
    param (
        [string]$FilePath,
        [string]$User,
        [string]$AccessRule
    )
    $acl = Get-Acl -Path $FilePath
    $rule = New-Object System.Security.AccessControl.FileSystemAccessRule($User, $AccessRule, "Allow")
    $acl.AddAccessRule($rule)
    Set-Acl -Path $FilePath -AclObject $acl
    Write-Host "Permissions set for $FilePath."
}

# Function to display the menu and handle user choices
function Show-Menu {
    Clear-Host
    Write-Host "PowerShell Administrative Toolkit"
    Write-Host "1: Get System Information"
    Write-Host "2: Create New User"
    Write-Host "3: Remove User"
    Write-Host "4: Backup Files"
    Write-Host "5: Clean Old Files"
    Write-Host "6: Start Service"
    Write-Host "7: Stop Service"
    Write-Host "8: Get Service Status"
    Write-Host "9: Get Network Information"
    Write-Host "10: Set IP Configuration"
    Write-Host "11: Set File Permissions"
    Write-Host "Q: Quit"
    $choice = Read-Host "Enter your choice"
    
    switch ($choice) {
        "1" { Get-SystemInfo }
        "2" {
            $username = Read-Host "Enter username"
            $password = Read-Host "Enter password" -AsSecureString
            $fullname = Read-Host "Enter full name"
            New-User -Username $username -Password $password -FullName $fullname
        }
        "3" {
            $username = Read-Host "Enter username"
            Remove-User -Username $username
        }
        "4" {
            $source = Read-Host "Enter source path"
            $destination = Read-Host "Enter destination path"
            Backup-Files -SourcePath $source -DestinationPath $destination
        }
        "5" {
            $path = Read-Host "Enter path"
            $days = Read-Host "Enter number of days"
            Clean-OldFiles -Path $path -Days $days
        }
        "6" {
            $service = Read-Host "Enter service name"
            Start-Service -ServiceName $service
        }
        "7" {
            $service = Read-Host "Enter service name"
            Stop-Service -ServiceName $service
        }
        "8" {
            $service = Read-Host "Enter service name"
            Get-ServiceStatus -ServiceName $service
        }
        "9" { Get-NetworkInfo }
        "10" {
            $interface = Read-Host "Enter interface alias"
            $ip = Read-Host "Enter IP address"
            $subnet = Read-Host "Enter subnet mask"
            $gateway = Read-Host "Enter default gateway"
            Set-IPConfiguration -InterfaceAlias $interface -IPAddress $ip -SubnetMask $subnet -DefaultGateway $gateway
        }
        "11" {
            $file = Read-Host "Enter file path"
            $user = Read-Host "Enter user"
            $rule = Read-Host "Enter access rule"
            Set-FilePermissions -FilePath $file -User $user -AccessRule $rule
        }
        "Q" { exit }
        default { Write-Host "Invalid choice, please try again." }
    }
}

# Main loop to display the menu and process user choices
while ($true) {
    Show-Menu
}
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *