PowerShell Script to Create GPO that sets Remote Desktop Session Time Limits

Hey guys,

I just started working for a new company that does a lot of  Windows Server automated builds.  As you can probably guess, a lot of PowerShell scripting is involved which I have very very basic knowledge of (basically 0 knowledge).  Anyways I was given the task of creating this PS script to get my feet wet using PowerShell scripting.

We want the PS script to be able to create a GPO that changes the "Session Time Limits" settings.  

I found a very good article as a reference in creating GPOs using PowerShell.  However the example they use in the article is on how to change the "Authoritative Time Server."  I want to be able to change the "Session Time Limits."

Here's a link to the article (looks better in IE): http://carlwebster.com/creating-a-group-policy-using-microsoft-powershell-to-configure-the-authoritative-time-server/

I think I've followed the article's steps pretty well up until the part where I need to set the registry keys.

Can someone assist me or point me in the right direction in creating a scripts similar to this?  I would greatly appreciate any help.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Adam BrownSr Solutions ArchitectCommented:
There are a few settings under the Session Time Limits section of a GPO (info: https://technet.microsoft.com/en-us/library/Cc726057%28v=WS.10%29.aspx )

Those GPO entries reference specific Registry entries under HKLM\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services, which is what you would set your GPO to modify with the script you're using. The values you need to add with your GPO are as follows:

Set time limit for active but idle Terminal Services sessions:
The value should be how long (in 1/1000s of a second) to keep the session open after a disconnect. IE, for 5 minutes, set the value to 300000.

Set time limit for disconnected sessions
The value should be how long (in 1/1000s of a second) to keep the session open after a disconnect. IE, for 5 minutes, set the value to 300000.

Terminate session when time limits are reached
Set 1 to disconnect sessions after time limit expires
set 0 to ignore time limits

Set time limit for active Terminal Services sessions
The value should be how long (in 1/1000s of a second) to keep the session open after a disconnect. IE, for 5 minutes, set the value to 300000.

Now, with all of that said, the method you're using to automate GPO creation is unnecessarily complex. And easier method would be to create GPOs in the GUI and set the actual policies in GPMC, then use powershell to Export the GPOs to XML files. From there, you can store the XML files for use in deploying your servers, and use the import-gpo cmdlet to import the GPO to the new environments that you're deploying.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
emag50Author Commented:
It does seem much easier to accomplish this using your suggested export import method using PowerShell.  Makes perfect sense as a simplified solution.  I'll go ahead and try this on the test environments.  Thanks a lot for your input.  Once I finish I'll post another comment.
emag50Author Commented:
Perfect!  Used the proper PowerShell cmdlets to export and import from one environment to another.  Now adding this to the auto build should be easy.  Thank you so much acbrown2010!
Sam KCommented:
Hi emag50,

Would you please be able to share the powershell script you created with me? My email is s1.k4@outlook.com.

Thanking you in advance.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.