Hi Guys,
I used the get.vievent script from vsphere powercli console example "modified for get the last 24 hours logged in users"
Original script :
C:\PS>Connect-VIServer -Server 10.23.113.41
$events = Get-VIEvent -MaxSamples 100
foreach ($event in $events) {if ($event.fullFormattedMessage -match "User
(.*)@\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b logged in") {Write-Host ("User
" + $matches[1] + " logged in at:" + $event.createdTime)} }
When i executed the output on the console is :
User vcops logged in at:03/21/2016 04:50:41
User vcops logged in at:03/21/2016 04:20:21
User backupsvahcs logged in at:03/21/2016 04:01:37
User backupsvahcs logged in at:03/21/2016 04:01:36
User backupsvahcs logged in at:03/21/2016 04:01:36
User backupsvahcs logged in at:03/21/2016 04:01:31
User vcloudum logged in at:03/21/2016 04:00:22
User vcops logged in at:03/21/2016 03:50:00
User vcops logged in at:03/21/2016 03:19:39
User vcloudum logged in at:03/21/2016 03:00:25
User vcops logged in at:03/21/2016 02:49:18
User vcops logged in at:03/21/2016 02:18:58
Until now everything is nice !
Modified script :
$fecha= get-date -Format D
$todayMidnight = (Get-Date -Hour 0 -Minute 0 -Second 0).AddMinutes(-1)
$events = Get-VIEvent -start $todayMidnight.AddDays(-1) -Finish $todayMidnight.AddDays(-0)
$events | foreach ($event in $events) {if ($event.fullFormattedMessage -match "User(.*)@\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b logged in") {Write-Host ("User" + $matches[1] + " logged in at:" + $event.createdTime)}} | out-file c:\ReportesvCENTER\test.txt
When i executed the script i receive :
PowerCLI C:\PS-Scripts> .\loggedin1.ps1
Unexpected token 'in' in expression or statement.
At C:\PS-Scripts\loggedin1.ps1:9 char:29
+ $events | foreach ($event in <<<< $events) {if ($event.fullFormattedMessage
-match "User(.*)@\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b logged in") {Write-Hos
t ("User" + $matches[1] + " logged in at:" + $event.createdTime)}} | > c:\Repo
rtesvCENTER\test.txt
+ CategoryInfo : ParserError: (in:String) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
PowerCLI C:\PS-Scripts>
But if i dont include the pipe before foreach :
PowerCLI C:\PS-Scripts> .\loggedin1.ps1
An empty pipe element is not allowed.
At C:\PS-Scripts\loggedin1.ps1:9 char:209
+ foreach ($event in $events) {if ($event.fullFormattedMessage -match "User(.*
)@\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b logged in") {Write-Host ("User" + $mat
ches[1] + " logged in at:" + $event.createdTime)}} | <<<< > c:\ReportesvCENTE
R\test.txt
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : EmptyPipeElement
And if i modified again the script :
$fecha = get-date -Format D
$alluser = @()
$todayMidnight = (Get-Date -Hour 0 -Minute 0 -Second 0).AddMinutes(-1)
$events = Get-VIEvent -start $todayMidnight.AddDays(-1) -Finish $todayMidnight.AddDays(-0)
foreach ($event in $events) {if ($event.fullFormattedMessage -match "User(.*)@\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b logged in") {Write-Host ("User" + $matches[1] + " logged in at:" + $event.createdTime)} }
$alluser += $events
$alluser | select UserName,IpAddress,CreatedTime ,FullFormattedMessage | out-file c:\ReportesvCENTER\Usuarios-conectadosPav-$(Get-Date -f yyyyMMdd-hhmm)-$fecha.txt
I send the output to a file but i dont send the result equal from the console.
Can anybody helpe please?
Thanks
Carlos
You should only use Write-Host sparingly. instead of Write-host try using Write-Output. You can also build the string without Write-Host ie "User $($matches[1])logged in at: $($event.createdTime)"
Remember that write-host can not be redirected to a different output, just like the Format cmdlets.
I could write this using an object as an output and putting it in to a function.
$events = Get-VIEvent -MaxSamples 100
function Get-UserLogins
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true,Position=0)]
$Events
)
foreach ($event in $events)
{
if ($event.fullFormattedMessage -match "User(.*)@\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b logged in")
{
New-Object -TypeName PSObject -Property @{'User'=$Matches[1];'LogginTime'=$Event.CreatedTime}
}
}
}
You should only use Write-Host sparingly. instead of Write-host try using Write-Output. You can also build the string without Write-Host ie "User $($matches[1])logged in at: $($event.createdTime)"
Remember that write-host can not be redirected to a different output, just like the Format cmdlets.
I could write this using an object as an output and putting it in to a function.
$events = Get-VIEvent -MaxSamples 100
function Get-UserLogins
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true,Position=0)]
$Events
)
foreach ($event in $events)
{
if ($event.fullFormattedMessage -match "User(.*)@\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b logged in")
{
New-Object -TypeName PSObject -Property @{'User'=$Matches[1];'LogginTime'=$Event.CreatedTime}
}
}
}
The ForEach doesn't place any objects in the pipeline.
You can do
$report = foreach ($event in $events) {
if ($event.fullFormattedMessage -match "User(.*)@\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b logged in") {
Write-Host ("User" + $matches[1] + " logged in at:" + $event.createdTime)
}
}
$report | out-file c:\ReportesvCENTER\test.txt
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Lucd,
THanks for your quickly reply.
Yesterday i test your function
Get-VISessionInfo was geat.
But i need a history report.
Best regards
And all my support for Belgium!
Hi again,
How i see between 04:01:31-04:01:37 four entrys of the same user?
User backupsvahcz logged in at:03/21/2016 04:01:37
User backupsvahcz logged in at:03/21/2016 04:01:36
User backupsvahcz logged in at:03/21/2016 04:01:36
User backupsvahcz logged in at:03/21/2016 04:01:31
Best regards
Script with Connect-VIServer ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes,
Best regards