Search code examples
powershelllogonserver

Startup PowerShell script, Export-Csv not exporting LogonServer variable


I have a PC which is continuously logging in, running a script, then logging off and rebooting to repeat the process.

$StartTime = Get-Date
...
(other code here, including delay)
...
$LServer = $env:logonserver
$ShutTime = Get-Date

New-Object -TypeName PSCustomObject -Property @{
    StartTime = $StartTime
    ShutTime = $ShutTime
    LogonServer = $LServer 
} | Export-Csv -Path "C:\Apps\DCResults.csv" -Append

When I execute the script manually, the file is updated with the correct information i.e. 3 columns of data are filled. But when the script executes on Windows startup, the LogonServer isn't wrriten to the CSV file. That column remains blanks, whereas the other two columns are correct.

EDIT: And actually, it doesn't have to be a .CSV, just a file I can put in Excel to do analysis on. If there's a better way to write to a file...


Solution

  • The LOGONSERVER environment variable, if set, refers to the computer that logged on the current user (i.e., the user account running your script). For a local account, LOGONSERVER (again, if set) will be the local computer.