Change email address in CSV - powershell

I am writing PowerShell code to export email addresses to a csv file and edit them. I've written the following:
# Script to get all DLs and email addresses in csv file
Get-ADGroup -Filter 'groupcategory -eq "distribution"' -Properties * |
select Name, mail |
Export-csv "C:\temp\Distribution-Group-Members.csv"
# Following will import the csv, make new column as proxy and save the file
# as update.csv
Import-Csv "C:\temp\Distribution-Group-Members.csv" |
Select-Object "Name", "mail", #{n = "proxy"; e = "mail"} |
Export-Csv "c:\temp\Distribution-Group-Members-Updated.csv" -NoTypeInfo
# Following script can import the csv and set proxy addresses from proxy
# column
Import-Csv "c:\temp\Distribution-Group-Members-Updated.csv" |
Foreach {
Get-ADGroup $_.Name | Set-ADGroup -Add #{
proxyaddresses = ($_.proxy -split ";")
}
}
Now, I would like to add 2 more features in the script:
update domain for existing mail column e.g. update mail address form test#abc.com to test#xyz.com
Add SMTP:test#xyz.com;smtp:test#abc.com" as Proxy mail address, so that xyz become primary mail address and abc as proxy domain
So assuming my DL name is "DL Test" email is "test#abc.com" => The script should update the email address of DL to "test#xyz.com" and add "smtp:test#abc.com" as proxy mail address
Can someone please advise, how can I achieve that?

The part where you have written:
select-object "Name", "mail", #{n = "proxy"; e = "mail"}|
The proxy part is called a calculated property. The first two with just names Name and Mail are copied directly from the input objects, but using the #{..} syntax, you can put code to calculate a new value instead.
So you can use this to achieve both your desired changes:
Import-Csv -Path 'C:\temp\Distribution-Group-Members.csv' |
Select-Object -Property Name,
#{Label='Mail'; Expression={$_.Mail -replace 'abc', 'xyz'}},
#{Label='Proxy'; Expression={"SMTP:$($_.Mail -replace 'abc', 'xyz');smtp:$($_.Mail)"}}|
Export-csv 'C:\temp\Distribution-Group-Members.csv' -NoTypeInformation

Related

I want to import CSV file, a list of group members to Distribution group name is ibv europa, but following script not working

$users = import-csv "c:\temp\merged11.csv" | ForEach-Object {Add-DistributionGroupMember -Identity "IBV Europa" }
we have removed "" from CSV file and it's now include only column of all email addresses.
I have tried different syntax's but won't helpful.
If you just have one column with the mail addresses, i would use get-content instead of import-csv and remove the header from the csv file.
merged11.csv Example:
User1#test.com
User2#test.com
User3#test.com
and the Powershell Script could be like this:
$UserEMailList = get-content "c:\temp\merged11.csv"
$UserEMailList | ForEach-Object {
Add-DistributionGroupMember -Identity "IBV Europa" -Member $_
}

Break csv data based on server name

