简体   繁体   English

启动PowerShell脚本,Export-Csv不导出LogonServer变量

[英]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. 我有一台PC,该PC连续登录,运行脚本,然后注销并重新启动以重复该过程。

$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 ie 3 columns of data are filled. 当我手动执行脚本时,将使用正确的信息更新文件,即填充3列数据。 But when the script executes on Windows startup, the LogonServer isn't wrriten to the CSV file. 但是,当脚本在Windows启动时执行时,LogonServer不会写入CSV文件。 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. 编辑:实际上,它不必是.CSV,而只是我可以放入Excel进行分析的文件。 If there's a better way to write to a file... 如果有更好的方式写入文件...

像这样的东西可以代替:

$LServer = (gwmi Win32_NtDomain | select DomainControllerName | where DomainControllerName -ne $null ).DomainControllerName[0] -replace '\\'

直到用户登录后,才定义LogonServer环境变量。如果您正在计算机启动脚本中运行脚本,则没有用户登录。也许如果您将脚本作为“登录”脚本运行,则可以执行此操作。

I would configure an autologon user: http://support.microsoft.com/kb/324737 我将配置一个自动登录用户: http : //support.microsoft.com/kb/324737

Make sure your logon user is a domain user and also a member of the local admins group. 确保您的登录用户是域用户,并且也是本地管理员组的成员。 Then set the script to run either on startup from the startup folder or in: HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run 然后将脚本设置为在启动时从启动文件夹或在以下位置运行:HKEY_LOCAL_MACHINE \\ SOFTWARE \\ Microsoft \\ Windows \\ CurrentVersion \\ Run

Configure the job in the registry to launch via cmd/bat file (just like the scheduled task). 在注册表中配置作业以通过cmd / bat文件启动(就像计划的任务一样)。 The script will run and produce output for any user who logs on who has rights to the folder and file even if you break your autologon user. 该脚本将为任何有权访问该文件夹和文件的登录用户运行并生成输出,即使您破坏了自动登录用户也是如此。 I would also add a few second delay using the shutdown command so that the reboot part can be easily broken with shutdown -a 我还将使用shutdown命令添加几秒钟的延迟,以便使用shutdown -a可以轻松破坏重新启动部分

The LOGONSERVER environment variable, if set, refers to the computer that logged on the current user (ie, the user account running your script). LOGONSERVER环境变量(如果已设置)是指登录到当前用户(即运行脚本的用户帐户)的计算机。 For a local account, LOGONSERVER (again, if set) will be the local computer. 对于本地帐户,LOGONSERVER(同样,如果已设置)将是本地计算机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM