This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
14
Convert-ObjectToString
Post Body
Hi,
I wanted my logging function to be able to accept objects, so I can easily save the information to a file. The problem was that I often saved the string PsObject or Collection[] instead of the actual values. So here is my helper function that converts any object's property name and value into a very long string. Not sure if this is of actual use, but it was fun playing around with it.
Let me know what you think.
Function Convert-ObjectToString {
PARAM
(
[parameter()]
[psobject]$InputObject
)
$PropertyNames = $InputObject.psobject.properties.name
foreach($PropertyName in $PropertyNames)
{
#if the property's value is $null, continue with the next property
if(-not $InputObject.$($PropertyName)){Continue}
#if the property's value is an object (the inner object)
if($InputObject.$($PropertyName).gettype().name -eq "Object[]")
{
#append the property's name
$NewString = "['{0}'=" -f ($PropertyName)
#and call Convert-ObjectToString recusrively for each inner object.
$NewString = Foreach($InnerObject in $($InputObject.$($PropertyName)))
{
Write-Output "{"
Convert-ObjectToString -InputObject $InnerObject
Write-Output "}"
}
$NewString = "']"
}
#if the property's value is anything else (string, int32, collection ...)
else
{
#append the property's name and value. If the value is a collection, all items will be joined to a single string
$NewString = "['{0}'='{1}'] " -f ($PropertyName, ($InputObject.$($PropertyName) -join ', '))
}
}
Write-Output $NewString
}
Convert-ObjectToString (Get-Service WinRM)
['Name'='WinRM'] ['RequiredServices'='RPCSS, HTTP'] ['CanShutdown'='True'] ['CanStop'='True'] ['DisplayName'='Windows Remote Management (WS-Management)'] ['MachineName'='.'] ['ServiceName'='WinRM']
['ServicesDependedOn'='RPCSS, HTTP'] ['ServiceHandle'='SafeServiceHandle'] ['Status'='Running'] ['ServiceType'='Win32ShareProcess'] ['StartType'='Automatic']
Convert-ObjectToString (Get-Date)
['DisplayHint'='DateTime'] ['DateTime'='Samstag, 20. Mai 2017 21:12:18'] ['Date'='05/20/2017 00:00:00'] ['Day'='20'] ['DayOfWeek'='Saturday'] ['DayOfYear'='140'] ['Hour'='21'] ['Kind'='Local'] ['Mi
llisecond'='791'] ['Minute'='12'] ['Month'='5'] ['Second'='18'] ['Ticks'='636309115387918364'] ['TimeOfDay'='21:12:18.7918364'] ['Year'='2017']
Author
Account Strength
90%
Account Age
15 years
Verified Email
Yes
Verified Flair
No
Total Karma
4,545
Link Karma
3,347
Comment Karma
1,198
Profile updated: 2 days ago
Posts updated: 10 months ago
Subreddit
Post Details
We try to extract some basic information from the post title. This is not
always successful or accurate, please use your best judgement and compare
these values to the post title and body for confirmation.
- Posted
- 7 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/PowerShell/...