I am having below data in my csv file
"Server Name","Control Group","Tape Number","Status"
"BR502","QCLDUSYSW","Q23028","ACTIVE"
"BR502","QCLDUSYSW","Q32521","ACTIVE"
"BR502","QCLDUSYSW","Q05599","ACTIVE"
"BR503","QCLDUIPLW","Q25582","ACTIVE"
"BR503","QCLDUIPLW","Q15529","ACTIVE"
"BR503","QCLDUIPLW","Q05109","ACTIVE"
"BR504","QCLDUSYSW","Q14445","ACTIVE"
"BR504","QCLDUSYSW","Q27785","ACTIVE"
"BR504","QCLDUUSRD","Q26071","ACTIVE"
"BR505","QCLDUGRPD","Q27657","ACTIVE"
"BR505","QCLDUIPLW","Q17404","ACTIVE"
Please let me know how to break it based on server name like below
"Server Name","Control Group","Tape Number","Status"
"BR502","QCLDUSYSW","Q23028","ACTIVE"
"BR502","QCLDUSYSW","Q32521","ACTIVE"
"BR502","QCLDUSYSW","Q05599","ACTIVE"
"Server Name","Control Group","Tape Number","Status"
"BR503","QCLDUIPLW","Q25582","ACTIVE"
"BR503","QCLDUIPLW","Q15529","ACTIVE"
"BR503","QCLDUIPLW","Q05109","ACTIVE"
so on for rest of the servers.
Use the Group-Object cmdlet to group the records together based on a particular property:
Import-Csv .\input.csv |Group-Object -Property 'Server Name' |ForEach-Object {
# for each group, output a new CSV file with just the records pertaining to that server, named after the server
$_.Group |Export-Csv .\$($_.Name).csv -NoTypeInformation
}
Something like this might do what you want:
import-csv tst.csv | where 'Server Name' -eq 'BR502'
If you want to examine each set you would use a Group-Object to bunch it on Server Name like this:
Import-Csv tst.csv | Group-Object 'Server Name'
This gives an object that has two properties Name, Count and Group which contain the Value you groped by, the number of items in the group and the list of items in the group. This can be used for calculations on each group. For example if you want to know how many in Control Groups in each server end with D you could use this:
Import-Csv tst.csv | Group-Object 'Server Name' | Foreach-Object { $name=$_.Name; Write-Output $_.Group | Where-Object 'Control Group' -LIKE '*D' | Measure-Object | Select-Object #{Label='Name';Expression={$name}},Count }
If you use aliases that command can be shortened to:
ipcsv tst.csv | Group 'Server Name' | % { $name=$_.Name; $_.Group | ? 'Control Group' -LIKE '*D' | Measure | Select #{L='Name';E={$name}},Count }

Get-ADUser account name whose email address matches any of the emails indicated in csv file

I have a comma-separated csv file as below (first row is the header):
category;email.1;email.2;email.3;email.4;email.5;email.6
category1;sample#gmail.com;;;;;
category2;;;sample2#gmail.com;;;
category3;;sample3#gmail.com;;sample44#hotmail.com;;sample55#gmail.com
and so on...
Now I import it:
$emails = Import-CSV -Encoding Default -Delimiter ";" "c:\temp\myFile.csv"
And finally, for each row in the csv I want to get the AD user account name whose email address matches any from email.1 to email.6
So I try this using a foreach loop but I have no idea what have to put within it to get what I want.
$emails | foreach {
$userAccountName = Get-ADUser -filter {something}
# do some stuff with $userAccountName
}
Note: I would like a generic solution taking into account that in future can be more than 6 emails by category. Also take into account that some emails can be empty for the category.
Once imported you can enumerate the headers matching a pattern
$emails = Import-CSV -Encoding Default -Delimiter ";" "c:\temp\myFile.csv"
$EmailHeaders = ($Emails[0].psobject.Properties|Where-Object Name -like 'email.*').Name
ATM this returns:
> $EmailHeaders
email.1
email.2
email.3
email.4
email.5
email.6
Nest foreachs's to iterate the emails per row which are populated.
Get-ADUser commented out for testing
foreach($Row in $Emails){
foreach($EmailHeader in $EmailHeaders){
if($Email=$Row.$EmailHeader){
[PSCustomObject]#{
Category = $Row.Category
Email_x = $EmailHeader
Email = $Email
SamAccountName= $Null #(Get-ADUser -Filter {EmailAddress -eq "$Email"} -Properties SamAccountName).SamAccountName
}
}
}
}
Category Email_x Email SamAccountName
-------- ------- ----- --------------
category1 email.1 sample#gmail.com
category2 email.3 sample2#gmail.com
category3 email.2 sample3#gmail.com
category3 email.4 sample44#hotmail.com
category3 email.6 sample55#gmail.com

Using a .txt file to populate a Powershell Script

I have a Powershell script that queries Active Directory, by User ID, and outputs to a .csv file listing user attributes that I specify.
The script works flawlessly for a single user, but I would like to use a text file, with a list of users, to populate the script and create output for multiple users all at once.
Please advise.
This is my original script pulling the user list from a specific OU:
import-module ActiveDirectory
$ADGroupParams=#{
'Server' = 'corp.xxxx.com'
'Searchbase' = 'OU=Security Groups,DC=corp,DC=xxxx,DC=com'
'Searchscope'= 'Subtree'
'Filter' = '*'
'Properties' = '*'
}
$SelectParams=#{
'Property' = 'cn', 'managedBy', 'info', 'sAMAccountName', 'created'
}
get-aduser #ADGroupParams | select-object #SelectParams | export-csv "C:\Scripts\Users_By_OU.csv"
Assuming your text file is a txt file with one username per line:
# Put all ADUser properties you want dumped to the CSV in an array
$adUserProperties = #(
'one',
'attribute',
'per',
'index'
)
# Read the users from the file, get the ADUser object for each one,
# selecting the properties you want dumped to the CSV, and export the
# returned information to a CSV file.
Get-Content \path\to\users.txt | Foreach-Object {
Get-ADUser -Filter "SamAccountName -eq '$($_)'" -Properties $adUserProperties
} | Select-Object -Properties $adUserProperties | Export-Csv -NoTypeInformation users.csv

run powershell command using csv as input

I have a csv that looks like
Name, email, address
Name, email, address
Name, email, address
I am wanting to run
New-Mailbox -Name "*Name*" -WindowsLiveID *email* -ImportLiveId
(where *x* is replaced by the value from the csv).
on each line in the csv file.
How can I do this?
$csv = Import-Csv c:\path\to\your.csv
foreach ($line in $csv) {
New-Mailbox -Name $line.Name -WindowsLiveID $line.Email -ImportLiveId
}
First line of csv has to be something like Name,Email,Address
If you cannot have the header in the CSV, you can also have:
$csv = Import-Csv c:\path\to\your.csv -Header #("Name","Email","Address")
-Header doesn't modify the csv file in any way.
import-csv .\file.csv -header ("first","second","third") | foreach{New-Mailbox -Name $_.first -WindowsLiveID $_.second -ImportLiveId}
This is some of the most useful information I have seen yet - it just made my job so much easier!!!
Combining Netapp commands:
get volumes from a controller, get snapshot schedule for said volumes, and export to a csv:
get-navol | Get-NaSnapshotSchedule | Export-Csv -path d:\something.csv
Import the csv reading in current values and assigning each column a label.
For each object, create a new schedule by RE-USING 4 of the 5 available columns/data fields
import-csv d:\something.csv -header ("label1","label2","label3","label4","label5") | foreach {Set-naSnapshotschedule $.label1 -Weeks $.label2 -Days $.label3 -Hours $.label4 -Whichhours "1,2,3,4,5"}
EXCELLENT STUFF!!!
Please note that the "Labels" should have an underscore - for whatever reason it isn't reflecting in the page so Dollar($)Underscore(_)Dot(.)Label