Hello everyone,<\/p>\n
I have written a powershell script to parse event logs, find out what servers you have RDP’d in and return a True False statement. Everything works, however while tweaking I came across an issue. I was trying to write the Out-File to be based off the individual wanting to run the script. For example, user John needs to type into the “Enter Admin Account” his username and the script writes the path’s as well as the user that it is looking for based off that information. When I wrote the $Account = Read-Host “EnterAdmin Account” the script ignores the variable. Here is the script:<\/p>\n
$AdmAccount = Read-Host \"EnterADM Account\"\n $Desktop = \"C:\\Users\\$($_.user)\\Desktop\" \n\n$serverlist = Start-Job { Get-WinEvent -FilterHashtable @{LogName=’Security’;ID=4648} |\n where {$_.Properties[5].Value -eq $AdmAccount} |\n foreach {“$($_.Properties[8].Value)” } | Out-File \"$Desktop\\servers.txt\"\n$file = \"$Desktop\\servers.txt\"\n(gc $file | Group-Object | %{$_.group | select -First 1}) | Where-Object {$_ -notmatch 'localhost'} | Set-Content $file\n}\n\nWait-Job $serverlist\nReceive-Job $serverlist \n$AdmAccount = Read-Host \"EnterADM Account\"\nparam($AdmAccount=$(throw \"You must specify your name\"))\n\nfunction Get-LoggedOn\n\n{\n\n\t[CmdletBinding()]\n\t[Alias('loggedon')]\n\t[OutputType([PSCustomObject])]\n\t\n\tParam\n\t(\n\t\t# Computer name to check\n\t\t[Parameter(ValueFromPipeline = $true,\n\t\t\t\t ValueFromPipelineByPropertyName = $true,\n\t\t\t Position = 0)]\n\t\t[Alias('ComputerName')]\n\t\t[string]\n\t\t$Name = $env:COMPUTERNAME,\n\t\t\n\t\t# Username to check against logged in users.\n\t\t[parameter()]\n\t\t[string]\n\t\t$CheckFor\n\t)\n\t\n\tProcess\n\t{\n\t\tfunction QueryToObject ($Computer)\n\t\t{\n\t\t\t$Output = @()\n\t\t\t$Users = query user /server:$Computer.nwie.net 2>&1\n\t\t\tif ($Users -like \"*No User exists*\")\n\t\t\t{\n\t\t\t\t$Output += [PSCustomObject]@{\n\t\t\t\t\tComputerName = $Computer\n\t\t\t\t\tUsername = $null\n\t\t\t\t\tSessionState = $null\n\t\t\t\t\tSessionType = \"[None Found]\"\n\t\t\t\t}\n\t\t\t}\n\t\t\telseif ($Users -like \"*Error*\")\n\t\t\t{\n\t\t\t\t$Output += [PSCustomObject]@{\n\t\t\t\t\tComputerName = $Computer\n\t\t\t\t\tUsername = $null\n\t\t\t\t\tSessionState = $null\n\t\t\t\t\tSessionType = \"[Error]\"\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t$Users = $Users | ForEach-Object {\n\t\t\t\t\t(($_.trim() -replace \">\" -replace \"(?m)^([A-Za-z0-9]{3,})\\s+(\\d{1,2}\\s+\\w+)\", '$1 none $2' -replace \"\\s{2,}\", \",\" -replace \"none\", $null))\n\t\t\t\t} | ConvertFrom-Csv\n\t\t\t\t\n\t\t\t\tforeach ($User in $Users)\n\t\t\t\t{\n\t\t\t\t\t$Output += [PSCustomObject]@{\n\t\t\t\t\t\tComputerName = $Computer\n\t\t\t\t\t\tUsername = $User.USERNAME\n\t\t\t\t\t\tSessionState = $User.STATE.Replace(\"Disc\", \"Disconnected\")\n\t\t\t\t\t\tSessionType = $($User.SESSIONNAME -Replace '#','' -Replace \"[0-9]+\",\"\")\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn $Output | Sort-Object -Property ComputerName\n\t\t}\n\t\t\n\t\tif ($CheckFor)\n\t\t{\n\t\t\t$Usernames = @()\n\t\t\t$Sessions = @()\n\t\t\t$Result = @()\n\t\t\t$Users = QueryToObject -Computer $Name\n\t\t\t\n\t\t\tforeach ($User in $Users) {\n\t\t\t\t$Usernames += $User.Username\n\t\t\t\t$Sessions += $User.SessionType\n\t\t\t}\n\t\t\t\n\t\t\tif (\"[Error]\" -in $Sessions)\n\t\t\t{\n\t\t\t\t$Result += [PSCustomObject]@{\n\t\t\t\t\tComputerName = $Name\n\t\t\t\t\tIsLoggedOn = \"[ERROR]\"\n\t\t\t\t}\n\t\t\t}\n\t\t\telseif ($CheckFor -in $Usernames -and \"[*]\" -notin $Sessions)\n\t\t\t{\n\t\t\t\t$Result += [PSCustomObject]@{\n\t\t\t\t\tComputerName = $Name\n\t\t\t\t\tIsLoggedOn = $true\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t$Result += [PSCustomObject]@{\n\t\t\t\t\tComputerName = $Name\n\t\t\t\t\tIsLoggedOn = $false\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn $Result | select ComputerName,IsLoggedOn\n\t\t}\n\t\telseif (!$CheckFor)\n\t\t{\n\t\t\t$Result = QueryToObject -Computer $Name\n\t\t\treturn $Result\n\t\t}\n\t}\n\n}\n\n$name=GC C:\\users\\$AdmAccount\\Desktop\\servers.txt\nForeach($list in $name){\nGet-LoggedOn -Name $list -CheckFor $AdmAccount | Out-File C:\\users\\$AdmAccount\\Desktop\\results.txt -Append\n}\n<\/code><\/pre>","upvoteCount":5,"answerCount":9,"datePublished":"2017-10-24T11:02:12.000Z","author":{"@type":"Person","name":"joe1232","url":"https://community.spiceworks.com/u/joe1232"},"acceptedAnswer":{"@type":"Answer","text":"\n\n
<\/div>\n
Shelly3360:<\/div>\n
\n
Is the $Desktop variable the one that your script is ignoring? It looks like the account entered is the one you want to use for that information? if that is the case, you would change $Desktop to be:<\/p>\n
$AdmAccount = Read-Host \"EnterADM Account\"\n $Desktop = \"C:\\Users\\$AdmAccount\\Desktop\" \n<\/code><\/pre>\n<\/blockquote>\n<\/aside>\nThe script was ignoring both variables ( I was trying different things with this but this has changed a few times since i initially asked the question) Unfortunately what i sent here was the wtf happened here version lol. I got the script to work though. For the directory variable’s i used $home for the Admin account search i used $env:UserName to grab the current users name as the input.<\/p>\n
$serverlist = Start-Job { Get-WinEvent -FilterHashtable @{LogName='Security';ID=4648} |\n where {$_.Properties[5].Value -eq $env:UserName} |\n foreach {“$($_.Properties[8].Value)” } | Out-File \"$home\\Desktop\\servers.txt\"\n$file = \"$home\\Desktop\\servers.txt\"\n(gc $file | Group-Object | %{$_.group | select -First 1}) | Where-Object {$_ -notmatch 'localhost'} | Set-Content $file\n}\n\nWait-Job $serverlist\nReceive-Job $serverlist \n<\/code><\/pre>","upvoteCount":0,"datePublished":"2017-10-24T12:40:05.000Z","url":"https://community.spiceworks.com/t/powershell-variable-not-working/614021/9","author":{"@type":"Person","name":"joe1232","url":"https://community.spiceworks.com/u/joe1232"}},"suggestedAnswer":[{"@type":"Answer","text":"Hello everyone,<\/p>\n
I have written a powershell script to parse event logs, find out what servers you have RDP’d in and return a True False statement. Everything works, however while tweaking I came across an issue. I was trying to write the Out-File to be based off the individual wanting to run the script. For example, user John needs to type into the “Enter Admin Account” his username and the script writes the path’s as well as the user that it is looking for based off that information. When I wrote the $Account = Read-Host “EnterAdmin Account” the script ignores the variable. Here is the script:<\/p>\n
$AdmAccount = Read-Host \"EnterADM Account\"\n $Desktop = \"C:\\Users\\$($_.user)\\Desktop\" \n\n$serverlist = Start-Job { Get-WinEvent -FilterHashtable @{LogName=’Security’;ID=4648} |\n where {$_.Properties[5].Value -eq $AdmAccount} |\n foreach {“$($_.Properties[8].Value)” } | Out-File \"$Desktop\\servers.txt\"\n$file = \"$Desktop\\servers.txt\"\n(gc $file | Group-Object | %{$_.group | select -First 1}) | Where-Object {$_ -notmatch 'localhost'} | Set-Content $file\n}\n\nWait-Job $serverlist\nReceive-Job $serverlist \n$AdmAccount = Read-Host \"EnterADM Account\"\nparam($AdmAccount=$(throw \"You must specify your name\"))\n\nfunction Get-LoggedOn\n\n{\n\n\t[CmdletBinding()]\n\t[Alias('loggedon')]\n\t[OutputType([PSCustomObject])]\n\t\n\tParam\n\t(\n\t\t# Computer name to check\n\t\t[Parameter(ValueFromPipeline = $true,\n\t\t\t\t ValueFromPipelineByPropertyName = $true,\n\t\t\t Position = 0)]\n\t\t[Alias('ComputerName')]\n\t\t[string]\n\t\t$Name = $env:COMPUTERNAME,\n\t\t\n\t\t# Username to check against logged in users.\n\t\t[parameter()]\n\t\t[string]\n\t\t$CheckFor\n\t)\n\t\n\tProcess\n\t{\n\t\tfunction QueryToObject ($Computer)\n\t\t{\n\t\t\t$Output = @()\n\t\t\t$Users = query user /server:$Computer.nwie.net 2>&1\n\t\t\tif ($Users -like \"*No User exists*\")\n\t\t\t{\n\t\t\t\t$Output += [PSCustomObject]@{\n\t\t\t\t\tComputerName = $Computer\n\t\t\t\t\tUsername = $null\n\t\t\t\t\tSessionState = $null\n\t\t\t\t\tSessionType = \"[None Found]\"\n\t\t\t\t}\n\t\t\t}\n\t\t\telseif ($Users -like \"*Error*\")\n\t\t\t{\n\t\t\t\t$Output += [PSCustomObject]@{\n\t\t\t\t\tComputerName = $Computer\n\t\t\t\t\tUsername = $null\n\t\t\t\t\tSessionState = $null\n\t\t\t\t\tSessionType = \"[Error]\"\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t$Users = $Users | ForEach-Object {\n\t\t\t\t\t(($_.trim() -replace \">\" -replace \"(?m)^([A-Za-z0-9]{3,})\\s+(\\d{1,2}\\s+\\w+)\", '$1 none $2' -replace \"\\s{2,}\", \",\" -replace \"none\", $null))\n\t\t\t\t} | ConvertFrom-Csv\n\t\t\t\t\n\t\t\t\tforeach ($User in $Users)\n\t\t\t\t{\n\t\t\t\t\t$Output += [PSCustomObject]@{\n\t\t\t\t\t\tComputerName = $Computer\n\t\t\t\t\t\tUsername = $User.USERNAME\n\t\t\t\t\t\tSessionState = $User.STATE.Replace(\"Disc\", \"Disconnected\")\n\t\t\t\t\t\tSessionType = $($User.SESSIONNAME -Replace '#','' -Replace \"[0-9]+\",\"\")\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn $Output | Sort-Object -Property ComputerName\n\t\t}\n\t\t\n\t\tif ($CheckFor)\n\t\t{\n\t\t\t$Usernames = @()\n\t\t\t$Sessions = @()\n\t\t\t$Result = @()\n\t\t\t$Users = QueryToObject -Computer $Name\n\t\t\t\n\t\t\tforeach ($User in $Users) {\n\t\t\t\t$Usernames += $User.Username\n\t\t\t\t$Sessions += $User.SessionType\n\t\t\t}\n\t\t\t\n\t\t\tif (\"[Error]\" -in $Sessions)\n\t\t\t{\n\t\t\t\t$Result += [PSCustomObject]@{\n\t\t\t\t\tComputerName = $Name\n\t\t\t\t\tIsLoggedOn = \"[ERROR]\"\n\t\t\t\t}\n\t\t\t}\n\t\t\telseif ($CheckFor -in $Usernames -and \"[*]\" -notin $Sessions)\n\t\t\t{\n\t\t\t\t$Result += [PSCustomObject]@{\n\t\t\t\t\tComputerName = $Name\n\t\t\t\t\tIsLoggedOn = $true\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t$Result += [PSCustomObject]@{\n\t\t\t\t\tComputerName = $Name\n\t\t\t\t\tIsLoggedOn = $false\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn $Result | select ComputerName,IsLoggedOn\n\t\t}\n\t\telseif (!$CheckFor)\n\t\t{\n\t\t\t$Result = QueryToObject -Computer $Name\n\t\t\treturn $Result\n\t\t}\n\t}\n\n}\n\n$name=GC C:\\users\\$AdmAccount\\Desktop\\servers.txt\nForeach($list in $name){\nGet-LoggedOn -Name $list -CheckFor $AdmAccount | Out-File C:\\users\\$AdmAccount\\Desktop\\results.txt -Append\n}\n<\/code><\/pre>","upvoteCount":5,"datePublished":"2017-10-24T11:02:12.000Z","url":"https://community.spiceworks.com/t/powershell-variable-not-working/614021/1","author":{"@type":"Person","name":"joe1232","url":"https://community.spiceworks.com/u/joe1232"}},{"@type":"Answer","text":"When you use single quotes around a PowerShell variable, PS gives you the actual variable name. If you want PS to parse it and use the value instead you have to use double quotes.<\/p>\n
where {$_.Properties[5].Value -eq \"$Account\"} |\n<\/code><\/pre>\nAlthough in this particular case, I believe you don’t need to use quotes of any kind to get the variable’s value.<\/p>\n
Also, please use the Insert Code button </> and select PowerShell when inserting code snippets. Otherwise your code is too hard for others to read.<\/p>","upvoteCount":1,"datePublished":"2017-10-24T11:07:18.000Z","url":"https://community.spiceworks.com/t/powershell-variable-not-working/614021/2","author":{"@type":"Person","name":"shelly","url":"https://community.spiceworks.com/u/shelly"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Shelly3360:<\/div>\n
\nWhen you use single quotes around a PowerShell variable, PS gives you the actual variable name. If you want PS to parse it and use the value instead you have to use double quotes.<\/p>\n
where {$_.Properties[5].Value -eq \"$Account\"} |\n<\/code><\/pre>\nAlthough in this particular case, I believe you don’t need to use quotes of any kind to get the variable’s value.<\/p>\n
Also, please use the Insert Code button </> and select PowerShell when inserting code snippets. Otherwise your code is too hard for others to read.<\/p>\n<\/blockquote>\n<\/aside>\n
Thank you for the help on this, I have changed it to double quotes and still no luck. I have also modified the post by clicking and inserting the code but I am not seeing it anywhere on here.<\/p>","upvoteCount":0,"datePublished":"2017-10-24T11:25:09.000Z","url":"https://community.spiceworks.com/t/powershell-variable-not-working/614021/3","author":{"@type":"Person","name":"joe1232","url":"https://community.spiceworks.com/u/joe1232"}},{"@type":"Answer","text":"
Can you try posting it again? Even if it’s in a reply?<\/p>","upvoteCount":1,"datePublished":"2017-10-24T11:30:04.000Z","url":"https://community.spiceworks.com/t/powershell-variable-not-working/614021/4","author":{"@type":"Person","name":"big-green-man","url":"https://community.spiceworks.com/u/big-green-man"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Big Green Man:<\/div>\n
\nCan you try posting it again? Even if it’s in a reply?<\/p>\n<\/blockquote>\n<\/aside>\n
Even then it does not insert it<\/p>\n
weird<\/p>","upvoteCount":0,"datePublished":"2017-10-24T11:40:50.000Z","url":"https://community.spiceworks.com/t/powershell-variable-not-working/614021/5","author":{"@type":"Person","name":"joe1232","url":"https://community.spiceworks.com/u/joe1232"}},{"@type":"Answer","text":"
Ohh well. Post it without inserting then.<\/p>","upvoteCount":1,"datePublished":"2017-10-24T11:41:39.000Z","url":"https://community.spiceworks.com/t/powershell-variable-not-working/614021/6","author":{"@type":"Person","name":"big-green-man","url":"https://community.spiceworks.com/u/big-green-man"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Big Green Man:<\/div>\n
\nOhh well. Post it without inserting then.<\/p>\n<\/blockquote>\n<\/aside>\n
Fixed it, long story short, IE sucks<\/p>","upvoteCount":0,"datePublished":"2017-10-24T11:45:01.000Z","url":"https://community.spiceworks.com/t/powershell-variable-not-working/614021/7","author":{"@type":"Person","name":"joe1232","url":"https://community.spiceworks.com/u/joe1232"}},{"@type":"Answer","text":"
Is the $Desktop variable the one that your script is ignoring? It looks like the account entered is the one you want to use for that information? if that is the case, you would change $Desktop to be:<\/p>\n
$AdmAccount = Read-Host \"EnterADM Account\"\n $Desktop = \"C:\\Users\\$AdmAccount\\Desktop\" \n<\/code><\/pre>","upvoteCount":0,"datePublished":"2017-10-24T11:59:22.000Z","url":"https://community.spiceworks.com/t/powershell-variable-not-working/614021/8","author":{"@type":"Person","name":"shelly","url":"https://community.spiceworks.com/u/shelly"}}]}}
joe1232
(Joe7896)
October 24, 2017, 11:02am
1
Hello everyone,
I have written a powershell script to parse event logs, find out what servers you have RDP’d in and return a True False statement. Everything works, however while tweaking I came across an issue. I was trying to write the Out-File to be based off the individual wanting to run the script. For example, user John needs to type into the “Enter Admin Account” his username and the script writes the path’s as well as the user that it is looking for based off that information. When I wrote the $Account = Read-Host “EnterAdmin Account” the script ignores the variable. Here is the script:
$AdmAccount = Read-Host "EnterADM Account"
$Desktop = "C:\Users\$($_.user)\Desktop"
$serverlist = Start-Job { Get-WinEvent -FilterHashtable @{LogName=’Security’;ID=4648} |
where {$_.Properties[5].Value -eq $AdmAccount} |
foreach {“$($_.Properties[8].Value)” } | Out-File "$Desktop\servers.txt"
$file = "$Desktop\servers.txt"
(gc $file | Group-Object | %{$_.group | select -First 1}) | Where-Object {$_ -notmatch 'localhost'} | Set-Content $file
}
Wait-Job $serverlist
Receive-Job $serverlist
$AdmAccount = Read-Host "EnterADM Account"
param($AdmAccount=$(throw "You must specify your name"))
function Get-LoggedOn
{
[CmdletBinding()]
[Alias('loggedon')]
[OutputType([PSCustomObject])]
Param
(
# Computer name to check
[Parameter(ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
Position = 0)]
[Alias('ComputerName')]
[string]
$Name = $env:COMPUTERNAME,
# Username to check against logged in users.
[parameter()]
[string]
$CheckFor
)
Process
{
function QueryToObject ($Computer)
{
$Output = @()
$Users = query user /server:$Computer.nwie.net 2>&1
if ($Users -like "*No User exists*")
{
$Output += [PSCustomObject]@{
ComputerName = $Computer
Username = $null
SessionState = $null
SessionType = "[None Found]"
}
}
elseif ($Users -like "*Error*")
{
$Output += [PSCustomObject]@{
ComputerName = $Computer
Username = $null
SessionState = $null
SessionType = "[Error]"
}
}
else
{
$Users = $Users | ForEach-Object {
(($_.trim() -replace ">" -replace "(?m)^([A-Za-z0-9]{3,})\s+(\d{1,2}\s+\w+)", '$1 none $2' -replace "\s{2,}", "," -replace "none", $null))
} | ConvertFrom-Csv
foreach ($User in $Users)
{
$Output += [PSCustomObject]@{
ComputerName = $Computer
Username = $User.USERNAME
SessionState = $User.STATE.Replace("Disc", "Disconnected")
SessionType = $($User.SESSIONNAME -Replace '#','' -Replace "[0-9]+","")
}
}
}
return $Output | Sort-Object -Property ComputerName
}
if ($CheckFor)
{
$Usernames = @()
$Sessions = @()
$Result = @()
$Users = QueryToObject -Computer $Name
foreach ($User in $Users) {
$Usernames += $User.Username
$Sessions += $User.SessionType
}
if ("[Error]" -in $Sessions)
{
$Result += [PSCustomObject]@{
ComputerName = $Name
IsLoggedOn = "[ERROR]"
}
}
elseif ($CheckFor -in $Usernames -and "[*]" -notin $Sessions)
{
$Result += [PSCustomObject]@{
ComputerName = $Name
IsLoggedOn = $true
}
}
else
{
$Result += [PSCustomObject]@{
ComputerName = $Name
IsLoggedOn = $false
}
}
return $Result | select ComputerName,IsLoggedOn
}
elseif (!$CheckFor)
{
$Result = QueryToObject -Computer $Name
return $Result
}
}
}
$name=GC C:\users\$AdmAccount\Desktop\servers.txt
Foreach($list in $name){
Get-LoggedOn -Name $list -CheckFor $AdmAccount | Out-File C:\users\$AdmAccount\Desktop\results.txt -Append
}
5 Spice ups
shelly
(Shelly3360)
October 24, 2017, 11:07am
2
When you use single quotes around a PowerShell variable, PS gives you the actual variable name. If you want PS to parse it and use the value instead you have to use double quotes.
where {$_.Properties[5].Value -eq "$Account"} |
Although in this particular case, I believe you don’t need to use quotes of any kind to get the variable’s value.
Also, please use the Insert Code button </> and select PowerShell when inserting code snippets. Otherwise your code is too hard for others to read.
1 Spice up
joe1232
(Joe7896)
October 24, 2017, 11:25am
3
Shelly3360:
When you use single quotes around a PowerShell variable, PS gives you the actual variable name. If you want PS to parse it and use the value instead you have to use double quotes.
where {$_.Properties[5].Value -eq "$Account"} |
Although in this particular case, I believe you don’t need to use quotes of any kind to get the variable’s value.
Also, please use the Insert Code button </> and select PowerShell when inserting code snippets. Otherwise your code is too hard for others to read.
Thank you for the help on this, I have changed it to double quotes and still no luck. I have also modified the post by clicking and inserting the code but I am not seeing it anywhere on here.
Can you try posting it again? Even if it’s in a reply?
1 Spice up
joe1232
(Joe7896)
October 24, 2017, 11:40am
5
Even then it does not insert it
weird
Ohh well. Post it without inserting then.
1 Spice up
joe1232
(Joe7896)
October 24, 2017, 11:45am
7
Fixed it, long story short, IE sucks
shelly
(Shelly3360)
October 24, 2017, 11:59am
8
Is the $Desktop variable the one that your script is ignoring? It looks like the account entered is the one you want to use for that information? if that is the case, you would change $Desktop to be:
$AdmAccount = Read-Host "EnterADM Account"
$Desktop = "C:\Users\$AdmAccount\Desktop"
joe1232
(Joe7896)
October 24, 2017, 12:40pm
9
Shelly3360:
Is the $Desktop variable the one that your script is ignoring? It looks like the account entered is the one you want to use for that information? if that is the case, you would change $Desktop to be:
$AdmAccount = Read-Host "EnterADM Account"
$Desktop = "C:\Users\$AdmAccount\Desktop"
The script was ignoring both variables ( I was trying different things with this but this has changed a few times since i initially asked the question) Unfortunately what i sent here was the wtf happened here version lol. I got the script to work though. For the directory variable’s i used $home for the Admin account search i used $env:UserName to grab the current users name as the input.
$serverlist = Start-Job { Get-WinEvent -FilterHashtable @{LogName='Security';ID=4648} |
where {$_.Properties[5].Value -eq $env:UserName} |
foreach {“$($_.Properties[8].Value)” } | Out-File "$home\Desktop\servers.txt"
$file = "$home\Desktop\servers.txt"
(gc $file | Group-Object | %{$_.group | select -First 1}) | Where-Object {$_ -notmatch 'localhost'} | Set-Content $file
}
Wait-Job $serverlist
Receive-Job $serverlist