Updating Active directory DN in csv records with Powershell - powershell

I'm completely stuck on how to update multiple rows in a CSV file with new values.. the issue is as follows, I import an active directory csv export file into my powershell which has roughly 500 users, like so:
$Users = Import-CSV "C:\Users\administrator\userExport.csv"
I then need to update the DN column so that they have the new active directory structure on the new domain
Current structure:
DN:
-----------------
CN=John Smith,OU=Users,DC=XTR,DC=ORG
Required new domain structure
DN:
-----------------
CN=John Smith,OU=Users,OU=Administration,DC=RTS,DC=LIVE,DC=LOCAL
The trouble is I have no idea how I can sequentially go through the entries in this CSV files to update only the necessary elements of the entry via powershell, any help with this would be much appreciated.
Sample line from csv file:
DN objectClass instanceType
-- ------------ ------------
CN=John Smith,OU=Users,DC=XTR,DC=ORG user 4
Apologies, I know how vague the request is but I'm sure there must be an elegant way of doing this but my powershell knowledge is extremely limited.
Thanks for your time.

Import-Csv will convert each row in the CSV to an object with properties corresponding to the column headers and the column value of the current row. This makes them easy to manipulate:
$Users = Import-CSV "C:\Users\administrator\userExport.csv"
$Users |ForEach-Object {
# Update DN column value
$_.DN = $_.DN -replace ',DC=XTR,DC=ORG$', ',OU=Administration,DC=RTS,DC=LIVE,DC=LOCAL'
# Output modified object
$_
} |Export-Csv C:\Users\administrator\userExportModified.csv -NoTypeInformation

You should put your CSV sample "as is", and not trying to format it. How can we know if the CSV is well-formed ?
I am trying to answer anyway, based on a well-formed CSV, with default delimiter (comma) :
Import-CSV "C:\Users\administrator\sourceUsers.csv" | ForEach {
$_.DN = $_.DN -replace "OU=Users,DC=XTR,DC=ORG","OU=Users,OU=Administration,DC=RTS,DC=LIVE,DC=LOCAL"
# ^
# Are you sure it is actually 'OU' here and not 'CN'
$_
} | Export-Csv "C:\Users\administrator\destinationUsers.csv" -NoTypeInformation -Enconding UTF8
If there is another delimiter, eg semi colon :
Import-CSV "C:\Users\administrator\sourceUsers.csv" -Delimiter ';' | ForEach {
$_.DN = $_.DN -replace "OU=Users,DC=XTR,DC=ORG","OU=Users,OU=Administration,DC=RTS,DC=LIVE,DC=LOCAL"
# ^
# Are you sure it is actually 'OU' here and not 'CN'
$_
} | Export-Csv "C:\Users\administrator\destinationUsers.csv" -NoTypeInformation -Enconding UTF8
Edit :
Mathias answered during I was typing my own one :), I keep it only because of my remarks about CSV and the 'OU=Users...'

Related

Using Powershell, how can I export and delete csv rows, where a particular value is *not found* in a *different* csv?

I have two files. One is called allper.csv
institutiongroup,studentid,iscomplete
institutionId=22343,123,FALSE
institutionId=22343,456,FALSE
institutionId=22343,789,FALSE
The other one is called actswithpersons.csv
abc,123;456
def,456
ghi,123
jkl,123;456
Note: The actswithpersons.csv does not have headers - they are going to be added in later via an excel power query so don't want them in there now. The actswithpersons csv columns are delimited with commas - there are only two columns, and the second one contains multiple personids - again Excel will deal with this later.
I want to remove all rows from allper.csv where the personid doesn't appear in actswithpersons.csv, and export them to another csv. So in the desired outcome, allper.csv would look like this
institutiongroup,studentid,iscomplete
institutionId=22343,123,FALSE
institutionId=22343,456,FALSE
and the export.csv would look like this
institutiongroup,studentid,iscomplete
institutionId=22343,789,FALSE
I've got as far as the below, which will put into the shell whether the personid is found in the actswithpersons.csv file.
$donestuff = (Get-Content .\ActsWithpersons.csv | ConvertFrom-Csv); $ids=(Import-Csv .\allper.csv);foreach($id in $ids.personid) {echo $id;if($donestuff -like "*$id*" )
{
echo 'Contains String'
}
else
{
echo 'Does not contain String'
}}
However, I'm not sure how to go the last step, and export & remove the unwanted rows from allper.csv
I've tried (among many things)
$donestuff = (Get-Content .\ActsWithpersons.csv | ConvertFrom-Csv);
Import-Csv .\allper.csv |
Where-Object {$donestuff -notlike $_.personid} |
Export-Csv -Path export.csv -NoTypeInformation
This took a really long time and left me with an empty csv. So, if you can give any guidance, please help.
Since your actswithpersons.csv doesn't have headers, in order for you to import as csv, you can specify the -Header parameter in either Import-Csv or ConvertFrom-Csv; with the former cmdlet being the better solution.
With that said, you can use any header name for those 2 columns then filter by the given column name (ID in this case) after your import of allper.csv using Where-Object:
$awp = (Import-Csv -Path '.\actswithpersons.csv' -Header 'blah','ID').ID.Split(';')
Import-Csv -Path '.\allper.csv' | Where-Object -Property 'Studentid' -notin $awp
This should give you:
institutiongroup studentid iscomplete
---------------- --------- ----------
institutionId=22343 789 FALSE
If you're looking to do it with Get-Content you can split by the delimiters of , and ;. This should give you just a single row of values which you can then compare the entirety of variable ($awp) using the same filter as above which will give you the same results:
$awp = (Get-Content -Path '.\actswithpersons.csv') -split ",|;"
Import-Csv -Path '.\allper.csv' | Where-Object -Property 'Studentid' -notin $awp

