diff --git a/src/functions/public/Get-PSModuleTest.ps1 b/src/functions/public/Get-PSModuleTest.ps1 deleted file mode 100644 index 0e9aacf..0000000 --- a/src/functions/public/Get-PSModuleTest.ps1 +++ /dev/null @@ -1,20 +0,0 @@ -#Requires -Modules Utilities - -function Get-PSModuleTest { - <# - .SYNOPSIS - Performs tests on a module. - - .EXAMPLE - Test-PSModule -Name 'World' - - "Hello, World!" - #> - [CmdletBinding()] - param ( - # Name of the person to greet. - [Parameter(Mandatory)] - [string] $Name - ) - Write-Output "Hello, $Name!" -} diff --git a/src/functions/public/Remove-Member.ps1 b/src/functions/public/Remove-Member.ps1 new file mode 100644 index 0000000..39155b7 --- /dev/null +++ b/src/functions/public/Remove-Member.ps1 @@ -0,0 +1,67 @@ +function Remove-Member { + <# + .SYNOPSIS + Removes specified members (properties) from an object. + + .DESCRIPTION + The Remove-Member function removes specified members (properties) from objects passed through the pipeline. + If the specified properties exist, they are removed; otherwise, a verbose message is displayed. + When the -PassThru switch is used, the modified object is returned. + + .EXAMPLE + $obj = [PSCustomObject]@{Name="John"; Age=30; City="New York"} + $obj | Remove-Member -Name "Age" -PassThru + + Output: + ```powershell + Name City + ---- ---- + John New York + ``` + + Removes the "Age" property from the PSCustomObject and returns the modified object. + + .OUTPUTS + PSCustomObject + + .NOTES + The modified object without the specified properties, if -PassThru is used. + + .LINK + https://psmodule.io/Object/Functions/Remove-Member + #> + [Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSUseShouldProcessForStateChangingFunctions', '', + Justification = 'Sets text in memory' + )] + [CmdletBinding()] + param( + # Accept input from the pipeline. + [Parameter(Mandatory, ValueFromPipeline)] + [System.Object] $InputObject, + + # Specify one or more member names to remove. + [Parameter(Mandatory)] + [string[]] $Name, + + # When specified, output the modified object. + [Parameter()] + [switch] $PassThru + ) + + process { + foreach ($obj in $InputObject) { + foreach ($member in $Name) { + if ($obj.PSObject.Properties[$member]) { + $null = $obj.PSObject.Properties.Remove($member) + } else { + Write-Verbose "Property '$member' not found on object of type $($obj.GetType().FullName)." + } + } + if ($PassThru) { + # Only output the object if -PassThru is used. + $obj + } + } + } +} diff --git a/tests/Object.Tests.ps1 b/tests/Object.Tests.ps1 new file mode 100644 index 0000000..ba1972d --- /dev/null +++ b/tests/Object.Tests.ps1 @@ -0,0 +1,34 @@ +[Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSReviewUnusedParameter', '', + Justification = 'Required for Pester tests' +)] +[Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSUseDeclaredVarsMoreThanAssignments', '', + Justification = 'Required for Pester tests' +)] +[CmdletBinding()] +param() + +Describe 'Object' { + Context 'Function: Remove-Member' { + It 'Removes a member from an object' { + $object = [pscustomobject]@{ Name = 'Test'; Value = 'Value' } + $object | Remove-Member -Name Value + $object.PSObject.Properties | Should -Not -Contain 'Value' + } + + It 'Removes multiple members from an object' { + $object = [pscustomobject]@{ Name = 'Test'; Value = 'Value'; Other = 'Other' } + $object | Remove-Member -Name Value, Other + $object.PSObject.Properties | Should -Not -Contain 'Value' + $object.PSObject.Properties | Should -Not -Contain 'Other' + } + + It 'Removes properties of classes' { + $object = [System.Net.WebClient]::new() + $object | Add-Member -MemberType NoteProperty -Name Something -Value 'http://example.com' + $object | Remove-Member -Name BaseAddress + $object.PSObject.Properties | Should -Not -Contain 'BaseAddress' + } + } +} diff --git a/tests/PSModuleTest.Tests.ps1 b/tests/PSModuleTest.Tests.ps1 deleted file mode 100644 index 8258bb7..0000000 --- a/tests/PSModuleTest.Tests.ps1 +++ /dev/null @@ -1,16 +0,0 @@ -[Diagnostics.CodeAnalysis.SuppressMessageAttribute( - 'PSReviewUnusedParameter', '', - Justification = 'Required for Pester tests' -)] -[Diagnostics.CodeAnalysis.SuppressMessageAttribute( - 'PSUseDeclaredVarsMoreThanAssignments', '', - Justification = 'Required for Pester tests' -)] -[CmdletBinding()] -param() - -Describe 'Module' { - It 'Function: Get-PSModuleTest' { - Get-PSModuleTest -Name 'World' | Should -Be 'Hello, World!' - } -}