My new friends Christa Anderson and Kristin L. Griffin wrote the Microsoft Windows Server 2008 R2 Remote Desktop Services Resource Kit for which they asked me to contribute a “From the Field” article that examines the PowerShell TS Module I created.
This is the code example I wrote for them. The script gets all RD Session host servers in a domain, finds all active sessions that have been running for over an hour and disconnects them in order to save server resources:
1. Shows the usage of the -WhatIf parameter, what will happen if I execute the command.
"Server1","Server2" | Foreach-Object{
Get-TSSession -ComputerName $_ -Filter {$_.IdleTime -gt '01:00:00'}
} | Disconnect-TSSession -WhatIf
What if: Performing operation "Disconnecting session id '0'" on Target "Server1".
What if: Performing operation "Disconnecting session id '2'" on Target "Server2".
2. The first command gets all RD servers from the current domain and disconnects sessions that have been idle for more than an hour. By default, Disconnect-TSSession prompts for confirmation before it disconnects the sessions. To suppress confirmations add the -Force or $Confirm:$false parameter.
Get-TSServers | Get-TSSession -Filter {$_.IdleTime -gt '01:00:00' } | `
Disconnect-TSSession -Force
- or -
Get-TSServers | Get-TSSession -Filter {$_.IdleTime -gt '01:00:00' } | `
Disconnect-TSSession -Confirm:$false
In this post I wrote about a way to modify email addresses with the Update-List cmdlet in a Exchange 2007 environment. Exchange 2010 has been released in the meantime and this method no longer works, you'll get this warning:
[PS] > Get-Mailbox User1 | Update-List -Property EmailAddresses -Add TestUser1@domain.com | Set-Mailbox
WARNING: The command completed successfully but no settings of 'domain.com/Users/TestUser1' have been modified.
Exchange 2010 uses PowerShell 2.0 Remote PowerShell sessions to manage Exchange and that changed the way objects and parameters are bind in the implementation of remote Exchange commands.
In 2010 we can use a hash table to modify the EmailAddresses property. For example, to add new SMTP address we use the 'Add' keyword:
[PS] > Set-Mailbox User1 -EmailAddresses @{Add='TestUser1@domain.com’}
The 'Add' keyword can be replaced with the '+' shortcut:
[PS] > Set-Mailbox User1 -EmailAddresses @{'+'='TestUser1@domain.com’}
To remove addresses use the 'Remove' keyword, or its '-' alias.
[PS] > Set-Mailbox User1 -EmailAddresses @{Remove='TestUser1@domain.com’}
[PS] > Set-Mailbox User1 -EmailAddresses @{'-'='TestUser1@domain.com’}
Note that if you decide to use the +/- aliases then you need to enclose them in quotes otherwise PowerShell will interpret them as the '-='/'+=' PowerShell operators.
We can also add and remove addresses in one call:
[PS] > Set-Mailbox User1 -EmailAddresses @{'Add'=@('TestUser20@domain.com','TestUser21@domain.com'); Remove='TestUser1@domain.com'}
Now, the EmailAddresses parameter is just one example, in fact you can use this method to modify any other MultiValuedProperty parameter:
[PS] > Set-RecipientFilterConfig -BlockedRecipients @{Add=@('user1@domain.com','user2@domain.com')}
[PS] > Set-TransportConfig -GenerateCopyOfDSNFor @{Add=@('5.4.7', '5.7.1');Remove= '5.7.2'}
[PS] > Get-Mailbox User2 | Set-Mailbox -RejectMessagesFrom @{Add='administrator@contoso.com'; Remove='sysadmin@contoso.com'}
[PS] > Get-Mailbox User2 | Set-Mailbox -GrantSendOnBehalfTo @{Add=('administrator@contoso.com','sysadmin@contoso.com')}
Have fun.
I have just found out that my blog has won me a free full ticket to the upcoming TechEd event in Eilat, Israel (Novemeber 28-30). I'm going to attend the event as a Microsoft guest. My blog is one (of two) most popular blogs in the past 8 months on Microsoft's Israel, w00t!
See you in the event, catch me on Twitter: @ShayLevy. THANK YOU Microsoft!