Powershell - Finding the output of get-contents and searching for all occurrences in another file using wild cards

I'm trying to get the output of two separate files although I'm stuck on the wild card or contains select-string search from file A (Names) in file B (name-rank).
The contents of file A is:
adam
george
william
assa
kate
mark
The contents of file B is:
12-march-2020,Mark-1
12-march-2020,Mark-2
12-march-2020,Mark-3
12-march-2020,william-4
12-march-2020,william-2
12-march-2020,william-7
12-march-2020,kate-54
12-march-2020,kate-12
12-march-2020,kate-44
And I need to match on every occurrence of the names after the '-' so my ordered output should look like this which is a combination of both files as the output:
mark
Mark-1
Mark-2
Mark-3
william
william-2
william-4
william-7
Kate
kate-12
kate-44
kate-54
So far I only have the following and I'd be grateful for any pointers or assistance please.
import-csv (c:\temp\names.csv) |
select-string -simplematch (import-csv c:\temp\names-rank.csv -header "Date", "RankedName" | select RankedName) |
set-content c:\temp\names-and-ranks.csv
I imagine the select-string isn't going to be enough and I need to write a loop instead.
The data you give in the example does not give you much to work with, and the desired output is not that intuitive, most of the time with Powershell you would like to combine the data in to a much richer output at the end.
But anyway, with what is given here and what you want, the code bellow will get what you need, I have left comments in the code for you
$pathDir='C:\Users\myUser\Downloads\trash'
$names="$pathDir\names.csv"
$namesRank="$pathDir\names-rank.csv"
$nameImport = Import-Csv -Path $names -Header names
$nameRankImport= Import-Csv -Path $namesRank -Header date,rankName
#create an empty array to collect the result
$list=#()
foreach($name in $nameImport){
#get all the match names
$match=$nameRankImport.RankName -like "$($name.names)*"
#add the name from the First list
$list+=($name.names)
#if there are any matches, add them too
if($match){
$list+=$match
}
}
#Because its a one column string, Export-CSV will now show us what we want
$list | Set-Content -Path "$pathDir\names-and-ranks.csv" -Force
For this I would use a combination of Group-Object and Where-Object to first group all "RankedName" items by the name before the dash, then filter on those names to be part of the names we got from the 'names.csv' file and output the properties you need.
# read the names from the file as string array
$names = Get-Content -Path 'c:\temp\names.csv' # just a list of names, so really not a CSV
# import the CSV file and loop through
Import-Csv -Path 'c:\temp\names-rank.csv' -Header "Date", "RankedName" |
Group-Object { ($_.RankedName -split '-')[0] } | # group on the name before the dash in the 'RankedName' property
Where-Object { $_.Name -in $names } | # use only the groups that have a name that can be found in the $names array
ForEach-Object {
$_.Name # output the group name (which is one of the $names)
$_.Group.RankedName -join [environment]::NewLine # output the group's 'RankedName' property joined with a newline
} |
Set-Content -Path 'c:\temp\names-and-ranks.csv'
Output:
Mark
Mark-1
Mark-2
Mark-3
william
william-4
william-2
william-7
kate
kate-54
kate-12
kate-44

PowerShell - Find AD users with specific AD attribute

