[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2014-12-04
5
Medium Priority
?
128 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 2000 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

649 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