简体   繁体   English

如何使用 powershell 在本地用户上设置“环境”>“登录时启动以下程序”属性?

[英]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 上进行了搜索,但找不到此特定属性。

I found a solution here .我在这里找到了解决方案。

$ou = [adsi]"WinNT://127.0.0.1"
$user = $ou.psbase.get_children().find("test")
$user.PSBase.InvokeSet("TerminalServicesInitialProgram", "C:\logoff.bat")
$user.setinfo()

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.

相关问题 如何使用PowerShell在IIS中停止和启动各个网站? - How can I stop and start individual websites in IIS using PowerShell? 如何使用智能卡自动登录Windows? - How can I logon to windows automatically using a smartcard? 如何使用没有管理员权限的vbs将任务设置为在登录时作为当前用户运行? - How do you set a task to run as the current user at logon using vbs without admin rights? 在Powershell中为另一个本地用户登录时触发的一次性计划任务 - One-time scheduled task that fires on logon for another local user in Powershell 如何通过PowerShell命令行将本地用户转变为管理员? - How can I turn a local user into an administrator via the PowerShell command line? 如何确定我创建的文件是否在当前用户的登录会话期间创建的? - How can I find out if a file I created was created during the current user's logon session? C ++:如何在Windows Credential Manager中以编程方式创建本地用户登录凭据,以便“ runas / savecred”可以使用它? - C++: How to programmatically create a local user logon credential in Windows Credential Manager so “runas /savecred” can use it? 如何使用注册表中的CurrentVersion \\ Run键为特定用户自动启动程序? - How do I auto-start a program for a specific user using the CurrentVersion\Run key in the registry? 如何使用远程PowerShell创建本地Windows用户帐户? - How to create a local windows user account using remote powershell? 如果使用SYSTEM用户令牌启动登录用户进程,是否会危害系统的安全性? - Do I jeopardize security of the system if I start a logon user process with the SYSTEM user token?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM