简体   繁体   English

使用Powershell在“环境”选项卡中为终端服务器客户端分配登录脚本

[英]Assign logon script in Environment Tab for terminal server client using Powershell

Hi I am looking to set the logon script parameter for a user profile, using Powershell. 嗨,我想使用Powershell为用户配置文件设置登录脚本参数。 I was planning to use WMIC USERACCOUNT to do this but found that it is not possible. 我打算使用WMIC USERACCOUNT来执行此操作,但是发现这是不可能的。 As shown below the method does not exist in the method: 如下所示,该方法不存在于方法中:

    class Win32_UserAccount : Win32_Account
{
  uint32   AccountType;
  string   Caption;
  string   Description;
  boolean  Disabled;
  string   Domain;
  string   FullName;
  datetime InstallDate;
  boolean  LocalAccount;
  boolean  Lockout;
  string   Name;
  boolean  PasswordChangeable;
  boolean  PasswordExpires;
  boolean  PasswordRequired;
  string   SID;
  uint8    SIDType;
  string   Status;
};

I would prefer to do this as a statement in powershell but if that is not possible it could be done as a script I am looking to set the parameter shown in picture, for a Win Server 2008 R2 我希望将其作为Powershell中的语句来执行,但是如果不可能,可以将其作为脚本来完成,我希望为Win Server 2008 R2设置如图所示的参数 这个参数

That setting is maintained through Group Policy, reference: Specify a Program to Start Automatically When a User Logs On . 该设置通过组策略维护,请参考: 指定当用户登录时自动启动的程序 Group Policy settings are ultimately handled by registry settings. 组策略设置最终由注册表设置处理。 The Group Policy Settings Reference for Windows and Windows Server might help you find what registry settings to change, but I didn't have luck finding it there. Windows和Windows Server组策略设置参考可能会帮助您找到要更改的注册表设置,但是我在这里找不到运气。 You'll note that a lot of the settings are HKCU which means they can only be set when the user is logged in. That may be problematic for you. 您会注意到,很多设置都是HKCU,这意味着只能在用户登录时进行设置。这可能对您造成问题。 The page, Windows Program Automatic Startup Locations , is a good reference on all the places in the registry that you can set a program to start. Windows程序自动启动位置页面是您可以将程序设置为启动的注册表中所有位置的良好参考。

I'd personally recommend using schtasks to do this instead. 我个人建议使用schtasks代替。 Here's an example that creates one in cmd or PowerShell: 这是在cmd或PowerShell中创建一个的示例:

schtasks -create -tn "Run command prompt" -tr "C:\WINDOWS\system32\cmd.exe" -sc ONLOGON

It took a long time but finally got the answer the trick was to use IADsTSUserEx . 花费了很长时间,但最终得到了答案,那就是使用IADsTSUserEx I also tried to use ADSI but could only get it to set a logon script for logging on localy. 我也尝试使用ADSI,但只能获取它来设置登录脚本以登录本地。 See other post . 参见其他帖子 Here is the code plus for Elijiah how to set environment varibles of local users through the registry 这是Elijiah的代码以及如何通过注册表设置本地用户的环境变量

# adds user
$objComputer = [ADSI]"WinNT://127.0.0.1"
$objUser = $objComputer.Create('user', $username)
$objUser.SetPassword($password)
$objUser.PSBase.InvokeSet('Description', "user " + $userName)
$objUser.PSBase.InvokeSet('userflags', 512)
$objUser.SetInfo();
# set password not to expire
wmic USERACCOUNT WHERE "Name = '$username'" SET Passwordexpires=FALSE
#set logoff script
$ou = [adsi]"WinNT://127.0.0.1"
$user = $ou.psbase.get_children().find("test")
$user.PSBase.InvokeSet("TerminalServicesInitialProgram", "C:\logoff.bat")
$user.setinfo()
#add to group
net localgroup $groupname $username /add
net localgroup "Remote Desktop Users" $username /add
#remote login
cmdkey /generic:TERMSRV/127.0.0.1 /user: $username /pass: $password
#add logoff script
#launch remote desktop
mstsc /v:127.0.0.1 | Out-Null
cmdkey /delete:TERMSRV/127.0.0.1
#load hive
reg load HKU\%username% "C:\Users\$username\NTUSER.dat"
#set environment valiables
Set-ItemProperty -Path HKU:\$username\Environment -Name SERVERTYPE -Type STRING -Value DIR
#Unload hive
reg unload HKU\$username  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Powershell中的环境选项卡用户adsi中为TSclients添加登录程序 - Add logon program for TSclients in environment tab user adsi in Powershell 使用PowerShell登录,注销脚本分析 - Logon, logoff script analysing using PowerShell 通过PowerDP / cmd通过RDP登录终端服务器并执行命令 - Logon terminal server over RDP per powershell/cmd and execute commands PowerShell登录脚本以管理员身份 - PowerShell logon script as admin 如何在 windows 终端 powershell 选项卡中运行 powershell 脚本 (.ps1)? - How to run powershell script (.ps1) in windows terminal powershell tab? Powershell登录脚本删除项 - Powershell logon script remove item 从登录脚本启动的Powershell脚本不起作用 - Powershell script launched from logon script not working 如何使用 powershell 在本地用户上设置“环境”>“登录时启动以下程序”属性? - How can I set the “Environment” > “Start the following program at logon” property on a local user using powershell? 登录时间脚本 | 如何覆盖上次登录 Powershell - Logon time script | how to overwrite last logon Powershell 使用powershell脚本作为启动外部程序的登录脚本(组策略)? - Using a powershell script as a logon script (group policy) which starts a external program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM