functions/Get-DbaComputerSystem.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
function Get-DbaComputerSystem {
    <#
        .SYNOPSIS
            Gets computer system information from the server.
 
        .DESCRIPTION
            Gets computer system information from the server and returns as an object.
 
        .PARAMETER ComputerName
            Target computer(s). If no computer name is specified, the local computer is targeted
 
        .PARAMETER Credential
            Alternate credential object to use for accessing the target computer(s).
         
        .PARAMETER IncludeAws
            If computer is hosted in AWS Infrastructure as a Service (IaaS), additional information will be included.
 
        .PARAMETER Silent
            Use this switch to disable any kind of verbose messages
 
        .NOTES
            Tags: ServerInfo
            Original Author: Shawn Melton (@wsmelton | http://blog.wsmelton.info)
 
            Website: https: //dbatools.io
            Copyright: (C) Chrissy LeMaire, clemaire@gmail.com
            License: GNU GPL v3 https: //opensource.org/licenses/GPL-3.0
 
        .LINK
            https://dbatools.io/Get-DbaComputerSystem
 
        .EXAMPLE
            Get-DbaComputerSystem
 
            Returns information about the local computer's computer system
 
        .EXAMPLE
            Get-DbaComputerSystem -ComputerName sql2016
 
            Returns information about the sql2016's computer system
 
        .EXAMPLE
            Get-DbaComputerSystem -ComputerName sql2016 -IncludeAws
 
            Returns information about the sql2016's computer system and includes additional properties around the EC2 instance.
    #>

    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline = $true)]
        [Alias("cn","host","Server")]
        [DbaInstanceParameter[]]$ComputerName = $env:COMPUTERNAME,
        [PSCredential]$Credential,
        [switch]$IncludeAws,
        [switch]$Silent
    )
    process {
        foreach ($computer in $ComputerName) {
            Write-Message -Level Verbose -Message "Attempting to connect to $computer"
            $server = Resolve-DbaNetworkName -ComputerName $computer.ComputerName -Credential $Credential

            $computerResolved = $server.FullComputerName

            if (!$computerResolved) {
                Write-Message -Level Warning -Message "Unable to resolve hostname of $computer. Skipping."
                continue
            }

            if (Test-Bound "Credential") {
                $computerSystem = Get-DbaCmObject -ClassName Win32_ComputerSystem -ComputerName $computerResolved -Credential $Credential
            }
            else {
                $computerSystem = Get-DbaCmObject -ClassName Win32_ComputerSystem -ComputerName $computerResolved
            }
            
            $adminPasswordStatus = 
                switch ($computerSystem.AdminPasswordStatus) {
                    0 {"Disabled"}
                    1 {"Enabled"}
                    2 {"Not Implemented"}
                    3 {"Unknown"}
                    default {"Unknown"}
                }

            $domainRole = 
                switch ($computerSystem.DomainRole) {
                    0 {"Standalone Workstation"}
                    1 {"Member Workstation"}
                    2 {"Standalone Server"}
                    3 {"Member Server"}
                    4 {"Backup Domain Controller"}
                    5 {"Primary Domain Controller"}
                }
            
            $isHyperThreading = $false
            if ($computerSystem.NumberOfLogicalProcessors -gt $computerSystem.NumberofProcessors) {
                $isHyperThreading = $true
            }

            if ($IncludeAws) {
                $isAws = Invoke-Command2 -ComputerName $computerResolved -Credential $Credential -ScriptBlock { ((Invoke-WebRequest -TimeoutSec 15 -Uri 'http://169.254.169.254').StatusCode) -eq 200} -Raw
                
                if ($isAws) {
                    $scriptBlock = {
                        [PSCustomObject]@{
                            AmiId = (Invoke-WebRequest -Uri 'http://169.254.169.254/latest/meta-data/ami-id').Content
                            IamRoleArn = ((Invoke-WebRequest -Uri 'http://169.254.169.254/latest/meta-data/iam/info').Content | ConvertFrom-Json).InstanceProfileArn
                            InstanceId = (Invoke-WebRequest -Uri 'http://169.254.169.254/latest/meta-data/instance-id').Content
                            InstanceType = (Invoke-WebRequest -Uri 'http://169.254.169.254/latest/meta-data/instance-type').Content
                            AvailabilityZone = (Invoke-WebRequest -Uri 'http://169.254.169.254/latest/meta-data/placement/availability-zone').Content
                            PublicHostname = (Invoke-WebRequest -Uri 'http://169.254.169.254/latest/meta-data/public-hostname').Content
                        }
                    }
                    $awsProps = Invoke-Command2 -ComputerName $computerResolved -Credential $Credential -ScriptBlock $scriptBlock
                }
                else {
                    Write-Message -Level Warning -Message "$computerResolved was not found to be an EC2 instance. Verify http://169.254.169.254 is accessible on the computer."
                }
            }
            $inputObject = [PSCustomObject]@{
                ComputerName            = $computerResolved
                Domain                  = $computerSystem.Domain
                DomainRole              = $domainRole
                Manufacturer            = $computerSystem.Manufacturer
                Model                   = $computerSystem.Model
                SystemFamily            = $computerSystem.SystemFamily
                SystemSkuNumber         = $computerSystem.SystemSKUNumber
                SystemType              = $computerSystem.SystemType
                NumberLogicalProcessors = $computerSystem.NumberOfLogicalProcessors
                NumberProcessors        = $computerSystem.NumberOfProcessors
                IsHyperThreading        = $isHyperThreading
                TotalPhysicalMemory     = [DbaSize]$computerSystem.TotalPhysicalMemory
                IsDaylightSavingsTime   = $computerSystem.EnableDaylightSavingsTime
                DaylightInEffect        = $computerSystem.DaylightInEffect
                DnsHostName             = $computerSystem.DNSHostName
                IsSystemManagedPageFile = $computerSystem.AutomaticManagedPagefile
                AdminPasswordStatus     = $adminPasswordStatus
            }
            if ($IncludeAws -and $isAws) {
                Add-Member -Force -InputObject $inputObject -MemberType NoteProperty -Name AwsAmiId -Value $awsProps.AmiId
                Add-Member -Force -InputObject $inputObject -MemberType NoteProperty -Name AwsIamRoleArn -Value $awsProps.IamRoleArn
                Add-Member -Force -InputObject $inputObject -MemberType NoteProperty -Name AwsEc2InstanceId -Value $awsProps.InstanceId
                Add-Member -Force -InputObject $inputObject -MemberType NoteProperty -Name AwsEc2InstanceType -Value $awsProps.InstanceType
                Add-Member -Force -InputObject $inputObject -MemberType NoteProperty -Name AwsAvailabilityZone -Value $awsProps.AvailabilityZone
                Add-Member -Force -InputObject $inputObject -MemberType NoteProperty -Name AwsPublicHostName -Value $awsProps.PublicHostname
            }
            $excludes = 'SystemSkuNumber','IsDaylightSavingsTime','DaylightInEffect','DnsHostName','AdminPasswordStatus'
            Select-DefaultView -InputObject $inputObject -ExcludeProperty $excludes
        }
    }
}