help with Get-Mailbox methods

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 15 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
jadamski
Posts: 32
Last visit: Tue Sep 22, 2009 12:32 am

help with Get-Mailbox methods

Post by jadamski »

I trying to write a script that will set or unset the ForwardingAddress on a mailbox based on a flag in a database for a select group of email addresses.

And example of this is most faculty are not on campus during the summer and we would want their email to be forward to the division secretary during this time and then turned off shortly before term start.

I did a get-member on get-mailbox and there are two methods that look like what I want to do (get_ForwardingAddress, set_ForwardingAddress). My problem is I am having brain freeze on how to use the methods in a powershell script.

I was thinking I would do something like this in the script <in pseudo-code>


get list from database with flags of the email-mailboxes
foreach email-mailboxes
if start_forward_flag = Y
if get-forwardingaddress not filled in set-forwardingaddress
if stop_forward_flag = Y
if get_forwardingaddress not blank set-forwardingaddress
end foreach

But not sure how to code using of the method get-forwardingaddress or set-forwardingaddress. Anyone wish to enlighten me?

John
User avatar
jadamski
Posts: 32
Last visit: Tue Sep 22, 2009 12:32 am

help with Get-Mailbox methods

Post by jadamski »

Ok, I need you experts help again to bail me out of my own mess I made. Below is a script that reads a Informix table gathering a group of users I need to potentially update where their email is fowarded or not.
I thought I knew what I was doing before I added the set-mailbox code. Now I get errors and not sure what I'm doing wrong.
## function to read Informix database #function Get-OleTable( [string]$sqlText, [string]$connectString) { #$da = New-Object System.Data.OleDb.OleDbConnection $da = new-object System.Data.OleDb.OleDbDataAdapter $ds = new-object System.Data.DataSet $conn = new-object System.Data.OleDb.OleDbConnection($connectString) $conn.Open() $cmd = new-object System.Data.OleDb.OleDbCommand($sqlText,$conn) $cmd.CommandType = "Text" $da.SelectCommand = $cmd $ret=$da.Fill($ds) $ds.Tables[0]}
##enter sql need to retreive desired records from database#$sql = 'select username, active_email, effective_date from gu_dl_fac_table d, gu_login_rec l where d.id=l.id'
##the communication string needed to connect to Informix#$cs = "Provider=IFXOLEDBC.2;Password=xxxx;User ID=xxxx;Data Source=xxxx@xxxx;Persist Security Info=true"
##retrieve data from database#$table = Get-OleTable $sql $cs
## foreach record if active_email=Y and DeliverToMailboxAndForward true set to false# else if active_email=N and DeliverToMailboxAndForward false set to true and forwarding email to user@domain.com#foreach ($id in $table){ if ($id.active_email = "Y") { Get-Mailbox -Identity $id.username | where {$_.delivertomailboxandforward -eq $true} | Set-Mailbox -DeliverToMailboxAndForward $false -WhatIf } if ($id.active_email = "N") { Get-Mailbox -Identity $id.username | where {$_.delivertomailboxandforward -eq $false} | Set-Mailbox -DeliverToMailboxAndForward $true -ForwardingAddress USER@domain.com -WhatIf }}

When I run it I get a bunch of these errors:
ERROR: + Get-Mailbox <<<< -Identity $id.username | where {$_.delivertomailboxandforward -eq $true} | Set-Mailbox -DeliverToMailboxAndForward $false -WhatIfERROR: Get-Mailbox : The operation could not be performed because object 'dickey ' could not be found on domain controller 'SERVER.domain.com'.ERROR: At line:26 char:16ERROR: + Get-Mailbox <<<< -Identity $id.username | where {$_.delivertomailboxandforward -eq $false} | Set-Mailbox -DeliverToMailboxAndForward $true -ForwardingAddress USER@domain.com -WhatIfERROR: Get-Mailbox : Value of '-1' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.ERROR: Parameter name: ValueERROR: At line:26 char:16
User avatar
jhicks
Posts: 1789
Last visit: Mon Oct 19, 2015 9:21 am

help with Get-Mailbox methods

Post by jhicks »

