-
Notifications
You must be signed in to change notification settings - Fork 385
Convert Microsoft.IIs.PowerShell.Framework.* objects to PSCustom #2510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+221
−27
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
...cs/HealthChecker/DataCollection/ExchangeInformation/IISInformation/ConvertTo-PSObject.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| . $PSScriptRoot\..\..\..\..\..\Shared\ScriptBlockFunctions\RemotePipelineHandlerFunctions.ps1 | ||
|
|
||
| <# | ||
| .DESCRIPTION | ||
| This function converts specific .NET or PowerShell objects into a custom PSCustomObject instance. | ||
| By copying selected properties onto a simple PSCustomObject, it helps avoid serialization | ||
| problems that can occur when remoting or persisting complex framework types. | ||
| #> | ||
| function ConvertTo-PSObject { | ||
| [CmdletBinding()] | ||
| param( | ||
| [Parameter(Mandatory = $true)] | ||
| [object]$ObjectToConvert, | ||
|
|
||
| [Parameter(Mandatory = $true)] | ||
| [string[]]$ObjectTypeToConvert, | ||
|
|
||
| [string[]]$PropertiesToSkip, | ||
|
|
||
| [int]$DefaultDepthValue = 5 | ||
| ) | ||
|
|
||
| begin { | ||
| Write-Verbose "Calling: $($MyInvocation.MyCommand)" | ||
| if ((Get-PSCallStack)[1].Command -ne "ConvertTo-PSObject") { | ||
| $Script:ConvertToPSObjectDepth = 0 | ||
| } | ||
dpaulson45 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $newPSObject = New-Object PSCustomObject | ||
| } | ||
| process { | ||
| # The properties on the object that we want to pull out and place on a custom PS Object. | ||
| # This should help with serialization issues. | ||
| $objectGetTypeFullName = $ObjectToConvert.GetType().FullName | ||
| $doNotConvert = $true | ||
|
|
||
| foreach ($type in $ObjectTypeToConvert) { | ||
| # This is intended to handle both ways that the wildcard can be setup and used. | ||
| if ($type -like $objectGetTypeFullName -or | ||
| $objectGetTypeFullName -like $type) { | ||
| $doNotConvert = $false | ||
| } | ||
| } | ||
|
|
||
| # $Pester should only be set when we are doing Pester testing. | ||
| if ($doNotConvert -and | ||
dpaulson45 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $Script:Pester -eq $false) { | ||
| Write-Verbose "Object '$($ObjectToConvert.GetType().FullName)' is not one of these types to convert: $([string]::Join(", ", $ObjectTypeToConvert))." | ||
| return | ||
| } | ||
|
|
||
dpaulson45 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $properties = ($ObjectToConvert | | ||
| Get-Member | | ||
| Where-Object { | ||
| $_.MemberType -ne "Method" -and | ||
| $_.MemberType -ne "ParameterizedProperty" -and | ||
| $_.MemberType -ne "CodeMethod" -and | ||
| $_.MemberType -ne "ScriptMethod" | ||
dpaulson45 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }).Name | | ||
dpaulson45 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Where-Object { $PropertiesToSkip -notcontains $_ } | ||
| Write-Verbose "Going to include the following properties to the object: $([string]::Join(", ", $properties))" | ||
|
|
||
| foreach ($prop in $properties) { | ||
| # If the property is an array, we need to handle this differently. | ||
| # If the property is one of the types, Call this function again. Otherwise, add it as is. | ||
| if ($ObjectToConvert.$prop -is [array]) { | ||
| Write-Verbose "$prop is an Array on this object." | ||
| $list = New-Object System.Collections.Generic.List[object] | ||
|
|
||
| foreach ($entry in $ObjectToConvert.$prop) { | ||
| if ($null -ne $entry) { | ||
| # Make this easier by just calling the method again, just need to add the type object that it is, just in case we are dealing with a list of a different type. | ||
| $Script:ConvertToPSObjectDepth++ | ||
| $value = $null | ||
| $params = @{ | ||
| ObjectToConvert = $entry | ||
| ObjectTypeToConvert = $ObjectTypeToConvert + ($entry.GetType().FullName) | ||
| PropertiesToSkip = $PropertiesToSkip | ||
| DefaultDepthValue = $DefaultDepthValue | ||
| } | ||
| ConvertTo-PSObject @params | Invoke-RemotePipelineHandler -Result ([ref]$value) | ||
| $list.Add($value) | ||
dpaulson45 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else { | ||
| # Add the empty list. | ||
| $list.Add($entry) | ||
| } | ||
| } | ||
|
|
||
| $newPSObject | Add-Member -MemberType NoteProperty -Name $prop -Value $list | ||
| } elseif ($null -ne $ObjectToConvert.$prop -and | ||
| [string]::Empty -ne $ObjectToConvert.$prop -and | ||
dpaulson45 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $null -ne ($ObjectTypeToConvert | Where-Object { $ObjectToConvert.$prop.GetType().FullName -like $_ })) { | ||
|
|
||
dpaulson45 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if ($Script:ConvertToPSObjectDepth -gt $DefaultDepthValue) { | ||
| Write-Verbose "Unable to convert this attribute property, as we are too deep in the object." | ||
| $newPSObject | Add-Member -MemberType NoteProperty -Name $prop -Value "--ERROR TOO DEEP--" | ||
| } else { | ||
| Write-Verbose "Going to call ConvertTo-PSObject for $prop property to expand" | ||
| $Script:ConvertToPSObjectDepth++ | ||
| $value = $null | ||
| $params = @{ | ||
| ObjectToConvert = ($ObjectToConvert.$prop) | ||
| ObjectTypeToConvert = $ObjectTypeToConvert | ||
| PropertiesToSkip = $PropertiesToSkip | ||
| DefaultDepthValue = $DefaultDepthValue | ||
| } | ||
| ConvertTo-PSObject @params | Invoke-RemotePipelineHandler -Result ([ref]$value) | ||
| $newPSObject | Add-Member -MemberType NoteProperty -Name $prop -Value $value | ||
| } | ||
| } else { | ||
| Write-Verbose "Adding property: $prop" | ||
| $newPSObject | Add-Member -MemberType NoteProperty -Name $prop -Value ($ObjectToConvert.$prop) | ||
dpaulson45 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
dpaulson45 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| end { | ||
| $Script:ConvertToPSObjectDepth-- | ||
dpaulson45 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return $newPSObject | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.