function ConvertTo-GraylogSession { <# .SYNOPSIS Converts a JSON string to a WebRequestSession object. .DESCRIPTION Converts a JSON string to a WebRequestSession object. .PARAMETER InputObject The JSON string to convert to a WebRequestSession object. .OUTPUTS A WebRequestSession object containing the properties from the JSON string. .EXAMPLE ConvertTo-Session -InputObject $Json Converts the JSON string to a WebRequestSession object. .EXAMPLE $Json | ConvertTo-Session Converts the JSON string to a WebRequestSession object. .NOTES This function is used to convert a JSON string to a WebRequestSession object. #> param ( [Parameter(Mandatory, ValueFromPipeline)] [string] $InputObject ) $Object = ConvertFrom-Json $InputObject $Session = [Microsoft.PowerShell.Commands.WebRequestSession]::new() $Object.Headers.PSObject.Properties | ForEach-Object { $Session.Headers.Add($_.Name, $_.Value) } $Session.UseDefaultCredentials = $Object.UseDefaultCredentials $Session.Credentials = $Object.Credentials $Session.Certificates = $Object.Certificates $Session.UserAgent = $Object.UserAgent $Session.Proxy = $Object.Proxy $Session.MaximumRedirection = $Object.MaximumRedirection try { $Session.MaximumRetryCount = $Object.MaximumRetryCount } catch {} # MaximumRetryCount is only available in PowerShell Core try { $Session.RetryIntervalInSeconds = $Object.RetryIntervalInSeconds } catch {} # RetryIntervalInSeconds is only available in PowerShell Core if ($Object.Cookies) { $Object.Cookies | ForEach-Object { $Cookie = [Net.Cookie]::new() $Cookie.Name = $_.Name $Cookie.Value = $_.Value $Cookie.Domain = $_.Domain $Cookie.Path = $_.Path $Cookie.Expires = $_.Expires $Cookie.Secure = $_.Secure $Cookie.HttpOnly = $_.HttpOnly $Session.Cookies.Add($Cookie) } } return $Session }