Extract a word we don't know of a chain file ?

Welcome Forums General PowerShell Q&A Extract a word we don't know of a chain file ?

This topic contains 10 replies, has 3 voices, and was last updated by

 
Participant
2 months, 1 week ago.

  • Author
    Posts
  • #144479

    Participant
    Topics: 5
    Replies: 21
    Points: 26
    Rank: Member

    Hello all,

    I explain to you my goal :

    I have a chain file who are format  like that :

     

    a.b.c name.server.FQDN date
    a.b.c toto.titi date
    a.b.c tata.totoooo date
    a.b.c lila.lilo date
    ....
    

    The lenght of the Name.server is different each times.
    I need to extract just the Second value ex "toto.titi", it's begin always at the same number, here "7", but we don't know the length of the value.
    So we can say i would like to extract the word wo begin at the Seven caratere and finish by the space.

    $file = "l:\admin\testslp.txt"
    
    ForEach ($ligne in $file){
        Write-Host "ligne : $ligne"
       $Lenght = $ligne.length
       Write-Host "Lenght : $Length"
       $NumEnd = $Lenth-5
       Write-Host "NumEnd : $NumEnd"
       $Chain = $ligne.SubString(6,$NumEnd)
       Write-Host "Chain : $Chain"
    } 
    

    Thx for help ^^

  • #144489

    Participant
    Topics: 1
    Replies: 1331
    Points: 1,680
    Helping Hand
    Rank: Community Hero

    Why so complicated? ... or did I get something wrong?

    $content = Get-Content -Path 'l:\admin\testslp.txt'
    
    foreach ($item in $content) {
        ($item -split '\s+')[1]
    }
  • #144501

    Participant
    Topics: 2
    Replies: 937
    Points: 1,628
    Helping Hand
    Rank: Community Hero

    Use the built-in CSV cmdlets, to convert what you have making the first line the header and select the name.server.FQDN property.

     
    Get-Command -Name '*-csv' | Format-Table -AutoSize
    
    CommandType Name            Version Source
    ----------- ----            ------- ------
    Cmdlet      ConvertFrom-Csv 3.1.0.0 Microsoft.PowerShell.Utility
    Cmdlet      ConvertTo-Csv   3.1.0.0 Microsoft.PowerShell.Utility
    Cmdlet      Export-Csv      3.1.0.0 Microsoft.PowerShell.Utility
    Cmdlet      Import-Csv      3.1.0.0 Microsoft.PowerShell.Utility
    
    
    # get function / cmdlet details
    (Get-Command -Name Import-Csv).Parameters
    Get-help -Name Import-Csv -Examples
    Get-help -Name Import-Csv -Full
    Get-help -Name Import-Csv -Online
    
  • #144678

    Participant
    Topics: 5
    Replies: 21
    Points: 26
    Rank: Member

    Hello, with y'our help i have done this little script :

    # Declaration des Variables # 
    $PSLP = "F:\admin\SLP-Name.txt"
    $FileBacklog = "f:\admin"
    $Backlog = @()
    $FileSLP = Get-Content $PSLP
    cd F:\Veritas\NetBackup\bin\admincmd
    
    ##### Corps du script  #####
    
    # Création du fichier des BackupID présent dans les SLP #
    $Backlog = ForEach ($slp in $FileSLP){
        .\nbstlutil.exe stlilist -l -lifecycle $slp -image_incomplete | find /i " I "
    }
    
    # Exécution des commandes de Cancel et Expiration des BackupID #
    $Backlog | Out-File "$FileBacklog\Backlog.txt"
    $content = Get-Content "F:\admin\Backlog.txt"
    ForEach ($ID in $content){
        $value = ($ID -split '\s+')[2]
        .\nbstlutil.exe cancel -backupid $value
        .\bpexpdate.exe -backupid $value -d 0 -force
    }
    
    .\bpimage.exe -cleanup -allclients
    .\nbdelete.exe -allvolumes -force
    
    
    # Il faudra ensuite se connecter sur l'Appliance et taper les commandes suivantes :
    # .\crcontrol.exe --processqueueinfo
    # .\crcontrol.exe --processqueue
    # .\crcontrol.exe --processqueueinfo, ceci tant que la valeur de "Busy :" est "on"
    # .\crcontrol.exe --compactstart
     
    
     $SLP = (.\nbstlutil.exe report) | Export-Csv "f:\admin\slp.csv" -Delimiter " " -Encoding UTF8
    

    I have manually create the SLP-Name.txt file, i would like to create it when i execute the script.
    To have the SLP, i need to run this command ".\nbstlutil report" Netbackup Command"

    I try with this code :

    .\nbstlutil report | Out-File -LiteralPath f:\admin\slp.txt
    $csv = Get-Content "f:\admin\slp.txt" | Export-Csv -Path "f:\admin\slp.csv" -Delimiter "," -Encoding UTF-8
    

    But the content of the csv is not waht i have expected :

    PS F:\Veritas\NetBackup\bin\admincmd> Get-content -Path "F:\admin\slp.csv"
    #TYPE System.String
    "PSPath","PSParentPath","PSChildName","PSDrive","PSProvider","ReadCount","Length"
    "F:\admin\slp.txt","F:\admin","slp.txt","F","Microsoft.PowerShell.Core\FileSystem","1","0"
    "F:\admin\slp.txt","F:\admin","slp.txt","F","Microsoft.PowerShell.Core\FileSystem","2","32"
    "F:\admin\slp.txt","F:\admin","slp.txt","F","Microsoft.PowerShell.Core\FileSystem","3","41"
    "F:\admin\slp.txt","F:\admin","slp.txt","F","Microsoft.PowerShell.Core\FileSystem","4","24"
    "F:\admin\slp.txt","F:\admin","slp.txt","F","Microsoft.PowerShell.Core\FileSystem","5","33"
    "F:\admin\slp.txt","F:\admin","slp.txt","F","Microsoft.PowerShell.Core\FileSystem","6","0"
    "F:\admin\slp.txt","F:\admin","slp.txt","F","Microsoft.PowerShell.Core\FileSystem","7","75"
    "F:\admin\slp.txt","F:\admin","slp.txt","F","Microsoft.PowerShell.Core\FileSystem","8","79"
    "F:\admin\slp.txt","F:\admin","slp.txt","F","Microsoft.PowerShell.Core\FileSystem","9","79"
    "F:\admin\slp.txt","F:\admin","slp.txt","F","Microsoft.PowerShell.Core\FileSystem","10","79"
    "F:\admin\slp.txt","F:\admin","slp.txt","F","Microsoft.PowerShell.Core\FileSystem","11","0"
    "F:\admin\slp.txt","F:\admin","slp.txt","F","Microsoft.PowerShell.Core\FileSystem","12","79"
    "F:\admin\slp.txt","F:\admin","slp.txt","F","Microsoft.PowerShell.Core\FileSystem","13","0"
    

    Have you an idea why ?

    Thx again for your help 😉

  • #144687

    Participant
    Topics: 1
    Replies: 1331
    Points: 1,680
    Helping Hand
    Rank: Community Hero

    But the content of the csv is not waht i have expected :
    Have you an idea why ?

    Hmmm ... what did you expect? 😉

    If you like to read CSV files you should use Import-CSV instead of Get-Content. And for Export-CSV I recommend using the parameter -NoTypeInformation. The type information wouldn't bother Excel or Powershell but on the other hand it usualy does not serv any benefit.

  • #144720

    Participant
    Topics: 5
    Replies: 21
    Points: 26
    Rank: Member

    Yes ok.

    I expected this format and result :

    PS F:\Veritas\NetBackup\bin\admincmd> Get-content -Path "F:\admin\slp.txt"
    
    Backlog of incomplete SLP Copies
    	In Process (Storage Lifecycle State: 2):
    		Number of copies:	
    		Total expected size	
    
    SLP Name: (state)                                 Number of copies: Size:  
    SLP_1_2_3 (inactive)                                 
    SLP_1_2_4 (inactive)                                
    SLP_1_2_5 (inactive)                                
    
    Total:
    

    I try again :

    PS F:\Veritas\NetBackup\bin\admincmd> Get-content -Path "F:\admin\slp.txt"| Export-Csv -NoTypeInformation -Path "F:\admin\slp.csv" -Delimiter ","
    
    PS F:\Veritas\NetBackup\bin\admincmd> import-csv -Path "F:\admin\slp.csv"
    
    
    PSPath       : F:\admin\slp.txt
    PSParentPath : F:\admin
    PSChildName  : slp.txt
    PSDrive      : F
    PSProvider   : Microsoft.PowerShell.Core\FileSystem
    ReadCount    : 1
    Length       : 0
    
    PSPath       : F:\admin\slp.txt
    PSParentPath : F:\admin
    PSChildName  : slp.txt
    PSDrive      : F
    PSProvider   : Microsoft.PowerShell.Core\FileSystem
    ReadCount    : 2
    Length       : 32 
    etc...
    
  • #144725

    Participant
    Topics: 1
    Replies: 1331
    Points: 1,680
    Helping Hand
    Rank: Community Hero

    I guess I missed something on way to here ... your initial request was to extract from a certain text file from each line a server name what's surrounded by something seperated with withe space, right? Did you try the code I posted in my first answer?

  • #144764

    Participant
    Topics: 5
    Replies: 21
    Points: 26
    Rank: Member

    Yes i do 😉
    It's in the script between the line 17 to 22.

    So that's unblock me for the script.

    After that, the problem is not the same !

    The create of the csv file !

    I try with NoType, ASCII...UTF8, and it's just write lenght as header, and the length value.

    Should i do another post ?

  • #144773

    Participant
    Topics: 1
    Replies: 1331
    Points: 1,680
    Helping Hand
    Rank: Community Hero

    Ah ... ok ... now I got it. Your initial request was just to extract the server names to get used for your external backup tools, right?

    OK, but I don't know these backup tools and I don't know what kind of output they produce. You could try to post some lines (maybe 4 or 5, formatted as code please) and we could try to find a usable structure.

    No, don't do another post ... now we already have been gone that far now we can proceed this way ... 😉

  • #144792

    Participant
    Topics: 5
    Replies: 21
    Points: 26
    Rank: Member

    The command i use to have the SLP (Storage Life Policy) since the Appliance NetBackup is .\nbstlutil report
    Icould send you exactly the output we have tomorrow.

    I succeed to do a txt file of the output but not a csv.
    I think i will not use the csv.

    The Name of the SLP that interest me as somthing like that :

    SLP_1_2_3 (inactive)       54545412Mo
    

    I think your first code help me again but What the meaning of the "+" ? and [num] : Number of occurence ?

    $file = "l:\admin\slp.txt"
    $array = @()
    $content = Get-Content "L:\admin\SLP.txt"
    
    
    ForEach($slp in $content){
        Write-Host $slp
        If($slp -like "(inactive)"){
        $value = ($slp -split '\s+')[0] 
        $array += $value
        }
    }
    

    But doesn't work, the if must be wrong.

  • #144795

    Participant
    Topics: 5
    Replies: 21
    Points: 26
    Rank: Member

    I think i have found :

    $file = "l:\admin\slp.txt"
    $array = @()
    Get-Content "l:\admin\slp.txt"| Where-Object {$_ -like "*(inactive)*"} | Out-File -LiteralPath "L:\admin\testslp.txt"
    
    $content = Get-Content "L:\admin\testslp.txt"
    
    ForEach($slp in $content){
        $value = ($slp -split '\s+')[0]
        $array += $value
    } 
    
    $array | Out-File -LiteralPath "l:\admin\SLP-Name.txt"
    

    Thx again Olaf 😉
    You help me to think.

    See you tomorrow 😉

    Have a good evening.

You must be logged in to reply to this topic.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort