需要通过powershell搜索带有最新时间戳的Windows文件

时间:2018-02-08 05:39:53

标签: windows powershell

我有项目分配给我,首先我需要找到文件在服务器中的目录,我写的代码

    $drive = get-psdrive |select root |select-string -pattern ':'
 Write-Host $drive

     foreach ($a in $drive)
        {
            Get-ChildItem $a -recurse -filter  "*DBaEnvProd*" |select  directory        
        }
there shd be one output a there will be only one dbenvprod on server
how to get one value 

输出iam得到

Get-ChildItem : Cannot find drive. A drive with name '@{Root=C' does not exist.
At D:\temp.ps1:6 char:26
+             Get-ChildItem  <<<< $a -recurse -filter  "*DBaEnvProd*" |select  directory
Get-ChildItem : Cannot find drive. A drive with name '@{Root=D' does not exist.
At D:\temp.ps1:6 char:26
+             Get-ChildItem  <<<< $a -recurse -filter  "*DBaEnvProd*" |select  directory
Get-ChildItem : Cannot find drive. A drive with name '@{Root=E' does not exist.
At D:\temp.ps1:6 char:26
+             Get-ChildItem  <<<< $a -recurse -filter  "*DBaEnvProd*" |select  directory
Get-ChildItem : Cannot find drive. A drive with name '@{Root=F' does not exist.
At D:\temp.ps1:6 char:26
+             Get-ChildItem  <<<< $a -recurse -filter  "*DBaEnvProd*" |select  directory
Get-ChildItem : Cannot find drive. A drive with name '@{Root=Z' does not exist.
At D:\temp.ps1:6 char:26
+             Get-ChildItem  <<<< $a -recurse -filter  "*DBaEnvProd*" |select  directory

2 个答案:

答案 0 :(得分:1)

添加-InputObject参数:

Select-String -InputObject {$_.Root} -Pattern ':'

答案 1 :(得分:0)

Get-PSDrive | ForEach-Object {Get-ChildItem -LiteralPath $_.Root -Filter "*DBaEnvProd*" -Recurse}