One Array and Concat in LoopBack

Welcome Forums General PowerShell Q&A One Array and Concat in LoopBack

This topic contains 7 replies, has 2 voices, and was last updated by

 
Participant
3 months, 1 week ago.

  • Author
    Posts
  • #104414

    Participant
    Points: 0
    Rank: Member

    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
       $TabSrv[$i] 
       $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

    Participant
    Points: 9
    Rank: Member

    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:

    "Node1","Node2"
    "Cluster1Node1","Cluster1Node2"
    "Cluster2Node1","Cluster2Node2"
    "Cluster3Node1","Cluster3Node2"

    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

    Participant
    Points: 0
    Rank: Member

    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

      Participant
      Points: 0
      Rank: Member

      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 :

      "Node1","Node2","Node3","Node4","Node5"
      "HyperV1","HyperV2","HyperV3","HyperV4","HyperV5"
      

      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

    Participant
    Points: 9
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    :// , 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

    Participant
    Points: 0
    Rank: Member

    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 😉

The topic ‘One Array and Concat in LoopBack’ is closed to new replies.