[英]How can I set the “Environment” > “Start the following program at logon” property on a local user using powershell?
Background:背景:
I have a server with Windows 2008 R2 installed running as a terminal server session host.我有一台安装了 Windows 2008 R2 的服务器作为终端服务器会话主机运行。 I have a long list of local users set-up and configured as remote desktop users.我有一长串本地用户设置和配置为远程桌面用户。 When the users remotely log on using remote desktop connection, a program automatically starts up.当用户使用远程桌面连接远程登录时,程序会自动启动。 When the user closes that program, the session ends.当用户关闭该程序时,会话结束。 This all works fine if I set it up manually.如果我手动设置,这一切都很好。
My Question:我的问题:
I have written a script to add a list of local users automatically and setup and configure the properties.我编写了一个脚本来自动添加本地用户列表并设置和配置属性。 The problem is that nowhere can I find how to set the "Environment" > "Start the following program at logon" properties.问题是我无处可找到如何设置“环境”>“登录时启动以下程序”属性。 (See image for the properties I want to set) (有关我要设置的属性,请参见图片)
A sample portion of my current script is as follow:我当前脚本的示例部分如下:
$computer = "localhost"
$userName = "aTestUser"
$objComputer = [ADSI]"WinNT://$computer"
$objUser = $objComputer.Create('user', $userName)
$objUser.SetPassword("Password")
$objUser.PSBase.InvokeSet('Description', "Some description for $userName")
$objUser.PSBase.InvokeSet('userflags', 512)
$objUser.PSBase.InvokeSet('passwordExpired', 1)
$objUser.SetInfo();
I also tried this command which doesn't work:我也试过这个不起作用的命令:
$objUser.PSBase.InvokeSet("TerminalServicesInitialProgram", "C:\programs\a_test_program.exe")
I have searched on Microsoft's MSDN site and Google and StackOverflow but could not find this specific property.我在 Microsoft 的 MSDN 站点以及 Google 和 StackOverflow 上进行了搜索,但找不到此特定属性。
Okay, so I finally got it working.好的,所以我终于让它工作了。 Seems like you have to first create the user then open it again for editing before the InvokeSet
sets the TerminalServicesInitialProgram
property.似乎您必须先创建用户,然后在InvokeSet
设置TerminalServicesInitialProgram
属性之前再次打开它进行编辑。
I am not sure, maybe someone can share some experience or explanation.我不确定,也许有人可以分享一些经验或解释。
Thank you to everyone for your help and assistance.感谢大家的帮助和帮助。
Working Code:工作代码:
# Read the CSV file and create the users
# The CSV file structure is:
# UserName,FullName,Description
$Users = Import-Csv -Path "C:\Users.csv"
foreach ($User in $Users)
{
# adds user
$computer = "localhost"
$username = $User.UserName
#$username = "atest001"
$fullname = $User.FullName
#$fullname = "My Name"
$description = $User.Description
#$description = "A new user description"
$password = "MyGreatUnbreakableSecretPassword"
$objComputer = [ADSI]"WinNT://$computer"
$objUser = $objComputer.Create('user', $username)
$objUser.SetPassword($password)
$objUser.PSBase.InvokeSet("Description", $description)
$objUser.PSBase.InvokeSet('userflags', 65536)
$objUser.SetInfo();
# set password not to expire
#wmic USERACCOUNT WHERE "Name = '$username'" SET Passwordexpires=FALSE
# Add to groups
$group = [ADSI]"WinNT://./Power Users,group"
$group.Add("WinNT://$username,user")
$group = [ADSI]"WinNT://./WW_Users,group"
$group.Add("WinNT://$username,user")
$ou = [adsi]"WinNT://127.0.0.1"
$user = $ou.psbase.get_children().find($username)
$user.PSBase.InvokeSet("TerminalServicesInitialProgram", "C:\Program Files (x86)\Wonderware\InTouch\view.exe c:\program files (x86)\archestra\framework\bin\sibanyegold-kdce_app_tse1_test")
$user.PSBase.InvokeSet("MaxConnectionTime", 120)
$user.PSBase.InvokeSet("MaxDisconnectionTime", 1)
$user.PSBase.InvokeSet("MaxIdleTime", 30)
$user.PSBase.InvokeSet("BrokenConnectionAction", 1)
$user.PSBase.InvokeSet("ReconnectionAction", 1)
$user.PSBase.InvokeSet("FullName", $fullname)
$user.setinfo()
}
I got the following errors when calling InvokeSet, any ideas?调用 InvokeSet 时出现以下错误,有什么想法吗?
PS C:\Users\admin> $user.PSBase.InvokeSet("TerminalServicesInitialProgram", "C:\Windows\System32\logoff.exe")
Exception calling "InvokeSet" with "2" argument(s): "Exception from HRESULT: 0x8000500D"
At line:1 char:1
+ $user.PSBase.InvokeSet("TerminalServicesInitialProgram", "C:\Windows\ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodTargetInvocation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.