Solved

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

Posted on 2014-12-04
5
120 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_
  • 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
A brief introduction to what I consider to be the best editor for PowerShell.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

792 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