[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Powershell

Posted on 2016-11-21
16
Medium Priority
?
72 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 2
16 Comments
 
LVL 85

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 71

Expert Comment

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

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
Q2 2017 - Latest Malware & Internet Attacks

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out our latest Quarterly Internet Security Report!

 

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 85

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 85

Accepted Solution

by:
oBdA earned 2000 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 85

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 71

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 85

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

656 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