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
Solved

Powershell

Posted on 2016-11-21
16
61 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
A procedure for exporting installed hotfix details of remote computers using powershell
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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