Skip to content

Commit 5bd9838

Browse files
author
Friedrich Weinmann
committed
New Templates
1 parent 149e598 commit 5bd9838

File tree

9 files changed

+129
-2
lines changed

9 files changed

+129
-2
lines changed

PSModuleDevelopment/functions/templating/Get-PSMDTemplate.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171
#endregion Apply filters
172172

173173
#region Return valid templates
174-
if ($All) { return $filteredProspects }
174+
if ($All) { return $filteredProspects | Sort-Object Type, Name, Version }
175175

176176
$prospectHash = @{ }
177177
foreach ($prospect in $filteredProspects)
@@ -185,7 +185,7 @@
185185
$prospectHash[$prospect.Name] = $prospect
186186
}
187187
}
188-
$prospectHash.Values
188+
$prospectHash.Values | Sort-Object Type, Name
189189
#endregion Return valid templates
190190
}
191191
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
New-PSMDTemplate -FilePath "$PSScriptRoot\þnameþ.ps1" -TemplateName PSFCimFunction -OutStore psmoduledevelopment -Description "PSFramework: Create function that connects via CIM" -Author "Friedrich Weinmann" -Tags 'function','file'

templates/PSFCimFunction/þnameþ.ps1

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
New-PSMDTemplate -FilePath "$PSScriptRoot\þnameþ.ps1" -TemplateName function -OutStore psmoduledevelopment -Description "Basic function template" -Author "Friedrich Weinmann" -Tags 'function','file'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Set-PSFConfig -Module 'þModuleNameþ' -Name 'Example.Setting' -Value 10 -Initialize -Validation 'integer' -Handler { } -Description "Example configuration setting. Your module can then use the setting using 'Get-PSFConfigValue'"

0 commit comments

Comments
 (0)