[英]How can I stop and start individual websites in IIS using PowerShell?
[英]How can I set the “Environment” > “Start the following program at logon” property on a local user using powershell?
背景:
我有一台安裝了 Windows 2008 R2 的服務器作為終端服務器會話主機運行。 我有一長串本地用戶設置和配置為遠程桌面用戶。 當用戶使用遠程桌面連接遠程登錄時,程序會自動啟動。 當用戶關閉該程序時,會話結束。 如果我手動設置,這一切都很好。
我的問題:
我編寫了一個腳本來自動添加本地用戶列表並設置和配置屬性。 問題是我無處可找到如何設置“環境”>“登錄時啟動以下程序”屬性。 (有關我要設置的屬性,請參見圖片)
我當前腳本的示例部分如下:
$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();
我也試過這個不起作用的命令:
$objUser.PSBase.InvokeSet("TerminalServicesInitialProgram", "C:\programs\a_test_program.exe")
我在 Microsoft 的 MSDN 站點以及 Google 和 StackOverflow 上進行了搜索,但找不到此特定屬性。
我在這里找到了解決方案。
$ou = [adsi]"WinNT://127.0.0.1"
$user = $ou.psbase.get_children().find("test")
$user.PSBase.InvokeSet("TerminalServicesInitialProgram", "C:\logoff.bat")
$user.setinfo()
好的,所以我終於讓它工作了。 似乎您必須先創建用戶,然后在InvokeSet
設置TerminalServicesInitialProgram
屬性之前再次打開它進行編輯。
我不確定,也許有人可以分享一些經驗或解釋。
感謝大家的幫助和幫助。
工作代碼:
# 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()
}
調用 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.