#saved as UTF8 # $username = "username" # $localnet = "1.2.3.4" # IP $lastevent = Get-EventLog Security -Message "*$username*:*.*.*.*" -Newest 1 # $IP = $lastevent.message.substring($lastevent.message.indexof(" :")+15,$lastevent.message.indexof(":")-$lastevent.message.indexof(" :")-18) $IDbyName = ((query session $username)[1] -split '\s+')[3] # "" if ($IDbyName -notlike $null) {if ($IP -notlike $localnet) { # logoff $IDbyName "`n---------" | Out-File "C:\SCRIPTFOLDER\Scripts\log.txt" -Append Get-Date | Out-File "C:\SCRIPTFOLDER\Scripts\log.txt" -Append " RDP `nUSER: $username `nIP: $ip" | Out-File "C:\SCRIPTFOLDER\Scripts\log.txt" -Append }}
<?xml version="1.0" encoding="UTF-16"?> <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"> <RegistrationInfo> <Date>2018-08-09T15:44:22.8651577</Date> <Author>ServerName\AdminUser</Author> <Description> IP</Description> </RegistrationInfo> <Triggers> <EventTrigger> <Enabled>false</Enabled> <Subscription><QueryList><Query Id="0" Path="Security"><Select Path="Security">*[System[(Level=4 or Level=0) and (band(Keywords,9007199254740992)) and (EventID=4648) and TimeCreated[timediff(@SystemTime) <= 3600000]]]</Select></Query></QueryList></Subscription> </EventTrigger> <SessionStateChangeTrigger> <Enabled>true</Enabled> <StateChange>RemoteConnect</StateChange> <UserId>ServerName\username</UserId> </SessionStateChangeTrigger> <LogonTrigger> <Enabled>true</Enabled> <UserId>ServerName\username</UserId> </LogonTrigger> </Triggers> <Principals> <Principal id="Author"> <UserId>ServerName\AdminUser</UserId> <LogonType>Password</LogonType> <RunLevel>HighestAvailable</RunLevel> </Principal> </Principals> <Settings> <MultipleInstancesPolicy>Parallel</MultipleInstancesPolicy> <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries> <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries> <AllowHardTerminate>true</AllowHardTerminate> <StartWhenAvailable>false</StartWhenAvailable> <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable> <IdleSettings> <StopOnIdleEnd>true</StopOnIdleEnd> <RestartOnIdle>false</RestartOnIdle> </IdleSettings> <AllowStartOnDemand>true</AllowStartOnDemand> <Enabled>false</Enabled> <Hidden>false</Hidden> <RunOnlyIfIdle>false</RunOnlyIfIdle> <WakeToRun>false</WakeToRun> <ExecutionTimeLimit>PT1H</ExecutionTimeLimit> <Priority>7</Priority> <RestartOnFailure> <Interval>PT1M</Interval> <Count>3</Count> </RestartOnFailure> </Settings> <Actions Context="Author"> <Exec> <Command>PowerShell</Command> <Arguments>-File "C:\SCRIPTFOLDER\Scripts\kickUsername.ps1"</Arguments> </Exec> </Actions> </Task>
Source: https://habr.com/ru/post/423301/
All Articles