locked
want to select any word of a single string from a file while comparing the other file and put it into a variable. RRS feed

  • Question

  • hello all,

    Nice to be in this group.
    Pretty new to PS.

    i tried all possibility but not getting my desired outputs
    here is what i have for you.

    want to get each word of a file.

    lun.txt

    lunname1
    lunname2
    lunname3

    luns.txt

    volname1 lunname1 2gb 1gb
    volname2 lunname2 3gb 2gb
    volname3 lunname3 4gb 3gb

    this is what i have written and tried.

    foreach($j in Get-Content lun.txt)
    {
    $vn_name=get-content luns.txt | ForEach-Object{ $_.split(' ')[0] }
    or $vn_name=Get-Content luns.txt | Select-String (get-content lun.txt) | %{ (-split $_)[0] }
    or (Select-String -Path luns.txt -Pattern '(?:\w+\s+){2}(\w+)').Matches | ForEach-Object { $_.Groups[1].Value }

    $lun_size=get-content luns.txt | ForEach-Object{ $_.split(' ')[2] }
    echo "New-NcLun -VserverContext abc -Path /vol/$vn_name/$j -Size $lun_size -Unreserved"
    echo "Set-NcLunSpaceReserved -VserverContext abc -path /vol/$vn_name/$j -off"
    }

    but all this is giving the below wrong output.

    Set-NcLunSpaceReserved -VserverContext abc -path /vol/volname1 volname2 volname3/lunname1 -off
    Set-NcLunSpaceReserved -VserverContext abc -path /vol/volname1 volname2 volname3/lunname2 -off
    Set-NcLunSpaceReserved -VserverContext abc -path /vol/volname1 volname2 volname3/lunname3 -off

    but rather i want as below.

    New-NcLun -VserverContext abc -Path /vol/volname1/lunname1 -Size 1gb -Unreserved
    Set-NcLunSpaceReserved -VserverContext abc -path /vol/volname1/lunname1 -off
    New-NcLun -VserverContext abc -Path /vol/volname2/lunname2 -Size 2gb -Unreserved
    Set-NcLunSpaceReserved -VserverContext abc -path /vol/volname2/lunname2 -off
    New-NcLun -VserverContext abc -Path /vol/volname3/lunname3 -Size 3gb -Unreserved
    Set-NcLunSpaceReserved -VserverContext abc -path /vol/volname3/lunname3 -off

    • Moved by Bill_Stewart Wednesday, July 26, 2017 7:49 PM Abandoned
    Monday, June 26, 2017 3:57 PM

All replies

  • If I got you right you should change your "config file" to a csv format like this:

    VolName,LunName,Size
    volname1,lunname1,1gb
    volname2,lunname2,2gb
    volname3,lunname3,3gb

    Notice that the first line contains the headers. Let's say the name of the file is "LunConfig.csv". Then you could do something like this:

    $LunConfig = Import-Csv -Path 'lunconfig.csv' -Delimiter ','
    
    Foreach($Config in $LunConfig){
        New-NcLun -VserverContext abc -Path "/vol/$($Config.VolName)/$($Config.LunName)" -Size $($Config.Size) -Unreserved -WhatIf
        Set-NcLunSpaceReserved -VserverContext abc -path "/vol/$($Config.VolName)/$($Config.LunName)" -off  -WhatIf
    }

    If you tested it and you're satisfied with the result you can remove the "-WhatIf"

    AND ... you should take a little time and learn the basics of Powershell. I'm convinced that it will pay off for you in the future.  ;-)

    AND ... please format the code as code here in the forum. Otherwise it will be hard to read and to understand. Thanks


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Monday, June 26, 2017 8:51 PM
    Monday, June 26, 2017 8:48 PM