none
How do I add a line to a system object? RRS feed

  • Question

  • Thanks in advance for the help.

    Writing a script to track and remove unused DL's. Need to track the activity in a .csv. So I have a base csv that I import to $TrackingCsv:

    $TrackingCsv

    GroupSMTP                                NumDaysUnused

    All HR@good.com                       10

    MyTeam@good.com                    250

    All I want to do is add an entry to $TrackingCsv because a new group was created since it ran last. I will then save the $TrackingCsv (Export-CSV) over writing the original .csv file. some things I have tried:

    $TrackingCsv += $TrackingCsv[0]
    $TrackingCsv[-1].GroupSMTP = "New_List3@good.com"
    $TrackingCsv[-1].NumDaysUnused = "7"
    $TrackingCsv += 'New_List4@good.com','5'
    $TrackingCsv.Add("New_List44@good.com"),("55")
    $TrackingCsv += @('New_List45@good.com','55')
    $TrackingCsv += ("New_List44@good.com"),("55")
    $TrackingCsv += @("New_List44@good.com"),@("55")
    $TrackingCsv += $TrackingCsv[13]
    $TrackingCsv[12].GroupSMTP = "New_List3@good.com"
    $TrackingCsv[12].NumDaysUnused = "7"
    $TrackingCsv[12] = "New_List3@good.com"
    $TrackingCsv += $TrackingCsv[24]
    $TrackingCsv[13].GroupSMTP.add("New_List3@good.com")
    $TrackingCsv[13] = @("New_List3@good.com", "98")
    $TrackingCsv[13] = ("New_List3@good.com", "98")
    $TrackingCsv[12] = @(,("New_List3@good.com", "98"))
    $TrackingCsv[2].GroupSMTP = "New_List3@good.com"
    $TrackingCsv += $TrackingCsv("fff","ddd")
    $TrackingCsv[+12] = ("New_List434@good.com"),("a55")
    $TrackingCsv += @{GroupSMTP="NewValue6453@good.com",NumDaysUnused="345"}
    $TrackingCsv += @{GroupSMTP="NewValueaaa@good.com"}, @{NumDaysUnused="aaa"}
    $TrackingCsv[13428].NumDaysUnused = "123"

    I understand this list is all over the place but I've been trying for hours. The only thing I get is a new value in the GroupSMTP column and am never able to set the NumDaysUnused. Any help would be appreciated.

    PS. Hoping this is a simple answer have only been doing PS for a few weeks. Switching over from 20+ years of LotusScript programming.

    Tuesday, May 17, 2016 11:09 PM

Answers

  • Assuming you have v3+, you can use Export-Csv's -Append parameter (it's slow, but useful in situations like these).

    This is all you'd need to do to add to your input file:

    New-Object PsObject -Property @{GroupSMTP='SomeOtherGroup@good.com';NumDaysUnused='30'} |
        Export-Csv .\input.csv -Append
    
    
    
    Example:
    PS C:\Scripts\PowerShell Scripts\Misc Testing\5-18-2016> Import-Csv .\input.csv | ft -AutoSize
    
    GroupSMTP       NumDaysUnused
    ---------       -------------
    AllHR@good.com  10           
    MyTeam@good.com 250          
    
    
    PS C:\Scripts\PowerShell Scripts\Misc Testing\5-18-2016> New-Object PsObject -Property @{GroupSMTP='SomeOtherGroup@good.com';NumDaysUnused='30'} |
        Export-Csv .\input.csv -Append
    
    PS C:\Scripts\PowerShell Scripts\Misc Testing\5-18-2016> Import-Csv .\input.csv | ft -AutoSize
    
    GroupSMTP               NumDaysUnused
    ---------               -------------
    AllHR@good.com          10           
    MyTeam@good.com         250          
    SomeOtherGroup@good.com 30  


    Wednesday, May 18, 2016 12:49 PM

All replies

  • You are not using a CSV.  It appears to be just some sort of file with a bunch of lines.

    To manage a CSV you need to clone and append a row then set the values or use a filter to lookup a row and adjust.  This would best be done with a datatable rather than a CSV.


    \_(ツ)_/

    Tuesday, May 17, 2016 11:19 PM
  • $csv=@'
    GroupSMTP,NumDaysUnused
    All HR@good.com,10
    MyTeam@good.com,250
    '@ | ConvertFrom-Csv
    
    $row=$csv[0].Psobject.Copy()
    $row.GroupSMTP='newgroup@good.com'
    $row.NumDaysUnused=0
    $csv+=$row


    \_(ツ)_/

    Tuesday, May 17, 2016 11:24 PM
  • Assuming you have v3+, you can use Export-Csv's -Append parameter (it's slow, but useful in situations like these).

    This is all you'd need to do to add to your input file:

    New-Object PsObject -Property @{GroupSMTP='SomeOtherGroup@good.com';NumDaysUnused='30'} |
        Export-Csv .\input.csv -Append
    
    
    
    Example:
    PS C:\Scripts\PowerShell Scripts\Misc Testing\5-18-2016> Import-Csv .\input.csv | ft -AutoSize
    
    GroupSMTP       NumDaysUnused
    ---------       -------------
    AllHR@good.com  10           
    MyTeam@good.com 250          
    
    
    PS C:\Scripts\PowerShell Scripts\Misc Testing\5-18-2016> New-Object PsObject -Property @{GroupSMTP='SomeOtherGroup@good.com';NumDaysUnused='30'} |
        Export-Csv .\input.csv -Append
    
    PS C:\Scripts\PowerShell Scripts\Misc Testing\5-18-2016> Import-Csv .\input.csv | ft -AutoSize
    
    GroupSMTP               NumDaysUnused
    ---------               -------------
    AllHR@good.com          10           
    MyTeam@good.com         250          
    SomeOtherGroup@good.com 30  


    Wednesday, May 18, 2016 12:49 PM
  • Thanks for the code to solve my problem.
    Wednesday, May 18, 2016 7:22 PM