Solved

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

Posted on 2014-12-04
5
119 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Synchronize a new Active Directory domain with an existing Office 365 tenant
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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