0

This script tells me how many times, in the last X amount of days, our workstations have been logged on.

My code works fine when I write the number of days inside (Get-Date).AddDays(-$days). For example:

$logins=(Invoke-Command -ComputerName $workstation -ScriptBlock { Get-EventLog System -Source Microsoft-Windows-Winlogon -InstanceId 7001 -After (Get-Date).AddDays(**-90**)} -ErrorAction SilentlyContinue| Select-Object -Property Index,  @{label="DayOfYear";expression={$_.TimeWritten.DayOfYear}} | Select-Object DayOfYear -ExpandProperty DayOfYear | sort -Unique).count
    Write-Host $logins " - " $workstation

It outputs:

# of logins in the last 90 days.

52  -  PC1
73  -  PC2
64  -  PC3

etc

However when I try to use Read-Host so the user can decide the number of days:

$days=Read-Host "How many days of history?"

The code returns:

0  -  PC1
0  -  PC2
0  -  PC3
etc

I tried converting the Read-Host output to integer:

$days=Read-Host "How many days of history?"
$number_of_days=[Convert]::ToInt32($days)

$logins=(Invoke-Command -ComputerName $workstation -ScriptBlock { Get-EventLog System -Source Microsoft-Windows-Winlogon -InstanceId 7001 -After (Get-Date).AddDays(**-$number_of_days**)}...

AND

[int]$days=Read-Host "How many days of history?"
$logins=(Invoke-Command -ComputerName $workstation -ScriptBlock { Get-EventLog System -Source Microsoft-Windows-Winlogon -InstanceId 7001 -After (Get-Date).AddDays(**-$days**)}...

However I still get the same results.

# of logins in the last 90 days.
0  -  PC1
0  -  PC2
0  -  PC3

etc

I was wondering if someone could help me figuring out what I am missing. Thank you

0 Answers0