Powershell脚本审核远程桌面用户登录

问题描述 投票:0回答:1

我找到了一个脚本,记录了RDS服务器的所有用户,这些用户运行良好;

Link here

但是我想让它特定于1个用户,而不是所有用户。

我真的不知道powershell所以需要一些帮助。

Param(
[array]$ServersToQuery = (hostname),
[datetime]$StartTime = "January 1, 1970"

)

foreach ($Server in $ServersToQuery) {

    $LogFilter = @{
        LogName = 'Microsoft-Windows-TerminalServices-LocalSessionManager/Operational'
        ID = 21, 23, 24, 25
         StartTime = (get-date).adddays(-7)
        }

    $AllEntries = Get-WinEvent -FilterHashtable $LogFilter -ComputerName $Server

    $AllEntries | Foreach { 
        $entry = [xml]$_.ToXml()
        [array]$Output += New-Object PSObject -Property @{
            TimeCreated = $_.TimeCreated
            User = $entry.Event.UserData.EventXML.User
            IPAddress = $entry.Event.UserData.EventXML.Address
            EventID = $entry.Event.System.EventID
            ServerName = $Server
            }        
        } 

}

$FilteredOutput += $Output | Select TimeCreated, User, ServerName, IPAddress, @{Name='Action';Expression={
            if ($_.EventID -eq '21'){"logon"}
            if ($_.EventID -eq '22'){"Shell start"}
            if ($_.EventID -eq '23'){"logoff"}
            if ($_.EventID -eq '24'){"disconnected"}
            if ($_.EventID -eq '25'){"reconnection"}
            }
        }

$Date = (Get-Date -Format s) -replace ":", "."
$FilePath = "$env:USERPROFILE\Desktop\$Date`_RDP_Report.csv"
$FilteredOutput | Sort TimeCreated | Export-Csv $FilePath -NoTypeInformation

写主机“写文件:$ FilePath”-ForegroundColor Cyan Write-host“Done!” -ForegroundColor Cyan

powershell remote-desktop audit
1个回答
1
投票

所以你说 …

(我真的不知道powershell所以需要一些帮助。)

...,但指向您要使用的非常高级的PowerShell脚本。

至关重要的是,您不要使用任何人的代码,而这些代码并不能完全理解任何人所做的事情。您可能严重损害/危害您的系统或整个企业。请加强保护自己,企业并避免不必要的混乱,并发症,问题,错误和挫折:

Follow this link

至于你的查询......

但是我想让它特定于1个用户,而不是所有用户。

...虽然脚本会返回所有用户,但您只需过滤/提示您所在的用户,而无需更改任何有关作者代码的内容。

通过在该param块中添加其他参数来提示用户

[string]$targetUser = (Read-Host -Prompt 'Enter a username')

在$ FilteredOutput部分中,您可以使用其他$ targetUser参数,使用Where-Object cmdlet或匹配在那里或在...中的字符串。

$FilteredOutput | Sort TimeCreated | Export-Csv $FilePath -NoTypeInformation

… 部分。就像是...

($FilteredOutput -match $TargetUser) | Sort TimeCreated | Export-Csv $FilePath -NoTypeInformation

我没有环境来测试这个,所以,我会把它留给你。

$ FilteredOutput |排序TimeCreated | Export-Csv $ FilePath -NoTypeInformation这是所有基本的PowerShell'使用参数'用例,并涵盖在所有开始的PowerShell课程,书籍,网站和内置PowerShell帮助文件中。

© www.soinside.com 2019 - 2024. All rights reserved.