O
O
OniVe2020-01-27 11:00:03
PowerShell
OniVe, 2020-01-27 11:00:03

How to change the program using a script when starting a terminal session?

For Windows Server 2003, you need to write a script to automatically set/change the setting " User Properties/Environment/Program Startup/Program File Name " for a group of users or users with a name ending in the string "Env" such as "User_Env".

There is an assumption how this can be done based on the materials of this book (p. 102).

As well as the script from the example for reading settings:

On Error Resume Next

Dim props

Set FileSystem = WScript.CreateObject("Scripting.FileSystemObject")
Set oFile      = FileSystem.CreateTextFile("GroupMembers.yml", True)

CRLF      = CHR(13) + CHR(10)
strDC     = "domainName.org" 'Доменное имя сервера
strRoot   = "domainName.org" 'Имя домена
strDomain = "DC=DOMAINNAME,DC=ORG"

Set DomainObj = GetObject("LDAP://" & strDC & "/CN=Users," & strDomain)

If Err.Number < 0 Then
  
  WScript.echo "Failed to connect to " & strDomain
  WScript.quit
End If

DomainObj.Filter = Array("group")

oFile.WriteLine ("groups:")

For Each GroupObj In DomainObj
  
  If GroupObj.Class = "group" Then
    
    oFile.WriteLine ( _
      "  - name        : " & MID(GroupObj.Name, 4) & CRLF & _
      "    description : " & GroupObj.Description _
    )
    
    set memberlist = GroupObj.Members
    
    If memberlist.Count > 0  Then
      
      oFile.WriteLine ("    members:")
      
      For Each member In memberlist
        
        oFile.WriteLine ("      - '{ " & _
          KeyValueStr("name", MID(member.Name, 4)) & ", " & _
          KeyValueStr("accountName", member.SAMAccountName) & ", " & _
          KeyValueStr("class", member.Class) & ", " & _
          KeyValueStr("scriptpath", member.Scriptpath)  & _
          " }'" _
        )
      Next
    End If
  End If
Next

Set DomainObj = Nothing
Set GroupObj = Nothing

If Err.Number <> 0 Then

  WScript.echo CRLF
  WScript.quit ("ERROR: " & Err.Number & " " & Err.Description & " from " & Err.Source)
  
  WScript.echo CRLF
End If

WScript.echo "Done"
WScript.quit

Function KeyValueStr(keyStr, valueStr)
  
  KeyValueStr = """" & keyStr & """: " & """" & valueStr & """"
End Function

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MaxKozlov, 2020-01-27
@OniVe

Get-ADUser -filter "samAccountName -like '*env'" | Set-ADUser -ScriptPath "somescriptname"
Get-ADGroupMember somegroupname | Set-ADUser -ScriptPath "somescriptname"

Found.
Based on https://devblogs.microsoft.com/scripting/hey-script...
$user = [ADSI]"LDAP://$($user_DistinguishedName)"
#read
#$user.psbase.InvokeGet('TerminalServicesInitialProgram')
#write
$user.psbase.InvokeSet('TerminalServicesInitialProgram', 'cmd.exe')
$user.SetInfo()

Let this also stay here
https://stackoverflow.com/questions/27814791/how-t...
list of all parameters
https://docs.microsoft.com/en-us/openspecs/windows...

R
res2001, 2020-01-27
@res2001

Add the launch of the program to the user's autorun.
If you need to run only in a terminal session, then launch the program in a batch file, where you can pre-determine the terminal session is active or local. Session information can be obtained using
query session /?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question