pscustomobject

How to convert PSCustomObject with additional props to a custom class

旧城冷巷雨未停 提交于 2021-01-27 20:10:12
问题 Is there a neat way to convert a PSCustomObject to a custom class as a function parameter in PowerShell 5.1? The custom object contains additional properties. I'd like to be able to do something like this: class MyClass { [ValidateNotNullOrEmpty()][string]$PropA } $input = [pscustomobject]@{ PropA = 'propA'; AdditionalProp = 'additionalProp'; } function DuckTypingFtw { [CmdletBinding()] param( [Parameter(Mandatory = $True, Position = 0, ValueFromPipeline)] [MyClass] $myObj ) 'Success!' }

Unable to modify value of a nested property in PS custom object

匆匆过客 提交于 2019-12-23 01:46:16
问题 I have a PS custom object: TourCode : s TournamentNumber : 524 TournamentName : Mitsubishi Electric Championship at Hualalai EnvironmentType : PGA Tour TournamentRootPath : /mitsubishi-electric-championship-at-hualalai SValues : {@{S1=championstour; S2=tournaments; S3=mitsubishi-electric-championship-at; S4=}} I can easily modify values of the top level properties like TourCode or TournamentName: PS C:\Users\yuriy> $testTournament.TourCode = 'r' PS C:\Users\yuriy> $testTournament TourCode : r

Powershell create object from multiple files

僤鯓⒐⒋嵵緔 提交于 2019-12-11 00:50:01
问题 I am making a PSObject from a json file bar.json { "derp": { "buzz": 42 }, "woot": { "toot": 9000 } } I can make a PSCustomObject form the json using ConvertFrom-Json $foo = Get-Content .\bar.json -Raw |ConvertFrom-Json $foo.gettype() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True False PSCustomObject System.Object However if I try and splat multiple json files, I get an array $foo = Get-Content .\*.json -Raw |ConvertFrom-Json $foo.gettype() IsPublic IsSerial Name

Powershell Object not being piped through to Functions

允我心安 提交于 2019-12-02 13:52:22
问题 I have two functions one creates a custom object which once done is piped to the next function. The problem is that the second function is not receiving my object correctly. Instead of using the pipeline I have tried setting a variable and then piping that variable to the function. Below are the two functions with the output of get member on the returned object. All the string param are being processed correctly. But the objects simply wont work. In the begin block of the New-BaseGuest I am

Powershell Object not being piped through to Functions

 ̄綄美尐妖づ 提交于 2019-12-02 06:19:16
I have two functions one creates a custom object which once done is piped to the next function. The problem is that the second function is not receiving my object correctly. Instead of using the pipeline I have tried setting a variable and then piping that variable to the function. Below are the two functions with the output of get member on the returned object. All the string param are being processed correctly. But the objects simply wont work. In the begin block of the New-BaseGuest I am not able to assign the results to the variables. Basically I want to end up with: Get-ServerFromXML

Column ordering when exporting to CSV in PowerShell - controlling the property enumeration order of custom objects created from hashtables

爷,独闯天下 提交于 2019-11-30 18:46:19
I'm writing a script in Powershell that exports all securitygroups and their members from Active Directory . Now I want to format the output of the .csv . The Code: $Groups = Get-ADGroup -Properties * -Filter * -SearchBase "OU=SERVICES,DC=XXXXXX,DC=XXXXX" $Table = @() $Record = @{ "Group Name" = "" "Name" = "" "Username" = "" } Foreach($G In $Groups) { $Arrayofmembers = Get-ADGroupMember -identity $G -recursive | select name,samaccountname Foreach ($Member in $Arrayofmembers) { $Record."Group Name" = $G.Name $Record."Name" = $Member.name $Record."UserName" = $Member.samaccountname $objRecord =

How to initialize an array of custom objects

大兔子大兔子 提交于 2019-11-30 11:44:22
问题 First, as this leads to my question, I'll start by noting that I've worked with XML a fair bit in PowerShell, and like how I can read data from XML files, quickly, into arrays of custom objects. For example, if I had the following XML file: <stuff> <item name="Joe" age="32"> <info>something about him</info> </item> <item name="Sue" age="29"> <info>something about her</info> </item> <item name="Cat" age="12"> <info>something else</info> </item> </stuff> And if I read it in simply, like this:

Column ordering when exporting to CSV in PowerShell - controlling the property enumeration order of custom objects created from hashtables

◇◆丶佛笑我妖孽 提交于 2019-11-30 01:50:41
问题 I'm writing a script in Powershell that exports all securitygroups and their members from Active Directory . Now I want to format the output of the .csv . The Code: $Groups = Get-ADGroup -Properties * -Filter * -SearchBase "OU=SERVICES,DC=XXXXXX,DC=XXXXX" $Table = @() $Record = @{ "Group Name" = "" "Name" = "" "Username" = "" } Foreach($G In $Groups) { $Arrayofmembers = Get-ADGroupMember -identity $G -recursive | select name,samaccountname Foreach ($Member in $Arrayofmembers) { $Record."Group