Is $id just a string like jeff? If it is the account name or samaccountname I think you can just do get-mailbox -identity $id
User avatar
jadamski
Posts: 32
Last visit: Tue Sep 22, 2009 12:32 am

help with Get-Mailbox methods

Post by jadamski »

Ok took a different approach and seems it is get-mail I'm messing up on. When I run this code I get the following error for each record I get from the database:
Name Alias ServerName ProhibitSendQuota---- ----- ---------- -----------------Linda Armstrong LINDAA athena unlimitedWARNING: An unexpected error has occurred and a Watson dump is being generated: Value of '-1' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.Parameter name: ValueERROR: Get-Mailbox : Value of '-1' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.ERROR: Parameter name: ValueERROR: At line:20 char:59ERROR: + $table | where {$_.active_email -eq 'Y'} | % { Get-Mailbox <<<< -Identity $_.alias }
Code I am using:
function Get-OleTable( [string]$sqlText, [string]$connectString) { #$da = New-Object System.Data.OleDb.OleDbConnection $da = new-object System.Data.OleDb.OleDbDataAdapter $ds = new-object System.Data.DataSet $conn = new-object System.Data.OleDb.OleDbConnection($connectString) $conn.Open() $cmd = new-object System.Data.OleDb.OleDbCommand($sqlText,$conn) $cmd.CommandType = "Text" $da.SelectCommand = $cmd $ret=$da.Fill($ds) $ds.Tables[0]}
$sql = 'select trim(username) alias, active_email, effective_date from gu_dl_fac_table d, gu_login_rec l where d.id=l.id'
$cs = "Provider=IFXOLEDBC.2;Password=xxxx;User ID=xxxx;Data Source=xxxx@xxxx;Persist Security Info=true"
$table = Get-OleTable $sql $cs
$table | where {$_.active_email -eq 'Y'} | % { Get-Mailbox -Identity $_.alias } jadamski2008-07-23 19:46:18
User avatar
jadamski
Posts: 32
Last visit: Tue Sep 22, 2009 12:32 am

help with Get-Mailbox methods

Post by jadamski »

I changed the last line to:
$table | where {$_.active_email -eq 'Y'} | ft
and get this list - they all look ok to me.
alias active_email effective_date----- ------------ --------------lindaa Y 7/9/2008 12:00:00 AMktate Y 7/9/2008 12:00:00 AMjbbradle Y 7/9/2008 12:00:00 AMmaccarty Y 7/9/2008 12:00:00 AMhadleybb Y 7/9/2008 12:00:00 AMbinnicke Y 7/9/2008 12:00:00 AMcleeton Y 7/9/2008 12:00:00 AMgedwards Y 7/9/2008 12:00:00 AMselvidge Y 7/9/2008 12:00:00 AMjmassey Y 7/9/2008 12:00:00 AMkgrazian Y 7/9/2008 12:00:00 AMgunzenha Y 7/9/2008 12:00:00 AMhalsneba Y 7/9/2008 12:00:00 AMwham Y 7/9/2008 12:00:00 AMiorg Y 7/9/2008 12:00:00 AMjajames Y 7/9/2008 12:00:00 AMkwapy Y 7/9/2008 12:00:00 AMjamison Y 7/9/2008 12:00:00 AMrlindgre Y 7/9/2008 12:00:00 AMmarvil Y 7/9/2008 12:00:00 AMshmclaug Y 7/9/2008 12:00:00 AMjmcmurry Y 7/9/2008 12:00:00 AMmcnitt Y 7/9/2008 12:00:00 AMrmillard Y 7/9/2008 12:00:00 AMscheira Y 7/9/2008 12:00:00 AMrwshaw Y 7/9/2008 12:00:00 AMsctate Y 7/9/2008 12:00:00 AMudelhofe Y 7/9/2008 12:00:00 AMwatley Y 7/9/2008 12:00:00 AMlhopkins Y 7/9/2008 12:00:00 AMrotto Y 7/9/2008 12:00:00 AMbartholo Y 7/9/2008 12:00:00 AMmnaylor Y 7/9/2008 12:00:00 AMmcnaylor Y 7/9/2008 12:00:00 AMdewilkin Y 7/9/2008 12:00:00 AMhalupa Y 7/9/2008 12:00:00 AMrlpierce Y 7/9/2008 12:00:00 AMstking Y 7/9/2008 12:00:00 AMjguenthe Y 7/9/2008 12:00:00 AMmarti Y 7/9/2008 12:00:00 AMzeiglerp Y 7/9/2008 12:00:00 AMjlcarr Y 7/9/2008 12:00:00 AMgriffinm Y 7/9/2008 12:00:00 AMpvu Y 7/9/2008 12:00:00 AMsmckenna Y 7/9/2008 12:00:00 AMholness Y 7/9/2008 12:00:00 AMcalloway Y 7/9/2008 12:00:00 AMsgriswol Y 7/9/2008 12:00:00 AMwsummers Y 7/9/2008 12:00:00 AMjrsmith Y 7/9/2008 12:00:00 AMpatwhite Y 7/9/2008 12:00:00 AMmcnamara Y 7/9/2008 12:00:00 AMlindad Y 7/9/2008 12:00:00 AMdevonis Y 7/9/2008 12:00:00 AMjrharris Y 7/9/2008 12:00:00 AMmalek Y 7/9/2008 12:00:00 AMkarenf Y 7/9/2008 12:00:00 AMkasalchr Y 7/9/2008 12:00:00 AMgreennig Y 7/9/2008 12:00:00 AMjavicker Y 7/9/2008 12:00:00 AMiedwards Y 7/9/2008 12:00:00 AMlandolf Y 7/9/2008 12:00:00 AMjharkey Y 7/9/2008 12:00:00 AMadu Y 7/9/2008 12:00:00 AMjegordon Y 7/9/2008 12:00:00 AMjlhoffma Y 7/9/2008 12:00:00 AMjlhall Y 7/9/2008 12:00:00 AMbelrose Y 7/9/2008 12:00:00 AMrrogers Y 7/9/2008 12:00:00 AMjlambert Y 7/9/2008 12:00:00 AMperz Y 7/9/2008 12:00:00 AMcpatton Y 7/9/2008 12:00:00 AMtonga Y 7/9/2008 12:00:00 AMfahey Y 7/9/2008 12:00:00 AMjlhill Y 7/9/2008 12:00:00 AMparke Y 7/9/2008 12:00:00 AMjeffreys Y 7/9/2008 12:00:00 AMkmorrow Y 7/9/2008 12:00:00 AMdarboe Y 7/9/2008 12:00:00 AMpircher Y 7/9/2008 12:00:00 AMcmcdole Y 7/9/2008 12:00:00 AMspeer Y 7/9/2008 12:00:00 AMnucciole Y 7/9/2008 12:00:00 AMwickersh Y 7/9/2008 12:00:00 AMsredl Y 7/9/2008 12:00:00 AMbjsimpso Y 7/9/2008 12:00:00 AMjathomas Y 7/9/2008 12:00:00 AMnlindsay Y 7/9/2008 12:00:00 AMdrowe Y 7/9/2008 12:00:00 AMjeanae Y 7/9/2008 12:00:00 AMrdparker Y 7/9/2008 12:00:00 AM

*** PowerShell Script finished. ***
If I put the line back to the original I get the error on each line.

Name Alias ServerName ProhibitSendQuota---- ----- ---------- -----------------Linda Armstrong LINDAA athena unlimitedWARNING: An unexpected error has occurred and a Watson dump is being generated: Value of '-1' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.Parameter name: ValueERROR: Get-Mailbox : Value of '-1' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.ERROR: Parameter name: ValueERROR: At line:20 char:59ERROR: + $table | where {$_.active_email -eq 'Y'} | % { Get-Mailbox <<<< -Identity $_.alias }ktate ktate hermes unlimitedWARNING: An unexpected error has occurred and a Watson dump is being generated: Value of '-1' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.Parameter name: ValueERROR: Get-Mailbox : Value of '-1' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.ERROR: Parameter name: ValueERROR: At line:20 char:59ERROR: + $table | where {$_.active_email -eq 'Y'} | % { Get-Mailbox <<<< -Identity $_.alias }jbbradle jbbradle athena unlimitedWARNING: An unexpected error has occurred and a Watson dump is being generated: Value of '-1' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.Parameter name: ValueERROR: Get-Mailbox : Value of '-1' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.ERROR: Parameter name: ValueERROR: At line:20 char:59ERROR: + $table | where {$_.active_email -eq 'Y'} | % { Get-Mailbox <<<< -Identity $_.alias }maccarty maccarty owlery unlimitedWARNING: An unexpected error has occurred and a Watson dump is being generated: Value of '-1' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.Parameter name: ValueERROR: Get-Mailbox : Value of '-1' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.ERROR: Parameter name: ValueERROR: At line:20 char:59ERROR: + $table | where {$_.active_email -eq 'Y'} | % { Get-Mailbox <<<< -Identity $_.alias }Hadley Belcher hadleybb hermes unlimitedWARNING: An unexpected error has occurred and a Watson dump is being generated: Value of '-1' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.Parameter name: ValueERROR: Get-Mailbox : Value of '-1' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.ERROR: Parameter name: ValueERROR: At line:20 char:59ERROR: + $table | where {$_.active_email -eq 'Y'} | % { Get-Mailbox <<<< -Identity $_.alias }Paul Binnicker binnicke hermes unlimitedWARNING: An unexpected error has occurred and a Watson dump is being generated: Value of '-1' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.Parameter name: ValueERROR: Get-Mailbox : Value of '-1' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.ERROR: Parameter name: ValueERROR: At line:20 char:59ERROR: + $table | where {$_.active_email -eq 'Y'} | % { Get-Mailbox <<<< -Identity $_.alias }cleeton cleeton hermes unlimitedWARNING: An unexpected error has occurred and a Watson dump is being generated: Value of '-1' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.Parameter name: ValueERROR: Get-Mailbox : Value of '-1' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.ERROR: Parameter name: ValueERROR: At line:20 char:59ERROR: + $table | where {$_.active_email -eq 'Y'} | % { Get-Mailbox <<<< -Identity $_.alias }
......etc, etc, etc, etc, etc
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

help with Get-Mailbox methods

Post by jvierra »

The return type of "alias" is probably not a string. I would bet that it is a char array. You need to convert it. It may also be unicode and may need to be converted. Much of this would depend on teh installation of you remail system. Use "GM" on "$table[0]" to check object types.
User avatar
jadamski
Posts: 32
Last visit: Tue Sep 22, 2009 12:32 am

help with Get-Mailbox methods

Post by jadamski »

-Identity <MailboxIdParameter> The Identity parameter identifies the mailbox. You can use one of the following values: * GUID * Distinguished name (DN) * DomainAccount * User principal name (UPN) * LegacyExchangeDN * SmtpAddress * Alias
Required? false Position? 1 Default value Accept pipeline input? True Accept wildcard characters? true
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

help with Get-Mailbox methods

Post by jvierra »

Try this:

$alias = "some alias" # replace with valid alias
Get-Mailbox -Identity $alias

If you are running your code from a file or from a cut and paste tehn try just typing it inas the file may have bad chars in it.

If the above test works then yuor code should work. If it doesn't then you may have a bug that would need to be reported to MS through the Exchange user forum.

Do a few more tests to see if we can't scope out what is going wrong.

Unfortuantely I don't have Exchange 2007 installed anywhere at this time. Corps are very slow on upgrades.


User avatar
jadamski
Posts: 32
Last visit: Tue Sep 22, 2009 12:32 am

help with Get-Mailbox methods

Post by jadamski »

PS H:> $alias = "adamski" # replace with valid aliasPS H:> Get-Mailbox -Identity $alias
Name Alias ServerName ProhibitSendQuota---- ----- ---------- -----------------John Adamski adamski owlery unlimited

bummer that works, that means somehow the data from the database is not really a string. oh poooh guess ack to drawing board to get the data in a usuable format.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

help with Get-Mailbox methods

Post by jvierra »

If that fails then try this:

$table | % { $_.alias }

If this doesn't work then something is wrong with you table builder code.
This topic is 15 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked