Mark Minasi's Tech Forum
Sign up Calendar Latest Topics
 
 
 


Reply
  Author   Comment  
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 93
Reply with quote  #1 
From the previous post, "Searching Array for Multiple Values", I realized I should be using PS Custom Objects. 

In a script,  I am using read-host to prompt for $firstname, $lastname, $department, $location, $city, $state.

How can I create a PS custom object given the following responses:


$firstname = Robert
$lastname = Johnson
$department = Accounting
$location = 2nd Floor
$city = Los Angeles
$state = CA

0
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 93
Reply with quote  #2 
The headers would be:

FNAME, LNAME, DEPT, LOCATION, CITY, STATE

0
cj_berlin

Avatar / Picture

Senior Member
Registered:
Posts: 371
Reply with quote  #3 
Hi,

the easiest way to create a custom object (with properties containing some values) is from a hashtable:

$obj = [PSCustomObject]@{
  'FNAME' = 'Robert'; 
  'LNAME' = 'Johnson';
  'DEPT' = 'Accounting';
  'LOCATION' = '2nd floor';
  'CITY' = 'Los Angeles';
  'STATE' = 'CA'
}


A somewhat non-intuitive but efficient way to create a custom object with empty properties is

$obj = "" | Select-Object -Property FNAME, LNAME, DEPT, LOCATION, CITY, STATE

__________________
Evgenij Smirnov

My personal blog (German): http://www.it-pro-berlin.de/
My stuff on PSGallery: https://www.powershellgallery.com/profiles/it-pro-berlin.de/
0
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 93
Reply with quote  #4 
Ok, that works great.  Is it possible to split the custom object, $obj, with a delimiter?  for example


Robert | Johnson | Accounting | 2nd floor | Los Angeles | CA
0
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 93
Reply with quote  #5 
Also, is it possible to display the custom object with a different color?

When I use Write-Host, it includes the '@" symbol and '=' sign.

I would like to instead display in cyan like below

FNAME        : Robert
LNAME        : Johnson
DEPT           : Accounting
LOCATION    : 2nd Floor
CITY            : Los Angeles
STATE          : MA


if I put in just $obj then it does display properly but in white
0
cj_berlin

Avatar / Picture

Senior Member
Registered:
Posts: 371
Reply with quote  #6 
Quote:
Originally Posted by meloao
Ok, that works great.  Is it possible to split the custom object, $obj, with a delimiter?  for example


Robert | Johnson | Accounting | 2nd floor | Los Angeles | CA


You need to understand how objects work. There is no "natural order" of properties, nor a "natural plain-text representation". Each property can potentially contain an array of other objects, possibly of different types (and AD group can contain user, machine and group members).

So, if you need a certain visual representation of an object, you'll have to "design" it according to your need. In your example, you could do a straightforward


"$($obj.FNAME) | $($obj.LNAME) | $($obj.DEPT) | $($obj.CITY) | $($obj.STATE)"

or leverage known functionality like

($obj | ConvertTo-CSV -Delimiter "|" -NoTypeInformation)[1]

where [1] references the second line of text output by ConvertTo-CSV, the first line being the header.

__________________
Evgenij Smirnov

My personal blog (German): http://www.it-pro-berlin.de/
My stuff on PSGallery: https://www.powershellgallery.com/profiles/it-pro-berlin.de/
0
cj_berlin

Avatar / Picture

Senior Member
Registered:
Posts: 371
Reply with quote  #7 
Quote:
Originally Posted by meloao
Also, is it possible to display the custom object with a different color?

When I use Write-Host, it includes the '@" symbol and '=' sign.

I would like to instead display in cyan like below

FNAME        : Robert
LNAME        : Johnson
DEPT           : Accounting
LOCATION    : 2nd Floor
CITY            : Los Angeles
STATE          : MA


if I put in just $obj then it does display properly but in white


See my previous post ;-) The only built-in cmdlet able to colour the output is Write-Host. If you need a certain order of properties, you can design it by composing a string and then using Write-Host on it. If you're OK with whatever order of properties PowerShell throws at you, you can loop through all of them like here:

$obj.PSObject.Properties.Foreach({Write-Host "$($_.Name.PadRight(8," ")) : $($obj.($_.Name))" -ForegroundColor Cyan})

__________________
Evgenij Smirnov

My personal blog (German): http://www.it-pro-berlin.de/
My stuff on PSGallery: https://www.powershellgallery.com/profiles/it-pro-berlin.de/
0
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 93
Reply with quote  #8 
This code below is most efficient given there are over 20 columns

 
($obj | ConvertTo-CSV -Delimiter "|" -NoTypeInformation)[1]



I did notice that output is displaying double quotes.  Can the data be displayed without the double quotes?


Thanks so much for  (quick) feedback!  I am learning so much. 
0
cj_berlin

Avatar / Picture

Senior Member
Registered:
Posts: 371
Reply with quote  #9 
Quote:
Originally Posted by meloao
This code below is most efficient given there are over 20 columns

($obj |ConvertTo-CSV -Delimiter"|"-NoTypeInformation)[1]



I did notice that output is displaying double quotes.  Can the data be displayed without the double quotes?


No, CSV in PowerShell will always put double quotes around string data. This is not required by the RFC as long as the delimiter is not contained within a data field, but it is allowed by the RFC to do it this way.

__________________
Evgenij Smirnov

My personal blog (German): http://www.it-pro-berlin.de/
My stuff on PSGallery: https://www.powershellgallery.com/profiles/it-pro-berlin.de/
0
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 93
Reply with quote  #10 
OK, I went with the first option:

<code>"$($obj.FNAME) | $($obj.LNAME) | $($obj.DEPT) | $($obj.CITY) | $($obj.STATE)" </code>
0
Previous Topic | Next Topic
Print
Reply

Quick Navigation:

Easily create a Forum Website with Website Toolbox.