SysAdmin-Extras.psm1

function Get-DiskInfo {
    <#
    .SYNOPSIS
        Gathers disk information from system.
 
    .DESCRIPTION
        Gathers and displays disk information along with drive letters, disk size allocated and disk usage.
 
    .PARAMETER ComputerName
        Specifies the name(s) of the remote computer(s) to obtain disk information from.
 
    .EXAMPLE
        PS> Get-DiskInfo -ComputerName server1
        Retrieves disk information for the specified server.
 
    .EXAMPLE
        PS> $computername = @('server1', 'server2', 'server3')
        PS> $computername | foreach-object {Get-DiskInfo -computername $_}
        Created a array of systems and pass them thru Get-DiskInfo.
 
   .EXAMPLE
 
         Get-DiskInfo -ComputerName Server01
 
         SystemName Drive Size_GB UsedSpace_GB FreeSpace_GB PercentFree
         ---------- ----- ------- ------------ ------------ -----------
         Server01 C: 79.5 32.6 46.9 59
         Server01 D: 3,899.9 3,511.3 388.6 10
         Server01 E: 69.9 44.3 25.6 37
         Server01 F: 299.9 62.3 237.6 79
         Server01 G: 249.9 37.3 212.6 85
         Server01 J: 1,123.9 877.5 246.4 22
         Server01 K: 1,149.9 1,056.5 93.4 8
.LINK
         Author: Luis Carrillo
         GitHub: https://www.github.com/LuisCarrilloTech
 
    #>


    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            Position = 0)]
        [string[]]$ComputerName
    )


    foreach ($computer in $ComputerName) {
        try {
            Write-Verbose "Gathering disk information for $computer machine. Please wait..."
            $diskInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter 'DriveType=3' -ComputerName $computer -ErrorAction stop | ForEach-Object {
                [PSCustomObject] @{
                    SystemName   = $_.SystemName
                    Drive        = $_.DeviceID
                    Size_GB      = '{0:N1}' -f ($_.Size / 1GB)
                    UsedSpace_GB = '{0:N1}' -f (($_.size / 1GB) - ($_.freespace / 1GB))
                    FreeSpace_GB = '{0:N1}' -f ($_.FreeSpace / 1GB)
                    PercentFree  = '{0:N0}' -f (($_.freespace * 100) / $_.Size)
                }
            }

            $diskInfo
        } catch {
            Write-Error -Message "Error retrieving disk info for $($computer): $($_.Exception.Message)"
        }
    }
}

function Get-SysUptime {
    <#
.SYNOPSIS
Get-SysUptime [-ComputerName] <String[]> []
 
.DESCRIPTION
The Get-SysUptime function retrieves the system uptime of one or more remote computers. It uses the Win32_OperatingSystem class from the CIM repository to get the last boot up time of each computer and calculates the uptime based on the current date and time. The function returns the uptime in months, weeks, days, hours, and minutes.
 
.PARAMETER ComputerName
 -ComputerName <String[]> Specifies the name(s) of the remote computer(s) for which to retrieve the system uptime.
 
.EXAMPLE
Get-SysUptime -ComputerName "Server01" Retrieves the system uptime of the remote computer named "Server01" and displays it in months, weeks, days, hours, and minutes.
 
.EXAMPLE
"Server01", "Server02" | Get-SysUptime Retrieves the system uptime of the remote computers named "Server01" and "Server02" and displays it in months, weeks, days, hours, and minutes.
 
.NOTES
 
To use this function, you need to have administrative privileges on the remote computers.
If there is an error retrieving the uptime of a particular computer, the function throws a terminating error and displays an error message.
 
.LINK
Author: Luis Carrillo
GitHub: https://www.github.com/LuisCarrilloTech
 
 #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            Position = 0)]
        [String[]]$ComputerName
    )

    foreach ($Computer in $ComputerName) {
        try {
            $sys = Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $Computer -ErrorAction Stop
            $time = New-TimeSpan -Start $sys.LastBootUpTime -End (Get-Date)
            [PSCustomObject]@{
                ComputerName = $Computer
                "Months"     = $time.Days / 30.4375
                "Weeks"      = $time.Days / 7
                "Days"       = $time.Days
                "Hours"      = $time.Hours
                "Minutes"    = $time.Minutes
            }
        } catch {

            Write-Error -Message "Error gathering uptime for system $Computer"
            Write-Output $_.Exception.Message
        }
    }
}