none
[Powershell 3] add value from where-object ? RRS feed

  • Question

  • hello, in this script i need to add email value to a string email1@email.it; email2@email.it;email3@email.it ...

    help :)


    Thursday, February 12, 2015 9:00 AM

Answers

  • Hi Enrico,

    each entry in $Rubrica is an object with two properties (nome and email). If you want to add the email, you need to specify the email property:

    $Rubrica | Where-Object { $_.nome -match $Nome } | ForEach-Object { $CCN.Add($_.Email) }

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Thursday, February 12, 2015 11:04 AM

All replies

  • Hi Enrico,

    this image is nigh impossible to read. You can insert a codeblock with the code, which will be a lot more legible.

    That said, here's an example on how to add strings to properties on csv file rows:

    # Import from Csv
    $Data = Import-csv C:\temp\users.csv  # Csv has two columns: 'Name' and 'Email'
    
    # Append string to existing string
    foreach ($d in $Data) { $d.Email = $d.Email + "@email.it" }
    
    # Export to file
    $Data | Export-Csv C:\temp\users2.csv -NoTypeInformation

    I hope that helps you, but that's my best guess based on the description you provided :)

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Thursday, February 12, 2015 9:08 AM
  • cls
    Set-Location C:\Dropbox\Powershell
    [Reflection.Assembly]::LoadWithPartialname("Microsoft.Office.Interop.Outlook") | out-null    #|  Attiva il connettore Outlook e non fa vedere l'output
    [enum]::GetNames("Microsoft.Office.Interop.Outlook.OlDefaultFolders")  |Out-Null             #|  Attiva il connettore Outlook e non fa vedere l'output
    $olFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type]                  #|> Attiva il connettore Outlook
    $outlook = new-object -comobject outlook.application                                         #|  Attiva il connettore Outlook
    $namespace = $outlook.GetNameSpace("MAPI")                                                   #|  Attiva il connettore Outlook
    $Cartella = $namespace.Folders.Item(1).folders.item(2).folders.item(1).folders.item(1)      # x pc_enrico Lavoro .seleziona la cartella inbox->...>Attivazioni
    #$Cartella = $namespace.Folders.Item(1).folders.item(2).folders.item(3).folders.item(1)       # su pc Virtuale cambia il numero item non so perchè...
    $ElencoEmail = $Cartella.Items | select -Property CreationTime,Body                          #Crea un elenco email scegliendo solo i campi CreationTime e Body
    
    $DataInizio = [datetime]::parse("01/02/2014")
    $DataFine = [datetime]::parse("01/03/2014")
    
    
    $EmailSelezionate = $ElencoEmail | where {$_.CreationTime -gt $DataInizio -and $_.CreationTime -lt $DataFine } # sfoglia l'elenco email, cerca quelle creata nell'intervallo della data, è in formato data non stringa
    $EmailSelezionate |foreach {$_ | select body  } | export-csv "$env:tmp\EmailSelezionate.csv"
    $Nomi = select-string -Path "$env:tmp\EmailSelezionate.csv" -Pattern 'gentile' | select line  |foreach { $_.Line.Substring(11)} # cancella gli spazi e la scritta gentile e li asssegna a $Nomi
    $Nomi | Out-File "$env:USERPROFILE\desktop\NodInScadenza.txt"       # salva i nomi dei nod in scadenza su un file
    $NomiCorti = $Nomi | foreach {  $_.substring(0,  $_.IndexOf(" ")) } # per ogni nome in elenco crea una sottostringa che inizia da zero e lunga fino alla posizione del carattere spazio
    $Nomi
    $Rubrica = import-csv Rubrica_Nod_Annuale.csv | Where-Object {$_.email.length -gt 1} | select nome,email #assegna a $Rubrica i nomi ed email della rubrica di adhoc, ma solo se hanno l'email
    
    
    
    $i =0
    
    do {
      $Nome= $nomicorti.item($i)
      write-host $nome -BackgroundColor YELLOW -ForegroundColor Red
    $Rubrica | where { $_.nome -match $Nome } 
    
    $i++
    } while ($i -lt $Nomicorti.Count) 
    i want add to string $.email from 
    $Rubrica | where { $_.nome -match $Nome } 

    do {
      $Nome= $nomicorti.item($i)
      write-host $nome -BackgroundColor YELLOW -ForegroundColor Red
    $Rubrica | where { $_.nome -match $Nome } 
    
    $i++
    } while ($i -lt $Nomicorti.Count) 



    Thursday, February 12, 2015 9:27 AM
  • Hi Enrico,

    how about doing this then:

    $Rubrica | where { $_.nome -match $Nome } | Select nome, @{ n = "Email"; e = {$_.Email + "@email.it"}}

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Thursday, February 12, 2015 9:35 AM
  • mmm... thanks but i can't understand how can i take email value :(

    forgive me, I started studying powershell last week :)

    Thursday, February 12, 2015 10:04 AM
  • Hi Enrico,

    then first of all: Welcome to Powershell scripting :)

    To start things simple, try reducing the overall complexity of what you try to the minimal steps probably necessary. Then build a test case with a small input / expected output.

    These pieces of information are usually very helpful to people trying to help you:

    • Input. What is the information you start with (and how is it structured / formatted)?
    • Output. What is the output you get?
    • Intended Output. What would you want to receive as output?
    • Errors. If you encounter an error, just what is the error? (Try translating error text to English)

    Now, with that out of the way, let's make sure I understand your issue properly:

    • Do you want to replace / append text to existing email data?
    • Do you want to filter by email instead of name?
    • If it's neither, try following the steps above: What is the concrete codeblock, what is the input, what is the output and what is the output you would like to have?

    Cheers,
    Fred


    There's no place like 127.0.0.1


    • Edited by FWN Thursday, February 12, 2015 10:24 AM
    Thursday, February 12, 2015 10:23 AM
  • i need a string with only all email select and for copy it in email CCN 

    my script search email from csv and create new email with CCN: , i need create a string for ccn: email1@gmail.it; email2@libero.it;....

    Thursday, February 12, 2015 10:31 AM
  • $Rubrica | where { $_.nome -match $Nome } |select Email |foreach {$CCN.Add( $_)}
    i try this but output is not correct :(
    Thursday, February 12, 2015 10:44 AM
  • Hi Enrico,

    each entry in $Rubrica is an object with two properties (nome and email). If you want to add the email, you need to specify the email property:

    $Rubrica | Where-Object { $_.nome -match $Nome } | ForEach-Object { $CCN.Add($_.Email) }

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Thursday, February 12, 2015 11:04 AM
  • Chiamata al metodo non riuscita. [Selected.System.Management.Automation.PSCustomObject] non contiene un metodo denominato 'Add'.
    In C:\Dropbox\Powershell\email.ps1:32 car:69
    + $Rubrica | Where-Object { $_.nome -match $Nome } | ForEach-Object { $CCN.Add($_. ...
    +                                                                     ~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (Add:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound
    

    Thursday, February 12, 2015 11:06 AM
  • Well, $CCN does not have an $add Method.

    Where/How did you introduce the $CCN variable?


    There's no place like 127.0.0.1

    Thursday, February 12, 2015 11:11 AM
  • yeaaaaah!!! thanks, the i solution is

    $ccn=""
    
    do{.....................
    
    $Rubrica | Where-Object { $_.nome -match $Nome } | ForEach-Object { $CCN += $_.email + "; " }

    Thursday, February 12, 2015 11:14 AM