Public/Get-PCInfo.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
function Get-PCInfo {

    <#
    .SYNOPSIS
        Generates a summary of CPU, RAM, Disk and other metrics for one or more computers
 
    .DESCRIPTION
        Generates a summary of CPU, RAM, Disk and other metrics for a computer via CIM and Active Directory.
        Code supports querying data from multiple computers. Also, data from this cmdlet can be viewed with other views
         like Out-Gridview or exported with Export-CSV or Export-Excel.
 
    .PARAMETER ComputerName
        Then name of a computer to query. Valid aliases are Name, Computer, & PC.
 
    .EXAMPLE
        PS C:\> Get-PCInfo
 
        Name : PRDUS2ITUTIL01
        Domain : aligntech.com
        Description :
        OS : Microsoft Windows Server 2019 Standard
        ServicePack : 0
        WindowsSN : 00429-70000-00000-AA946
        Model : VMware Virtual Platform
        MACAddress : 00:50:56:8A:CB:1D
        IPAddress : 10.16.157.68
        CPUInfo : Intel(R) Xeon(R) CPU E5-2690 v3 @ 2.60GHz, Number of Processors: 2
        RAM : 8 GB
        DriveInfo : {@{Name=C:\; DriveType=FixedDisk; TotalSize=99 GB; UsedSpace=31 GB; FreeSpace=68 GB;
                        PercentUsed=31 %; Filesystem=NTFS; IsBootVolume=True; CompressedVolume=False;
                        ContainsPageFile=True}, @{Name=D:\; DriveType=CD-Rom; TotalSize=; UsedSpace=; FreeSpace=;
                        PercentUsed=; Filesystem=; IsBootVolume=; CompressedVolume=; ContainsPageFile=}}
        LastBootTime : 4/7/2020 10:38:44 AM
        UpTime : 30 Days, 2 Hours, 58 Minutes
 
        Returns info for local PC
 
    .EXAMPLE
 
    PS C:\> Get-PCInfo -computername PRSCEFTP01 -OutVariable FOO
 
        Name : PRSCEFTP01
        Domain : aligntech.com
        Description : eFTP.
        OS : Microsoft Windows Server 2008 R2 Standard
        ServicePack : 1
        WindowsSN : 00477-001-0000421-84672
        Model : VMware Virtual Platform
        MACAddress : 00:50:56:8A:66:A2
        IPAddress : {10.16.243.198, 10.16.243.197, 10.16.243.196}
        CPUInfo : Intel(R) Xeon(R) CPU E5-2690 v3 @ 2.60GHz, Number of Processors: 4
        RAM : 32 GB
        DriveInfo : {@{Name=E:\; DriveType=FixedDisk; TotalSize=137 GB; UsedSpace=100 GB; FreeSpace=37 GB;
                         PercentUsed=73 %; Filesystem=NTFS; IsBootVolume=False; CompressedVolume=False;
                         ContainsPageFile=False}, @{Name=F:\; DriveType=FixedDisk; TotalSize=410 GB; UsedSpace=158 GB;
                         FreeSpace=252 GB; PercentUsed=39 %; Filesystem=NTFS; IsBootVolume=False;
                         CompressedVolume=False; ContainsPageFile=False}, @{Name=C:\; DriveType=FixedDisk;
                         TotalSize=160 GB; UsedSpace=131 GB; FreeSpace=29 GB; PercentUsed=82 %; Filesystem=NTFS;
                         IsBootVolume=True; CompressedVolume=False; ContainsPageFile=True}, @{Name=D:\;
                         DriveType=CD-Rom; TotalSize=; UsedSpace=; FreeSpace=; PercentUsed=; Filesystem=;
                         IsBootVolume=; CompressedVolume=; ContainsPageFile=}}
        LastBootTime : 4/20/2020 12:56:58 PM
        UpTime : 17 Days, 2 Hours, 0 Minutes
 
 
    PS C:\> $foo.driveinfo | format-table
 
        Name DriveType TotalSize UsedSpace FreeSpace PercentUsed Filesystem IsBootVolume CompressedVolume ContainsPageFile
        ---- --------- --------- --------- --------- ----------- ---------- ------------ ---------------- ----------------
        E:\ FixedDisk 137 GB 100 GB 37 GB 73 % NTFS False False False
        F:\ FixedDisk 410 GB 158 GB 252 GB 39 % NTFS False False False
        C:\ FixedDisk 160 GB 131 GB 29 GB 82 % NTFS True False True
        D:\ CD-Rom
 
        This is a multi-part example that shows how to see the drive info in detail.
 
        The goal is to see the drive info but it is stored in a sub-array that doesn't format well in the initial output
        view. To get around this problem, the first query is run using Out-Variable FOO, which saves the returned data
        to a variable named FOO.
 
        Once the dat is saved to $FOO, a second query can be run by typing the variable name $FOO and the accessing
        the sub property called DriveInfo. This produced a formatted output of the drive information contained in the
        sub array
 
 
    .EXAMPLE
 
        Get-PCInfo -computername PRSCEFTP01, PRDUS4DC01
 
        Name : PRSCEFTP01
        Domain : aligntech.com
        Description : eFTP.
        OS : Microsoft Windows Server 2008 R2 Standard
        ServicePack : 1
        WindowsSN : 00477-001-0000421-84672
        Model : VMware Virtual Platform
        MACAddress : 00:50:56:8A:66:A2
        IPAddress : {10.16.243.198, 10.16.243.197, 10.16.243.196}
        CPUInfo : Intel(R) Xeon(R) CPU E5-2690 v3 @ 2.60GHz, Number of Processors: 4
        RAM : 32 GB
        DriveInfo : {@{Name=E:\; DriveType=FixedDisk; TotalSize=137 GB; UsedSpace=100 GB; FreeSpace=37 GB;
                       PercentUsed=73 %; Filesystem=NTFS; IsBootVolume=False; CompressedVolume=False;
                       ContainsPageFile=False}, @{Name=F:\; DriveType=FixedDisk; TotalSize=410 GB; UsedSpace=158 GB;
                       FreeSpace=252 GB; PercentUsed=39 %; Filesystem=NTFS; IsBootVolume=False; CompressedVolume=False;
                       ContainsPageFile=False}, @{Name=C:\; DriveType=FixedDisk; TotalSize=160 GB; UsedSpace=131 GB;
                       FreeSpace=29 GB; PercentUsed=82 %; Filesystem=NTFS; IsBootVolume=True; CompressedVolume=False;
                       ContainsPageFile=True}, @{Name=D:\; DriveType=CD-Rom; TotalSize=; UsedSpace=; FreeSpace=;
                       PercentUsed=; Filesystem=; IsBootVolume=; CompressedVolume=; ContainsPageFile=}}
        LastBootTime : 4/20/2020 12:56:58 PM
        UpTime : 17 Days, 1 Hours, 54 Minutes
 
        Name : PRDUS4DC01
        Domain : aligntech.com
        Description :
        OS : Microsoft Windows Server 2019 Standard
        ServicePack : 0
        WindowsSN : 00429-70000-00000-AA847
        Model : VMware7,1
        MACAddress : 00:50:56:8A:76:63
        IPAddress : {10.110.10.10, fe80::ed82:56b9:4c79:bf17}
        CPUInfo : Intel(R) Xeon(R) CPU E5-2697 v4 @ 2.30GHz, Number of Processors: 1
        RAM : 8 GB
        DriveInfo : {@{Name=C:\; DriveType=FixedDisk; TotalSize=59 GB; UsedSpace=23 GB; FreeSpace=36 GB;
                       PercentUsed=40 %; Filesystem=NTFS; IsBootVolume=True;
                       CompressedVolume=False; ContainsPageFile=True}, @{Name=D:\; DriveType=CD-Rom; TotalSize=;
                       UsedSpace=; FreeSpace=; PercentUsed=; Filesystem=; IsBootVolume=; CompressedVolume=;
                       ContainsPageFile=}}
        LastBootTime : 5/4/2020 9:27:23 PM
        UpTime : 2 Days, 17 Hours, 24 Minutes
 
        Returns info for two remote computers named PRSCEFTP01 & PRDUS4DC01
 
    .INPUTS
        ComputerName
        Accepts input from pipeline
 
    .OUTPUTS
        Output (if any)
 
    .NOTES
        NAME: Get-PCInfo.ps1
        AUTHOR: Mike Kanakos
        Date Created: 2020-04-29
    #>


    [CmdletBinding()]
    param (
        [Alias("Name","PC","Computer")]
        [Parameter(ValueFromPipeline = $true)]
        [string[]]$ComputerName=$env:COMPUTERNAME
    )

    process {
        Foreach ($PC in $ComputerName){
            Write-Verbose "Testing that $PC is online"
            $online = Test-Connection -ComputerName $PC -Count 2 -Quiet
                if ($online -eq $true){
                    # Get Machine Info
                    $ComputerInfo = Get-CimInstance Win32_ComputerSystem -ComputerName $PC
                    $OSInfo = Get-CimInstance Win32_OperatingSystem -ComputerName $PC
                    $BIOSInfo = Get-CimInstance Win32_BIOS -ComputerName $PC
                    $CPUInfo = Get-CimInstance Win32_Processor -ComputerName $PC
                    $CPUName = $CPUInfo | select-object -ExpandProperty Name -Unique
                    $CPUCount = $CPUInfo.count
                    $DriveInfo = Get-DriveInfo -computername $PC
                    $LastBootTime = $OSInfo.LastBootUpTime
                    $Uptime = (New-TimeSpan -start $lastBoottime -end (Get-Date))
                    $Days = $uptime.days
                    $Minutes = $uptime.Minutes
                    $Hours = $uptime.hours
                    $MacAddress = (get-ciminstance win32_networkadapterConfiguration -Filter "ipenabled='True'" -ComputerName $PC | Select-Object macaddress).Macaddress
                    $IPAddress = Get-CimInstance win32_networkadapterconfiguration -ComputerName $PC | Where-Object {$_.IPAddress -ne $null} | Select-Object -ExpandProperty IPAddress

                    $Info = [PSCustomObject]@{
                        Name = $ComputerInfo.Name
                        Domain = $ComputerInfo.Domain
                        Description = (Get-ADComputer $PC -Properties Description | Select-Object  Description -ExpandProperty description)
                        OS = $OSInfo.Caption
                        ServicePack = $OSInfo.servicepackmajorversion
                        WindowsSN = $OSInfo.SerialNumber
                        Model = $ComputerInfo.Model
                        MACAddress = $MacAddress
                        IPAddress = $IPAddress
                        CPUInfo = "$CPUName, Number of Processors: $CPUCount"
                        RAM = "$([math]::round($ComputerInfo.TotalPhysicalMemory/1gb)) GB"
                        DriveInfo = $DriveInfo
                        LastBootTime = $LastBootTime
                        UpTime = "$Days Days, $hours Hours, $Minutes Minutes"
                    } #end Info object creation

                    $Info
                } #end if

                else {
                     Write-Warning "The computer $(($computer).ToUpper()) could not be contacted"
                    }
        }  #end foreach

    }#end process block

} #end