Solved

Powershell

Posted on 2016-11-21
16
30 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 82

Expert Comment

by:oBdA
Comment Utility
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 68

Expert Comment

by:Qlemo
Comment Utility
Do you want to check against Active Directory entries to validate the input?
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
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
 

Author Comment

by:Exchange Admin
Comment Utility
Thanks a lot, it works in desired manner
0
 

Author Comment

by:Exchange Admin
Comment Utility
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 82

Expert Comment

by:oBdA
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Exchange Admin
Comment Utility
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 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
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
Comment Utility
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 82

Expert Comment

by:oBdA
Comment Utility
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 68

Expert Comment

by:Qlemo
Comment Utility
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
Comment Utility
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
Comment Utility
Best solution
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now