Solved

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

Posted on 2014-12-04
5
116 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
Comment Utility
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_
Comment Utility
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_
Comment Utility
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
Comment Utility
$( ) 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_
Comment Utility
thanks a lot for your complement of answer.

toshi
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
This article will help you understand what HashTables are and how to use them in PowerShell.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now