1
+ function þnameþ
2
+ {
3
+ <#
4
+ . SYNOPSIS
5
+ <Insert Synopsis here>.
6
+
7
+ . DESCRIPTION
8
+ <Insert Description here>.
9
+
10
+ . PARAMETER ComputerName
11
+ The computer(s) to connect to.
12
+ Can be an established CimSession, which will then be reused.
13
+
14
+ . PARAMETER Credential
15
+ The credentials to use to connect to remote computer(s) with.
16
+ This parameter is ignored for local queries.
17
+ This parameter is ignored if passing an established Cim Session for ComputerName.
18
+
19
+ . PARAMETER Authentication
20
+ The authentication method to use to when authenticating to remote computer(s).
21
+ Uses the system default settings by default.
22
+ This parameter is ignored for local queries.
23
+ This parameter is ignored if passing an established Cim Session for ComputerName.
24
+
25
+ . PARAMETER EnableException
26
+ This parameters disables user-friendly warnings and enables the throwing of exceptions.
27
+ This is less user friendly, but allows catching exceptions in calling scripts.
28
+
29
+ . EXAMPLE
30
+ PS C:\> þnameþ
31
+
32
+ <insert description for local execution>.
33
+
34
+ . EXAMPLE
35
+ PS C:\> Get-Content servers.txt | þnameþ
36
+
37
+ <insert description for remote execution from file>
38
+
39
+ . EXAMPLE
40
+ PS C:\> Get-ADComputer -Filter "name -like 'Desktop*'" | þnameþ
41
+
42
+ <insert description for remote execution from AD Computer>
43
+ #>
44
+ [CmdletBinding ()]
45
+ Param (
46
+ [Parameter (ValueFromPipeline = $true )]
47
+ [PSFComputer []]
48
+ $ComputerName = $env: COMPUTERNAME ,
49
+
50
+ [System.Management.Automation.CredentialAttribute ()]
51
+ [System.Management.Automation.PSCredential ]
52
+ $Credential ,
53
+
54
+ [Microsoft.Management.Infrastructure.Options.PasswordAuthenticationMechanism ]
55
+ $Authentication = [Microsoft.Management.Infrastructure.Options.PasswordAuthenticationMechanism ]::Default ,
56
+
57
+ [switch ]
58
+ $EnableException
59
+ )
60
+
61
+ begin
62
+ {
63
+ Write-PSFMessage - Level InternalComment - Message " Bound parameters: $ ( $PSBoundParameters.Keys -join " , " ) " - Tag ' debug' , ' start' , ' param'
64
+ }
65
+ process
66
+ {
67
+ # region Process by Computer Name
68
+ foreach ($Computer in $ComputerName )
69
+ {
70
+ # region Remote Connection
71
+ Write-PSFMessage - Level VeryVerbose - Message " [$Computer ] Establishing connection" - Target $Computer - Tag ' connect' , ' start'
72
+ try
73
+ {
74
+ if (-not $Computer.IsLocalhost )
75
+ {
76
+ if ($Computer.Type -like " CimSession" ) { $session = $Computer.InputObject }
77
+ else { $session = New-CimSession - ComputerName $Computer - Credential $Credential - Authentication $Authentication - ErrorAction Stop }
78
+
79
+ # Some dummy code, replace with actual logic
80
+ Write-PSFMessage - Level SomewhatVerbose - Message " [$Computer ] Retrieving OS information" - Target $Computer - Tag ' os' , ' get'
81
+ $operatingSystem = Get-CimInstance - ClassName Win32_OperatingSystem - CimSession $session - ErrorAction Stop
82
+
83
+ if ($Computer.Type -notlike " CimSession" ) { Remove-CimSession - CimSession $session }
84
+ }
85
+ else
86
+ {
87
+ # Some dummy code, replace with actual logic
88
+ # No point in establishing a session to localhost, custom credentials also not supported
89
+ Write-PSFMessage - Level SomewhatVerbose - Message " [$Computer ] Retrieving OS information" - Target $Computer - Tag ' os' , ' get'
90
+ $operatingSystem = Get-CimInstance - ClassName Win32_OperatingSystem - ErrorAction Stop
91
+ }
92
+ }
93
+ catch
94
+ {
95
+ Stop-PSFFunction - Message " [$Computer ] Failed to connect to target computer" - Target $Computer - Tag ' connect' , ' fail' - ErrorRecord $_ - EnableException $EnableException - Continue
96
+ }
97
+ # endregion Remote Connection
98
+
99
+ # Dummy data, replace with actual data object to build
100
+ # region Process Data
101
+ $systemInfo = New-Object Fred.IronScripter2018.SystemInformation - Property @ {
102
+ ComputerName = $Computer.ComputerName
103
+ Name = $operatingSystem.Caption
104
+ Version = $operatingSystem.Version
105
+ ServicePack = " {0}.{1}" -f $operatingSystem.ServicePackMajorVersion , $operatingSystem.ServicePackMinorVersion
106
+ Manufacturer = $operatingSystem.Manufacturer
107
+ WindowsDirectory = $operatingSystem.WindowsDirectory
108
+ Locale = $operatingSystem.Locale
109
+ FreePhysicalMemory = $operatingSystem.FreePhysicalMemory * 1024 # Comes in KB
110
+ VirtualMemory = $operatingSystem.TotalVirtualMemorySize * 1024 # Comes in KB
111
+ FreeVirtualMemory = $operatingSystem.FreeVirtualMemory * 1024 # Comes in KB
112
+ }
113
+
114
+ Write-PSFMessage - Level Verbose - Message " [$Computer ] Finished gathering information" - Target $Computer - Tag ' success' , ' finished'
115
+ $systemInfo
116
+ # endregion Process Data
117
+ }
118
+ # endregion Process by Computer Name
119
+ }
120
+ end
121
+ {
122
+
123
+ }
124
+ }
0 commit comments