Solved

Powershell

Posted on 2016-11-21
16
45 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 83

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 68

Expert Comment

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

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
 

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 83

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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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 83

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 83

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 68

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 83

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

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
A brief introduction to what I consider to be the best editor for PowerShell.
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.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

919 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

16 Experts available now in Live!

Get 1:1 Help Now