Solved

in powershell how can i cumulate string format in a function ?

Posted on 2014-12-04
5
121 Views
Last Modified: 2014-12-09
hello experts

i'm looking to format string result in a string inside my function in order to return infos with a correct display but it's not displaying as expected. I started without a function (1) but with a function i dont have the display as expected.
how can i correct this in my function (2).
thank you in advance.


1 ----------------------------  correct display ----------------------------------
   $machineInfos = Get-ADComputer WSTA00010145 -Properties * |  select primaryGroup, canonicalname, Description
   #'primaryGroup: {0,40} canonicalName:  {1, 50}, Utilisateur: {2, 50}' -f $machineInfos.primaryGroup, $machineInfos.canonicalName, $machineInfos.description
   Write-Host ""
   
   'description : {0,80} '      -f  $machineInfos.Description
   'groupe primaire : {0, 80} ' -f  $machineInfos.primaryGroup
   'canonicalName : {0, 80}'    -f  $machineInfos.canonicalname
--------------------------------------------------------------------------------------------


2 ----------------------------  NOT correct display ----------------------------------
function InfoMachine($computerName)
{
    $result = "" 
    $machineInfos = Get-ADComputer $computerName -Properties * #|  select primaryGroup, canonicalname, Description
    #'primaryGroup: {0,40} canonicalName:  {1, 50}, Utilisateur: {2, 50}' -f $machineInfos.primaryGroup, $machineInfos.canonicalName, $machineInfos.description
    Write-Host ""
   
    $result += 'description : {0,80} '      -f  $machineInfos.Description
    $result += 'groupe primaire : {0, 80} ' -f  $machineInfos.primaryGroup
    $result += 'canonicalName : {0, 80}'    -f  $machineInfos.canonicalname

    return $result;
}
   
InfoMachine machinename0001
--------------------------------------------------------------------------------------------

toshi
0
Comment
Question by:toshi_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 40480716
Try adding a line break with each string addition.. For example..
$result += "$('description : {0,80} '      -f  $machineInfos.Description)`n"
$result += "$('groupe primaire : {0, 80} ' -f  $machineInfos.primaryGroup)`n"
$result += "$('canonicalName : {0, 80}'    -f  $machineInfos.canonicalname)`n"

Open in new window

0
 

Author Comment

by:toshi_
ID: 40480725
hello Subsun,
thank you for the reply,

why parenthesis around the lines ?  i've look up on web but i couldn't find an answer ...and i dont want to loose the thread,

thank you in advance.
0
 

Author Comment

by:toshi_
ID: 40480753
ok, i found a link that explains that
http://www.neolisk.com/techblog/powershell-specialcharactersandtokens

thanks a lot ! it's work.

Toshio
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40480832
$( ) called a subexpression operator. In the above example I used to combine the result of expression

'description : {0,80} '      -f  $machineInfos.Description

and  line break  `n

to create a single string.
0
 

Author Comment

by:toshi_
ID: 40488492
thanks a lot for your complement of answer.

toshi
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help you understand what HashTables are and how to use them in PowerShell.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question