Hello and Greetings to anyone paying attention to the post.
As always, Your time and assistance is greatly appreciated and i do not take your help for granted.
You guys ( and gals ) are Lifesavers.
I have a csv sheet , filled with different VALUES, which are set to a PRATICULAR custom ATTTRIBUTE my users have. Lets call this value, "costumattribute".
I have done a test with this PS line using one of the attribute VALUES from the csv :
Get-ADUser -Filter 'costumattribute -like "+999999999"' | Select-Object samaccountname
Running this line, indeed gave me the desired result, presenting the SamAccountName of the user
Which has the value "+999999999" set in the "costumattribute" he has.
I would like to continue the command script based on the following requirements:
Values should be read from csv ( Import-CSV ) . The Csv will contain the following:
Column A: different "costumattribute" values, listed throughout the column.
From this actions, i would like , a CSV file exported, containing the following data:
SamAccount Name, for each user, matching the "costumattribute" found, from each particular line
in Column A , on the imported CSV FILE.
IMPORTANT - i need the exported file , to add BLANK line in the exported csv for each unfound value.
And not SKIP IT, so that the exported csv file, will contain exactly the same number of
Lines as the imported csv, for later comparisons.
Thank you all !
First of all, using the -like operator without wildcards will do the same as -eq.
If your csv input file looks anything like this:
ColumnA,ColumnB
-6666666,BlahBlah
+7777777,BlahBlahBlah
-8888888,BlahBlahBlahBlah
+9999999,BlahBlahBlahBlahBlah
Then something like below would result in a csv file I think you want:
$attribute = 'costumattribute'
Import-Csv -Path 'D:\Test\attributes.csv' | ForEach-Object {
$attribValue = $_.ColumnA
$accountName = (Get-ADUser -Filter "$attribute -eq '$attribValue'" -Properties $attribute).SamAccountName
"" | Select-Object #{Name = 'SamAccountName'; Expression = {$accountName -join '; '}},
#{Name = $attribute; Expression = {$attribValue}}
} | Export-Csv -Path 'D:\Test\users_attributes.csv' -NoTypeInformation

How do I merge 2 adjacent columns in a CSV file into a single column, separated by comma? (PowerShell)

I have a CSV file with 2 columns, latitude & longitude. I am trying to merge the 2 columns into 1, separated by a comma (no spaces).
Input CSV file, first 5 rows
latitude longitude
35.1868 -106.6652
42.3688 -83.4799
40.3926 -79.9052
40.5124 -88.9883
38.5352 -90.0006
My goal is to take this CSV and create a new one with a single column with both values separated by a comma (no spaces in-between) using PowerShell. See the desired output below...
location
35.1868,-106.6652
42.3688,-83.4799
40.3926,-79.9052
40.5124,-88.9883
38.5352,-90.0006
Any help will be greatly appreciated!
The IMO easiest way is a Select-Object with a calculated property
Import-Csv .\input.csv |
Select-Object #{Name='Location';Expression={$_.latitude,$_.longitude -join ','}} |
Export-Csv .\output.csv -NoTypeInformation
> Get-Content .\output.csv
"Location"
"35.1868,-106.6652"
"42.3688,-83.4799"
"40.3926,-79.9052"
"40.5124,-88.9883"
"38.5352,-90.0006"
Edit
In case there are other columns which should not be affected by the merge,
see this modified Select-Object
Select-Object *,#{N='Location';E={$_.latitude,$_.longitude -join ','}} -Exclude latitude,longitude|
But the new column will then be the last one.
the 1st ten lines are just a way to embed sample data in a script without needing to write it to a file & then read it back in. [grin]
use Import-CSV to get the real data into the script.
# fake reading in a CSV file
# in real life, use Import-CSV
$InStuff = #'
latitude, longitude
35.1868, -106.6652
42.3688, -83.4799
40.3926, -79.9052
40.5124, -88.9883
38.5352, -90.0006
'# | ConvertFrom-Csv
$LocationList = foreach ($IS_Item in $InStuff)
{
[PSCustomObject]#{
Location = #($IS_Item.Latitude, $IS_Item.Longitude) -join ','
}
}
# on screen
$LocationList
# CSV file
$LocationList |
Export-Csv -LiteralPath "$env:TEMP\JohnnyCarino_LocationList.csv" -NoTypeInformation
screen output ...
Location
--------
35.1868,-106.6652
42.3688,-83.4799
40.3926,-79.9052
40.5124,-88.9883
38.5352,-90.0006
CSV file content ...
"Location"
"35.1868,-106.6652"
"42.3688,-83.4799"
"40.3926,-79.9052"
"40.5124,-88.9883"
"38.5352,-90.0006"

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