Skip to content

Commit

Permalink
FIX PSScriptAnalyzer rule violations
Browse files Browse the repository at this point in the history
Fixed rule violations found by PSScriptAnalyzer 1.17.0
Removed trailing whitespaces in Add-ObjectDetail
Moved some variable initialisations from Begin Block into Process block in Add-PASAccount.
  • Loading branch information
pspete committed Jun 3, 2018
1 parent 3d25850 commit 66d2641
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 77 deletions.
14 changes: 7 additions & 7 deletions psPAS/Functions/Accounts/Add-PASAccount.ps1
Expand Up @@ -246,12 +246,6 @@ None
$baseParameters = @("Safe", "PlatformID", "Address", "AccountName", "Password", "Username",
"DisableAutoMgmt", "DisableAutoMgmtReason", "GroupName", "GroupPlatformID")

#declare empty hashtable to hold "non-base" parameters
$properties = @{}

#declare empty array to hold keys to remove from bound parameters
$keysToRemove = @()

}#begin

PROCESS {
Expand All @@ -278,6 +272,12 @@ None

#Process for required formatting

#declare empty hashtable to hold "non-base" parameters
$properties = @{}

#declare empty array to hold keys to remove from bound parameters
[array]$keysToRemove = @()

#Get "non-base" parameters
$boundParameters.keys | Where-Object {$baseParameters -notcontains $_} | ForEach-Object {

Expand All @@ -302,7 +302,7 @@ None
}

#add the "non-base" parameter key to array
[array]$keysToRemove += $_
$keysToRemove += $_

}

Expand Down
129 changes: 59 additions & 70 deletions psPAS/Private/Add-ObjectDetail.ps1
@@ -1,6 +1,5 @@
function Add-ObjectDetail
{
<#
function Add-ObjectDetail {
<#
.SYNOPSIS
Decorate an object with
- A TypeName
Expand All @@ -10,17 +9,17 @@
Helper function to decorate an object with
- A TypeName
- New properties
- Default parameters
- Default parameters
.PARAMETER InputObject
Object to decorate. Accepts pipeline input.
.PARAMETER TypeName
Typename to insert.
This will show up when you use Get-Member against the resulting object.
.PARAMETER PropertyToAdd
Add these noteproperties.
Format is a hashtable with Key (Property Name) = Value (Property Value).
Example to add a One and Date property:
-PropertyToAdd @{
Expand Down Expand Up @@ -68,82 +67,72 @@
.NOTES
This breaks the 'do one thing' rule from certain perspectives...
The goal is to decorate an object all in one shot
This abstraction simplifies decorating an object, with a slight trade-off in performance. For example:
10,000 objects, add a property and typename:
Add-ObjectDetail: ~4.6 seconds
Add-Member + PSObject.TypeNames.Insert: ~3 seconds
Initial code borrowed from Shay Levy:
http://blogs.microsoft.co.il/scriptfanatic/2012/04/13/custom-objects-default-display-in-powershell-30/
.LINK
http://ramblingcookiemonster.github.io/Decorating-Objects/
.FUNCTIONALITY
PowerShell Language
#>
[CmdletBinding()]
param(
[Parameter( Mandatory = $true,
Position=0,
ValueFromPipeline=$true )]
[ValidateNotNullOrEmpty()]
[psobject[]]$InputObject,
[CmdletBinding()]
param(
[Parameter( Mandatory = $true,
Position = 0,
ValueFromPipeline = $true )]
[ValidateNotNullOrEmpty()]
[psobject[]]$InputObject,

[Parameter( Mandatory = $false,
Position=1)]
[string]$TypeName,
[Parameter( Mandatory = $false,
Position = 1)]
[string]$TypeName,

[Parameter( Mandatory = $false,
Position=2)]
[System.Collections.Hashtable]$PropertyToAdd,
[Parameter( Mandatory = $false,
Position = 2)]
[System.Collections.Hashtable]$PropertyToAdd,

[Parameter( Mandatory = $false,
Position=3)]
[ValidateNotNullOrEmpty()]
[Alias('dp')]
[System.String[]]$DefaultProperties,
[Parameter( Mandatory = $false,
Position = 3)]
[ValidateNotNullOrEmpty()]
[Alias('dp')]
[System.String[]]$DefaultProperties,

[boolean]$Passthru = $True
)

Begin
{
if($PSBoundParameters.ContainsKey('DefaultProperties'))
{
# define a subset of properties
$ddps = New-Object System.Management.Automation.PSPropertySet DefaultDisplayPropertySet,$DefaultProperties
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]$ddps
}
}
Process
{
foreach($Object in $InputObject)
{
switch ($PSBoundParameters.Keys)
{
'PropertyToAdd'
{
foreach($Key in $PropertyToAdd.Keys)
{
#Add some noteproperties. Slightly faster than Add-Member.
$Object.PSObject.Properties.Add( ( New-Object System.Management.Automation.PSNoteProperty($Key, $PropertyToAdd[$Key]) ) )
}
}
'TypeName'
{
#Add specified type
[void]$Object.PSObject.TypeNames.Insert(0,$TypeName)
}
'DefaultProperties'
{
# Attach default display property set
Add-Member -InputObject $Object -MemberType MemberSet -Name PSStandardMembers -Value $PSStandardMembers
}
}
if($Passthru)
{
$Object
}
}
}
[boolean]$Passthru = $True
)

Begin {
if($PSBoundParameters.ContainsKey('DefaultProperties')) {
# define a subset of properties
$ddps = New-Object System.Management.Automation.PSPropertySet DefaultDisplayPropertySet, $DefaultProperties
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]$ddps
}
}
Process {
foreach($Object in $InputObject) {
switch ($PSBoundParameters.Keys) {
'PropertyToAdd' {
foreach($Key in $PropertyToAdd.Keys) {
#Add some noteproperties. Slightly faster than Add-Member.
$Object.PSObject.Properties.Add( ( New-Object System.Management.Automation.PSNoteProperty($Key, $PropertyToAdd[$Key]) ) )
}
}
'TypeName' {
#Add specified type
[void]$Object.PSObject.TypeNames.Insert(0, $TypeName)
}
'DefaultProperties' {
# Attach default display property set
Add-Member -InputObject $Object -MemberType MemberSet -Name PSStandardMembers -Value $PSStandardMembers
}
}
if($Passthru) {
$Object
}
}
}
}

0 comments on commit 66d2641

Please sign in to comment.