One Array and Concat in LoopBack

This topic contains 7 replies, has 2 voices, and was last updated by  Renaud 4 days, 3 hours ago.

  • Author
  • #104414


    Hi all ^^,

    I would like to do a script to automatically configure a Cluster Hyper-V / CVS and other operations.
    To do that i have the list of the server in a file ListSrv.txt.
    So i create an array to put all the server in.
    After i would like to do the Test of validity beforecreating the ckuster with the command Test-Cluster -Node...,
    I need ton concat the $TabSrv[0] "Add a ","" with $TabSrv[1]"Add a ","" if there is another server etc...
    To do automatically this commande like that :

    Test-Cluster -Node $TabSrv[0],$TabSrv[1] -Include Hyper-V,Stockage 

    But i've no idea to do that.

    $FileSrv = "C:\Users\admin\Documents\ListSrv.txt"
    $ListSrv = (Get-Content "C:\Users\admin\Documents\ListSrv.txt")
    $TabSrv = @()
    [Int]$i = 0
    $x = Get-Content $FileSrv | measure-object -line
    $nbl = $x.Lines                
     forEach($srv in $ListSrv) {
       $TabSrv += $srv
       $i = $i+1
    #Test Cluster Hyper-v et Stockage
    Test-Cluster -Node $TabSrv[0],$TabSrv[1] -Include Hyper-V,Stockage 

    Thx all for replay ^^

  • #104419

    Olaf Soyk

    May I recommend to change from plain text file as your source to a csv file. It's easier to store structured data and it's even easier to access these structured data in Powershell. This csv file could look like this:


    Then you simply do something like this:

    $ClusterList = Import-Csv -Path ..\ClusterNodeList.csv -Delimiter ','
    Foreach($Cluster in $ClusterList){
        Test-Cluster -Node $Cluster.Node1,$Cluster.Node2 -Include Hyper-V,Stockage
  • #104446


    Yes good idea 😉

    But if there is 50 servers how do you do with the command ?

    Test-Cluster -Node $Cluster.Node1,$Cluster.Node2.......,$Cluster.Node50

    How could we generate this command without do it manually ?

    • #104450


      after try you'r solution, it's the same think like me .
      More complicate i think.
      Perhaps my explication wasnt good 😉

      For exemple, we have 5 servers, we could have 100 servers etc...
      The need is to have this line with all the servers in the csv :

      The csv is like this :


      at least i need to have this line :

       Test-Cluster -Node $Cluster.Node1,$Cluster.Node2,$Cluster.Node3,$Cluster.Node4,$Cluster.Node5 -Include Hyper-V,Stockage
  • #104456

    Olaf Soyk

    OK, I think I misunderstood you. It's not about several clusters it's just about one cluster with several nodes, right? Nothing easier than that (almost) 😉
    If you have an array of elements (strings) you can join them with the -join statement. Assumed you use your listserv.txt file you could do something like this:

    $NodeList = ( Get-Content -Path 'C:\Users\admin\Documents\ListSrv.txt' ) -join ','
    Test-Cluster -Node $NodeList -Include Hyper-V,Stockage 
  • #104462


    Amazing !!! its' work !

    You're awsome, thx a lot Olaf ^^

    $FileSrv = "C:\Users\admin\Documents\ListSrv.csv"
    $ListSrv = (Get-Content "C:\Users\admin\Documents\ListSrv.txt") -Join ','
    Write-Host "List Srv : $ListSrv"

    List Srv : HyperV1,HyperV2

    Thx again !

  • #104471


    :// , no it's not working.
    The Write-Host write just one Word who is "HyperV1,HyperV2".
    So when i do the cmdlet Test-Cluster, he doesn't understand.

  • #104474


    Yes found it !

    $ListSrv = (Get-Content "C:\Users\admin\Documents\ListSrv.txt") 
    $CHyp = "Hyper-V"
    $CStorage = "Stockage"
    #$ListNetwork = "C:\Users\admin\Documents\ListNetwork.txt"
    #$x = Get-Content $FileSrv | measure-object -line
    #$nbl = $x.Lines                
    #$ClusterList = Import-Csv -Path $FileSrv -Delimiter ','
    Test-Cluster -Node $ListSrv -Include $CHyp,$CStorage

    Just with the like that it's works, i was very surprise it' works.

    Thx to help me , i have learn a lot of things with you 😉

You must be logged in to reply to this topic.