Solved

Powershell

Posted on 2016-11-21
16
63 Views
Last Modified: 2016-11-23
Input is provided to script during run time, and the script should determine if the input is a display name or email address or alias
0
Comment
Question by:Exchange Admin
  • 7
  • 6
  • 2
16 Comments
 
LVL 84

Expert Comment

by:oBdA
ID: 41897084
Please provide some more information what it is exactly you want to do.
Well, for an email address, you can check whether the string contains a "@" and at least one dot behind it.
But how do you define "alias", and how can you differentiate it from from a "display name" in your scenario?
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 41897145
Do you want to check against Active Directory entries to validate the input?
0
 
LVL 84

Expert Comment

by:oBdA
ID: 41897160
Sindhura, please reply in your question, not as messages.

Thanks for your quick response. Let's say, an input can be determined as      

      email address if it contains "@" and ".com"
      alias if it contains "." like first name.last name
      display name if it contains "," like lastname, first name

As a quick draft (the regexes could be refined) to check if this resembles what you need:
$Input = Read-Host -Prompt "Input"
Switch -regex ($Input) {
	'.+@.+\.com' {$Type = 'Email'; Break}
	'.+,\s*.+' {$Type = 'Display'; Break}
	'.+\..+' {$Type = 'Alias'; Break}
	default  {$Type = 'Unknown'; Break}
}
Write-Host "Type: $($Type)"

Open in new window

0
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 

Author Comment

by:Exchange Admin
ID: 41897168
Thanks a lot, it works in desired manner
0
 

Author Comment

by:Exchange Admin
ID: 41897179
Could you please explain me the below expressions. I do understand that "," and "." are used here to identify the inputs as display name and alias respectively.

'.+,\s*.+' {$Type = 'Display'; Break}
'.+\..+' {$Type = 'Alias'; Break}
0
 
LVL 84

Expert Comment

by:oBdA
ID: 41897190
These are "Regular Expressions" (see, for example, http://www.regular-expressions.info/quickstart.html ).
A single "." is any character, the "+" behind it means at least one, "\s" is a space the "*" behind it means 0 or any number of repetitions.
The escaped dot, "\.", is a literal ".".
0
 

Author Comment

by:Exchange Admin
ID: 41897319
I have gone through the provided article and got a clear understanding of regular expressions. Checked them with few test expressions as well.

I am currently working on a powershell script which should meet the below requirements:

                       Input is a file which consists of a complete list of email addresses or alias or display name
                       Script should identify if the input is email addresses or alias or display name
                       Script should prompt user to select output which could be any attribute of the command "get-mailbox -identity <>"
                       User enters the attributes names which he/she desires and it could be 1, 2 or many attributes
                       If mailbox i.e., input doesn't exist, then the attribute result should be "Not Found"

I have worked on the same scenario with single user as input:

$UserInputName = Read-Host "`t`tPlease enter the user"
$AttributeInput = Read-Host "`t`tPlease enter the required attribute"
get-mailbox -identity $UserInputName | select $AttributeInput
0
 

Author Comment

by:Exchange Admin
ID: 41897332
Could you please assist me on the last 2 steps of script:

                       1. Input is a file which consists of a complete list of email addresses or alias or display name
                       2. Script should identify if the input is email addresses or alias or display name
                       3. Script should prompt user to select output which could be any attribute of the command "get-mailbox -identity <> | fl"
                       4. User enters the attributes names which he/she desires and it could be 1, 2 or many attributes
                       5. If mailbox i.e., input doesn't exist, then the attribute result should be "Not Found"
0
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 41897409
Can't test the Get-Mailbox part, but this will turn a string with a comma separated list into an array of properties:
$UserInput = Read-Host "`tPlease enter the user"
$AttributeInput = Read-Host "`t`Please enter the required attribute (a comma separated list is possible)`r`n`t"
Switch -regex ($UserInput) {
	'.+@.+\.com' {$Type = 'Email'; Break}
	'.+,\s*.+' {$Type = 'Display'; Break}
	'.+\..+' {$Type = 'Alias'; Break}
	default  {$Type = 'Unknown'; Break}
}
$AttributeList = $AttributeInput.Split(',') | ForEach-Object {$_.Trim()}
Write-Host "Type: $($Type)"
Write-Host "Attributes:"
$AttributeList | ForEach-Object {Write-Host "`t - $($_)"}
Get-Mailbox -Identity $UserInputName | Select-Object $AttributeList

Open in new window

0
 

Author Comment

by:Exchange Admin
ID: 41897426
Input here will be a file which consists f number of users
And the desired output is any of the attributes of the command "get-mailbox -identity <> | fl"
0
 
LVL 84

Expert Comment

by:oBdA
ID: 41897469
As before, can't test the Get-Mailbox part, but it works with Get-ADUser.
$UserFile = 'C:\Temp\Users.txt'
$OutputFile = 'C:\Temp\Export.csv'
$AttributeInput = Read-Host "`t`Please enter the required attribute(s) (a comma separated list is possible)`r`n`t"
$AttributeList = @($AttributeInput.Split(',') | ForEach-Object {$_.Trim()})
Write-Host "Attributes:"
$AttributeList | ForEach-Object {Write-Host "`t - $($_)"}
Get-Content -Path $UserFile | ForEach-Object {
	$Identifier = $_
	Write-Host "Processing '$($Identifier)' ..." -NoNewline
	Switch -regex ($Identifier) {
		'.+@.+\.com' {$Type = 'Email'; Break}
		'.+,\s*.+' {$Type = 'Display'; Break}
		'.+\..+' {$Type = 'Alias'; Break}
		default  {$Type = 'Unknown'; Break}
	}
	Write-Host " type is $($Type)."
	Try {
		$Mailbox = Get-Mailbox -Identity $Identifier -ErrorAction Stop
	} Catch {
		$_.Exception.Message | Write-Host -Fore Red
		$Mailbox = '' | Select-Object -Property $AttributeList
		$AttributeList | ForEach-Object {$Mailbox.$_ = 'n/a'}
	}
	$Mailbox | Select-Object (@(@{Name='Identifier'; Expression={$Identifier}}) + $AttributeList)
} | Export-Csv -NoTypeInformation -Path $OutputFile

Open in new window

0
 
LVL 69

Expert Comment

by:Qlemo
ID: 41897546
Sin Ga, I'm not amused about https://www.experts-exchange.com/questions/28980973/Powershell-script.html - which is a very similar question, and I've supplied you with a comfortable attribute choice there. The question is still open.

I cannot see why determining which kind of data has been supplied has any relevance but for display, so strictly seen this two questions are duplicates.
0
 

Author Comment

by:Exchange Admin
ID: 41898975
I am really sorry, I did not check your comment over there. If I have seen, I would have replied there itself. Since the requirements of the script has changed, I have created a new post. I am really grateful for your assistance on this.

      I believe, it has to be Write-Host "$AttributeList" instead of Write-Host "Attributes:" in the script so that is displays the attributes selected by the user.

Thanks a lot again and so sorry for the double posts.
0
 

Author Closing Comment

by:Exchange Admin
ID: 41898992
Best solution
0
 
LVL 84

Expert Comment

by:oBdA
ID: 41899035
I believe, it has to be Write-Host "$AttributeList" instead of Write-Host "Attributes:" in the script so that is displays the attributes selected by the user.
No. That line is just decoration. The attributes identified will be listed below that line, each attribute in its own line, with the next line of code:
$AttributeList | ForEach-Object {Write-Host "`t - $($_)"}

Open in new window

0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question