繁体   English   中英

查找 Powershell 脚本的正确文件路径

[英]Finding The Correct File Path For a Powershell Script

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

    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 的了解有限,我不确定我的情况可能是正确的。 我使用此脚本将我的事件查看器日志复制到新的文件路径以进行组织

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

  1. 以管理员或有权查看安全日志的用户身份打开事件查看器。
  2. 右击左侧的安全日志object,打开属性。

安全日志

或者您可以通过检查注册表来获取 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 上尝试同样的事情吗? 我遇到过类似这样的文件系统/硬盘问题,但可能性不大

暂无
暂无

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

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