Powershell - write User to CSV, explicitly exclude part of the User from any action
Hi,
In the Script posted below i marked the steps where i need help e.g. ### Question No. 1###
1. What is the best way to write only the USERIDs of the DB "*WEB*" to an
external CSV at this step?
2. Is there a way to exclude the User which are stored in Database (*WEB*) from the action should taken below, after identify them and write to an external file
or must be this user explicitly exclude from any action before it should taken?
Firstly, the code you posted doesn't look like it integrates the corrections several Experts (including me) suggested already. E.g. the "LGA*" switch statement cannot work.
Each switch condition is processed, even if a prior case was hit. To prevent from that, end the code of each case with a break. That makes the switch work more like a cascaded IF.
Mandy_
ASKER
Hi to everyone, hi qlemo
First a big thank you to all the people who have helped me so far.
I'm sorry that i'm not able to integrate all requirements from the beginning.
The complete task is very complex and there are many variations to observed.
I checked every time at home in my virtual environment with exchange2013
every variations of user , databases, departments, password flags, group
membership and and and... You know its not easy to observe everything
at one time. I like a product at the end that makes almost everything
automatically. I dont like to spend a lot of time to fix problems gonna
happen in the live environment. I'm working for a known company
I have to anonymize my code and cannot use realnames of company, groups , user and
so on. For this it could happen that i'm writing one time LCA or DKH or
what else. Names are not important. The error occurs with that i can remove myself.
Pls just pay attention for the questions i'm asking. That are not questions to
fix the complete code itself i need to optimize it with new requirements
as a result of checking and testing.
Ok the switch statement it's already checked and its working. Above i forgot to use
the switch -wildcard ($user.company){ parameter, but thats not my question.
I'm getting always the solution which provide first to me and build in after checking
in test environment. One time the expert resolve my last question not answering
for my next question. What should i do? Private contact as far as i know here
not allowed. I can understand if different handwriting fonts mess up the final result.
Too many cooks spoil the porridge :)
Just pay attention for the questions i'm exactly asking then everything will be fine
Thanks to all again
Mandy
Qlemo
I understand well that it is not easy for you to provide obfuscated code. Besides, that is not important.
The issue with the code is that we need to understand what it should do, to get the "big picture". So without understanding the code (and that is for sure - I do not understand it), I have posted a recommendation for the second question: use break in each case statement block.
Another recommendation to break that lengthy code into managable parts is to use functions for each task. That way the outer switch is visually protruding, the details are a black box, and we do not need to understand their purpose fully. That has also the advantage that you can test much more easily.
I do not understand your first question - the code seems to do that already?!
First question: yes i did it already but is it the best way to do that, if i like only
the Userid to an external file?
2nd: Maybe its the better way to put it to my 1st step. At this step i'm converting my
CSV and check the output. I could check also for database web user and just remove that
lines from the CSV. Here's my first step and i tried to implement that.
(import-csv -delimiter ';' c:\import.csv | convertto-csv -notype ) -replace "\uFEFF" | out-file c:\export.csv -encoding utf8ForEach ($User in Import-Csv "c:\export.csv"){$user}foreach ($line in $user) {$user_db = (Get-Mailbox -Identity $user.userid).Database($user_db -like "*WEB*" | Out-File "c:\Temp\webUser.csv") #writing only user.id with column useridForEach ($User in Import-Csv "c:\Temp\webUser.csv"foreach ($line in $user) {$userid = (Get-Mailbox -Identity $user.userid).alias(Get-Content "c:\export.csv") -notmatch $userid | Out-File "c:\export2.csv"
I did not understand "break in each case statement block"
I'm not expert in powershell but if it to difficult i should accept the error messages
i'm getting if the script try to enable an existing mailbox of this webuser,
but also the custom attributes will be change, an email address will be set
and a password reset will be done.
What you think? I think then its the better way to remove them from the CSV
before. Pls take a look to my code above.
thank you. thats the one. Yes you're right. The -append not working with powershell 2.0
I dont know if the out-file command do it. But what can i use instead in earlier ps versions?
Each switch condition is processed, even if a prior case was hit. To prevent from that, end the code of each case with a break. That makes the switch work more like a cascaded IF.