我正在尝试显示一个脚本的输出,该脚本在域控制器中搜索已锁定到我创建的 GUI 中的标签中并遇到问题的帐户。我已经能够做到这一点,而我使用的其他脚本没有问题,但是这个有点不同,我是一个完整的新手,并且一直在努力弄清楚。在 Powershell 中单独运行脚本时,它会以美观、易于准备的格式输出信息,但我无法将这些信息放入我的标签中。
#LOCKOUT TRACE BUTTON
$Button16 = New-Object system.Windows.Forms.Button
$Button16.text = "Lockout Trace"
$Button16.width = 85
$Button16.height = 30
$Button16.location = New-Object
System.Drawing.Point(105,113)
$ErrorActionPreference = 'SilentlyContinue'
$Button16.Add_Click(
{
$Label2.Text = ""
Import-Module ActiveDirectory
$UserName = $($TextBox1.Text)
#Get DCs
$PDC = (Get-ADDomainController -Discover -Service PrimaryDC).Name
$DCs = (Get-ADDomainController -Filter *).Name
#Get user info
$UserInfo = Get-ADUser -Identity $UserName
#Search PDC for lockout events with ID 4740
$LockedOutEvents = foreach ($DC in $DCs) { Get-WinEvent -ComputerName $DC
-FilterHashtable @{LogName='Security';Id=4740} -ErrorAction Stop | Sort-
Object -Property TimeCreated -Descending }
#Parse and filter out lockout events
Foreach($Event in $LockedOutEvents)
{
If($Event | Where {$_.Properties[2].value -match $UserInfo.SID.Value})
{
$Event | Select-Object -Property @(
@{Label = 'User'; Expression = {$_.Properties[0].Value}}
@{Label = 'DomainController'; Expression = {$_.MachineName}}
@{Label = 'EventId'; Expression = {$_.Id}}
@{Label = 'LockoutTimeStamp'; Expression = {$_.TimeCreated}}
@{Label = 'Message'; Expression = {$_.Message -split "`r" | Select -
First 1}}
@{Label = 'LockoutSource'; Expression = {$_.Properties[1].Value}}
)
}}
$Label2.Text = ("User = $($_.Properties[0].Value)
DomainController = $($_.MachineName)
EventID = $($_.ID)
LockoutTimeStamp = $($_.TimeCreated)
Message = $($_.Message -split "`r" | Select -First 1)
LockoutSource = $($_.Properties[1].Value)")
}
)
$main_form.Controls.Add($Button16)
在提供的代码中,它按预期在自己的行上返回 "User =" "DomainController =" "Event ID =" 等,只是没有数据跟随。如果我将“$Label2.Text =”放在“Foreach($Event in $LockedOutEvents)”之前,所有数据都将在标签中返回,但它们都在一条连续线上。我希望它在创建的标签中在自己的单独行上输出数据点,所以我向天才寻求帮助。这个网站已经回答了我在这次旅程中的不止一个问题,我希望还有另一个问题。提前致谢。
您显示的代码有时会在任意位置用换行符中断。我希望这只是试图在问题中格式化它。
至于我认为你想要什么,最好的办法可能是不使用 Label 对象在表单中显示结果,而是使用带有滚动条的多行 TextBox,这样您就知道返回的值将始终适合表单。
现在,我保持$Label2
原样,但将您改造$Button16.Add_Click()
成:
$Button16.Add_Click({
$Label2.Text = ""
$UserName = $TextBox1.Text
# Try and get the user object from the name entered in the textbox
$UserInfo = Get-ADUser -Identity $UserName
if (!$UserInfo) {
$Label2.Text = "$UserName not found!"
return
}
# Get DCs
## $PDC = (Get-ADDomainController -Discover -Service PrimaryDC).Name
$DCs = (Get-ADDomainController -Filter *).Name
# Search DC's for lockout events with ID 4740
$LockedOutEvents = foreach ($DC in $DCs) {
Get-WinEvent -ComputerName $DC -FilterHashtable @{LogName='Security';Id=4740} -ErrorAction SilentlyContinue |
Where {$_.Properties[2].value -match $UserInfo.SID} |
Sort-Object -Property TimeCreated -Descending
}
if ($LockedOutEvents) {
# Parse and filter out lockout events. Collect the outputted string(s) in a variable
# use 'Format-List | Out-String' to collect nicely formatted text
$events = $LockedOutEvents | ForEach-Object {
($_ | Select-Object -Property @(
@{Name = 'User'; Expression = {$_.Properties[0].Value}}
@{Name = 'DomainController'; Expression = {$_.MachineName}}
@{Name = 'EventId'; Expression = {$_.Id}}
@{Name = 'LockoutTimeStamp'; Expression = {$_.TimeCreated}}
@{Name = 'Message'; Expression = {$_.Message -split '\r?\n' | Select-Object -First 1}}
@{Name = 'LockoutSource'; Expression = {$_.Properties[1].Value}}
) | Format-List | Out-String).Trim()
}
# join the collected output and insert that into the label
$Label2.Text = $events -join ([Environment]::NewLine * 2)
}
else {
$Label2.Text = "No Lockout events found for user $UserName"
}
})
该Import-Module ActiveDirectory
行可以向上移动到脚本的第一行。
希望这可以帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句