[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

powershell what if statement displayname contains certain characters

Posted on 2014-08-28
5
Medium Priority
?
867 Views
Last Modified: 2014-08-29
I have my powershell exchange code but trying to figure out the best way to add an if statement for display names containing special characters like &($. I would like to write those to a separate csv file if possible and then the rest to another csv file.


@ (

 "microsoft.com/users/accounting"
 "microsoft.com/users/finance"
 "microsoft.com/users/computers"
      
) | % {

$stat1 = Get-mailbox -resultsize:unlimited –organizationalunit $ou_dir | foreach-object {

  $stat2 = get-mailboxstatistics $_

   
 

 

    new-object PsObject -Property @{

     PrimarySmtpAddress = $_.PrimarySmtpAddress

     "number of items"          = $stat2.ItemCount


     UseDatabaseQuotaDefaults = $_.UseDatabaseQuotaDefaults

     OU   = $_.identity

     IssueWarningQuota  = $_.IssueWarningQuota

     ProhibitSendQuota  = $_.ProhibitSendQuota

     "totalitemsize" = $stat2.TotalItemSize.Value.ToMB()

     Alias              = $_.Alias

     office              = $_.office

     Database           = $_.database

     RecipientTypeDetails       = $_.RecipientTypeDetails

     SingleItemRecoveryEnabled   = $_.SingleItemRecoveryEnabled

  }

} | select Alias, RecipientTypeDetails, Office, SingleItemRecoveryEnabled, Database, "totalitemsize", PrimarySmtpAddress, ItemCount,OU, UseDatabaseQuotaDefaults, identity, IssueWarningQuota,prohibitSendQuota   | export-csv c:\report.csv –nti
0
Comment
Question by:techdrive
  • 2
  • 2
5 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 40291872
Try..
$report = @(
"microsoft.com/users/accounting"
"microsoft.com/users/finance"
"microsoft.com/users/computers"
) | % {
Get-mailbox -resultsize:unlimited –organizationalunit $ou_dir | foreach-object {
  $stat2 = get-mailboxstatistics $_
    new-object PsObject -Property @{
     DisplayName = $_.DisplayName
		 Alias = $_.Alias
		 PrimarySmtpAddress = $_.PrimarySmtpAddress
     "number of items" = $stat2.ItemCount
     UseDatabaseQuotaDefaults = $_.UseDatabaseQuotaDefaults
     OU = $_.identity
     IssueWarningQuota = $_.IssueWarningQuota
     ProhibitSendQuota = $_.ProhibitSendQuota
     totalitemsize = $stat2.TotalItemSize.Value.ToMB()
     office = $_.office
     Database = $_.database
     RecipientTypeDetails = $_.RecipientTypeDetails
     SingleItemRecoveryEnabled = $_.SingleItemRecoveryEnabled
  }
 }
} | select DisplayName,Alias,Office,SingleItemRecoveryEnabled,Database,totalitemsize,"number of items",PrimarySmtpAddress,ItemCount,OU,UseDatabaseQuotaDefaults,IssueWarningQuota,prohibitSendQuota
$report | ?{$_.DisplayName -match "[&($]"} | export-csv c:\SpecialDisplayreport.csv –nti
$report | ?{$_.DisplayName -notmatch "[&($]"} | export-csv c:\NormalDisplayreport.csv –nti

Open in new window

If you want to match any special characters, you can try replacing "[&($]" with "[\W]"
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 40292117
The other way is to collect the data into two different vars (one for each case) in the loop, and then export those, but it doesn't make a difference in efficiency (only sketched, to emphasize the difference):
@ ( ... ) | % { ...
} | select ... |  % {
  if ($_.DisplayName -match '[&($]') { $Special += $_ } else { $Normal += $_ }
}
$Special | export-csv -NTI c:\SpecialDisplayreport.csv
$Normal | export-csv -NTI c:\NormalDisplayreport.csv

Open in new window

This way is better if you would also need to process the "special" cases further.
0
 

Author Comment

by:techdrive
ID: 40293325
one more last thing subsun. I would like for this to work with prompting for an OU please.

Add-Type -AssemblyName Microsoft.VisualBasic
$export_dir = [Microsoft.VisualBasic.Interaction]::InputBox("Please enter the complete path of the output file", "Output file",  "C:\temp")
Add-Type -AssemblyName Microsoft.VisualBasic
$onedir = [Microsoft.VisualBasic.Interaction]::InputBox("Please enter the OU path in the object field in AD", "Object", "EX: microsoft.com/users/accounting")
Get-mailbox -resultsize:unlimited –organizationalunit $onedir| foreach-object {
  $stat2 = get-mailboxstatistics $_
    new-object PsObject -Property @{
     DisplayName = $_.DisplayName
             Alias = $_.Alias
             PrimarySmtpAddress = $_.PrimarySmtpAddress
     "number of items" = $stat2.ItemCount
     UseDatabaseQuotaDefaults = $_.UseDatabaseQuotaDefaults
     OU = $_.identity
     IssueWarningQuota = $_.IssueWarningQuota
     ProhibitSendQuota = $_.ProhibitSendQuota
     totalitemsize = $stat2.TotalItemSize.Value.ToMB()
     office = $_.office
     Database = $_.database
     RecipientTypeDetails = $_.RecipientTypeDetails
     SingleItemRecoveryEnabled = $_.SingleItemRecoveryEnabled
  }
 
} | select DisplayName,Alias,Office,SingleItemRecoveryEnabled,Database,totalitemsize,"number of items",PrimarySmtpAddress,ItemCount,OU,UseDatabaseQuotaDefaults,IssueWarningQuota,prohibitSendQuota
$report | ?{$_.DisplayName -match "[^(|]"} | export-csv c:\SpecialDisplayreport.csv –nti
$report | ?{$_.DisplayName -notmatch "[^(|]"} | export-csv c:\NormalDisplayreport.csv –nti
0
 
LVL 40

Accepted Solution

by:
Subsun earned 2000 total points
ID: 40293460
Try..
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$ou_dir = [Microsoft.VisualBasic.Interaction]::InputBox("Please enter the OU path in the object field in AD")

$report = Get-mailbox -resultsize:unlimited –organizationalunit $ou_dir | foreach-object {
  $stat2 = get-mailboxstatistics $_
    new-object PsObject -Property @{
     DisplayName = $_.DisplayName
		 Alias = $_.Alias
		 PrimarySmtpAddress = $_.PrimarySmtpAddress
     "number of items" = $stat2.ItemCount
     UseDatabaseQuotaDefaults = $_.UseDatabaseQuotaDefaults
     OU = $_.identity
     IssueWarningQuota = $_.IssueWarningQuota
     ProhibitSendQuota = $_.ProhibitSendQuota
     totalitemsize = $stat2.TotalItemSize.Value.ToMB()
     office = $_.office
     Database = $_.database
     RecipientTypeDetails = $_.RecipientTypeDetails
     SingleItemRecoveryEnabled = $_.SingleItemRecoveryEnabled
  }
}| select DisplayName,Alias,Office,SingleItemRecoveryEnabled,Database,totalitemsize,"number of items",PrimarySmtpAddress,ItemCount,OU,UseDatabaseQuotaDefaults,IssueWarningQuota,prohibitSendQuota

$report | ?{$_.DisplayName -match "[&($]"} | export-csv c:\SpecialDisplayreport.csv –nti
$report | ?{$_.DisplayName -notmatch "[&($]"} | export-csv c:\NormalDisplayreport.csv –nti

Open in new window

0
 

Author Comment

by:techdrive
ID: 40293710
thanks
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Exchange administrators are always vigilant about Exchange crashes and disasters that are possible any time. It is quite essential to identify the symptoms of a possible Exchange issue and be prepared with a proper recovery plan. There are multiple…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

868 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