【问题标题】:Finding The Correct File Path For a Powershell Script查找 Powershell 脚本的正确文件路径
【发布时间】:2020-10-13 12:14:21
【问题描述】:

所以我一直在家里编写这个脚本

    Copy-Item "$env:SystemRoot\System32\Winevt\Logs\Security.evtx" "C:\CommFiles\LogFile_$(get-date -uformat %d-%m-%Y-%H.%M.%S).evtx"
if(-not $?) { 
Write-Warning "Copy Failed" 
} else {
Remove-Item "$env:SystemRoot\System32\Winevt\Logs\Security.evtx"
}

我知道它可以工作,因为我在家里使用它,它的文件路径与我在办公室使用的相同,但我不断收到此警告

Copy-Item : Could not find a part of the path 'C:\windows\System32\Winevt\Logs\Security.evtx'.
At line:1 char:1
+ Copy-Item "$env:SystemRoot\System32\Winevt\Logs\Security.evtx" "C:\Co ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [Copy-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : 
System.IO.DirectoryNotFoundException,Microsoft.PowerShell.Commands.CopyItemCommand

我怀疑我不在正确的目录中,但由于我对 powershell 的了解有限,我不确定适合我的情况的目录是什么。我正在使用此脚本将我的事件查看器日志复制到新的文件路径以进行组织

【问题讨论】:

  • 出现问题的机器上运行的 PowerShell 和操作系统版本是什么?
  • @AdminOfThings 据我所知,这台机器正在运行最新版本的 PowerShell,操作系统版本是 windows 10 企业版 10.0.17763 Build 17763
  • 如果你手动进入文件夹C:\windows\System32\Winevt\Logs,你能看到Security.evtx文件。你的权限允许你复制它吗?
  • @Theo 我有管理员权限,但这没关系,我使用下面的解决方案来获取正确的文件路径,它仍然做同样的事情。

标签: powershell directory filepath event-viewer


【解决方案1】:

文件路径可以通过以下步骤找到:

  1. 以管理员或有权查看安全日志的用户身份打开事件查看器。
  2. 右键单击左侧的安全日志对象并打开“属性”。

或者您可以通过检查注册表来获取 powershell 的完整路径 - 请注意,这还需要以管理员用户身份运行 powershell:

PS C:\> (Get-ItemProperty HKLM:\system\CurrentControlSet\Services\EventLog\Security\).file

C:\WINDOWS\System32\winevt\Logs\Security.evtx

由于您的错误特别指出DirectoryNotFound,请尝试查找无法打开的目录:

gci C:\
gci C:\windows\
gci C:\windows\System32\
gci C:\windows\System32\Winevt\
gci C:\windows\System32\Winevt\Logs\

并调查它的权限:

(get-acl C:\Windows\System32\winevt\).Access | select IdentityReference,FileSystemRights

IdentityReference                FileSystemRights
-----------------                ----------------
NT AUTHORITY\Authenticated Users Read, Synchronize
NT AUTHORITY\SYSTEM              FullControl
BUILTIN\Administrators           FullControl
NT SERVICE\EventLog              DeleteSubdirectoriesAndFiles, Write, ReadAndExecute, Synchronize

如果那里一切正常,是否考虑在另一台 PC 上尝试相同的操作?我遇到过类似这样的文件系统/硬盘问题,但可能性不大

【讨论】:

  • 这只是确认我的文件路径是正确的,我知道为什么它仍然无法工作。我拥有完整的管理员权限,并且我以管理员身份运行 powershell。我还需要更多帮助。
  • @Zizzay 我已经添加了一些进一步的建议 - 希望这会有所帮助
  • 所以我能够得出结论,我想要将复制的文件放入的文件夹还不存在。我不知道为什么,但是在我的家庭桌面上,它创建了文件夹并将其全部填充在相同的确切代码中,但是现在复制功能再次按预期工作,我最后的删除功能仍然不起作用。删除项目:无法删除项目 C:\Windows\System32\winevt\Logs\Security.evtx:进程无法访问文件 'C:\Windows\System32\winevt\Logs\Security.evtx',因为它正被另一个过程。在 line:4 char:7
  • @Zizzay 你可能不得不停止事件日志服务:stop-service eventlog。在 PSv5.1 中,您可以改为使用 Clear-EventLog Security。我不确定目标是什么,但我建议在大多数情况下更改事件查看器中的最大大小和翻转属性。
  • 如果我结束任务,我是否必须使用 powershell 启动一个新操作才能记录更多事件?最终目标是出于审计原因每天都有清晰的事件日志
猜你喜欢
  • 2020-02-05
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
  • 2019-08-23
  • 2017-03-21
  • 2015-02-01
  • 2010-11-11
  • 1970-01-01
相关资源
最近更新 更多