Calling line in CSV to pipe into powershell argument

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Calling line in CSV to pipe into powershell argument

Post by jvierra »

Unfortunately your code doesn't make any sense. You aer not using the OU anywhere you are just getting a collection of objects and trying to pass them

Also all you need to do to get users is use "Get-AdUser"

Get-AdUser -Filter * -SearchBase $OU[0].RecipientOU | Set-AdUser -Add @{msExchExtensionCustomAttribute1 = $_.ExchangeDistrictCode }/b]

I suggest starting by doing a tutotial on basic PowerShell instrad of trying to guess at things. Without the basics you will likely copy bad code and try to use it as you seem to have done here.

Here is how to use a collection:

Code: Select all

import-csv c:\temp\book1.csv |
    ForEach-Object{
        Get-AdUser -Filter * -SearchBase $_.RecipientOU | 
            Set-AdUser -Add @{msExchExtensionCustomAttribute1 = $_.ExchangeDistrictCode}
        }
User avatar
Carpe_diem
Posts: 22
Last visit: Thu Feb 10, 2022 1:50 pm

Re: Calling line in CSV to pipe into powershell argument

Post by Carpe_diem »

WOW thank you for your help

I really appreciate your help and I am here to tell you that the lines in the last post work. running it, made the change on all contacts in the OU based on the value defined behind "msExchExtensionCustomAttribute1" as stated the only thing that does not work is the value added from CSV under this.

I forgot to add to the last post that I made the csv changes you hinted towards and updated the last post with it as well.

Also, I said that I am trying to change Contacts not users so I think Get-ADObject is correct., I also stated that I am trying to only filter out only Groups, Rooms and Contacts. those are the only once I am interested in change the attribute on.

the last script you posted would not incorporate the filters I was looking for

https://social.technet.microsoft.com/wi ... lters.aspx

All distribution groups (Notes 4, 15)
(&(objectCategory=group)
(!(groupType:1.2.840.113556.1.4.803:=2147483648)))
All contact objects
(objectClass=contact)
All contact objects
(objectClass=contact)
and rooms
(msExchResourceMetaData=ResourceType:Room))

I know that you have way more experience then I in Powershell, however I do find your last comment a bit condescending and don’t think that this is helping moving people along, rather the shutting them down and feeling uncomfortable to ask questions in this forum. So, your last statement (if you felt obligated to say that) should have been in a private message.

However I do see the many post you made and I thank you for all the people you help including me.

feel free to have the moderator delete the entire post.

Carpe Diem
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Calling line in CSV to pipe into powershell argument

Post by jvierra »

Sorry if you think I was being condescending but the code you posted clearly indicate a complete lack of knowledge of basic PowerShell. Without the fundamentals you will not be able to understand most of the simplest things you need to know to understand how to write code. My statement is a suggestion that would save you a lot of time and confusion.

On simple thing is how to read an error (exception). The exception is telling you something very fundamental. Knowing basic PowerShell would have led you to seeing one of the issues you face with what you have coded.

What we are doing is debugging your code one line at a time. Effectively you are asking us to help you learn the basics one line at time. Forums are not a good place to ask for individual training. It is time consuming and does not provide a good understanding of the process and discipline of coding.

You have a smattering of exposure to coding with PwoerShell. A tutorial or book would move you towards becoming a proficient PowerShell scripter. Why not take the plunge.

There are a number of excellent books that any new user should get and read. Even reading the first 3 or four chapters would be extremely enlightening to anyone with no programming training or experience.

There is also one excellent video tutorial done by the inventor of PwoerShell which is excellent in teaching new people what PowerShell is and how it is used.

https://mva.microsoft.com/en-us/trainin ... shell-8276

My favorite newest book is: https://www.manning.com/books/windows-p ... rd-edition

It is the most up to date version of the book. THe second edition is available at a huge discount from many sellers and is also an excellent choice but doesn't cover the newest features of WMF 5
This topic is 5 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked