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

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 ^^

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]

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 Microsoft.PowerShell.Utility
Cmdlet      ConvertTo-Csv Microsoft.PowerShell.Utility
Cmdlet      Export-Csv Microsoft.PowerShell.Utility
Cmdlet      Import-Csv 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

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

Have you an idea why ?

Thx again for your help :wink:

[quote quote=144678]But the content of the csv is not waht i have expected :
Have you an idea why ?[/quote]

Hmmm … what did you expect? :wink:

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.

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)                                


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 

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?

Yes i do :wink:
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 ?

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 … :wink:

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.

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 :wink:
You help me to think.

See you tomorrow :wink:

Have a good evening.