Удалить часть строки в powershell в хеш-таблице - PullRequest
0 голосов
/ 22 ноября 2018

Я получил список драйверов

$HashTable = Get-WindowsDriver –Online -All | Where-Object {$_.Driver -like "oem*.inf"} | Select-Object Driver, OriginalFileName, ClassDescription, ProviderName, Date, Version
Write-Host "All installed third-party drivers" -ForegroundColor Yellow
$HashTable | Sort-Object ClassDescription | Format-Table

В таблице указан полный путь к файлу inf в столбце OriginalFileName.Мне нужно сократить полный путь, например,

C:\Windows\System32\DriverStore\FileRepository\pantherpointsystem.inf_amd64_bde4cf569a728803\pantherpointsystem.inf

до

pantherpointsystem.inf.

И так во всех строках.

Ответы [ 3 ]

0 голосов
/ 22 ноября 2018

Другим решением будет RegEx один:

$FullPath = "C:\Windows\System32\DriverStore\FileRepository\pantherpointsystem.inf_amd64_bde4cf569a728803\pantherpointsystem.inf"
$FullPath -match '.*\\(.*)$'
$Required = $matches[1]

.*\\(.*)$ соответствует всем символам после последней черты \ и до конца строки $

0 голосов
/ 22 ноября 2018

Чтобы отделить имя файла от полного пути, вы можете использовать командировку Powershells Split-Path следующим образом:

$FullPath = "C:\Windows\System32\DriverStore\FileRepository\pantherpointsystem.inf_amd64_bde4cf569a728803\pantherpointsystem.inf"
$FileName = $FullPath | Split-Path -Leaf

или использовать .NET следующим образом:

$FullPath = "C:\Windows\System32\DriverStore\FileRepository\pantherpointsystem.inf_amd64_bde4cf569a728803\pantherpointsystem.inf"
$FileName = [System.IO.Path]::GetFileName($FullPath)

В вашемВ этом случае я бы использовал вычисляемое свойство для заполнения Hashtable:

$HashTable = Get-WindowsDriver –Online -All | 
                Where-Object {$_.Driver -like "oem*.inf"} | 
                Select-Object Driver, @{Name = 'FileName'; Expression = {$_.OriginalFileName | Split-Path -Leaf}},
                              ClassDescription, ProviderName, Date, Version

Write-Host "All installed third-party drivers" -ForegroundColor Yellow
$HashTable | Sort-Object ClassDescription | Format-Table
0 голосов
/ 22 ноября 2018

Попробуйте это -

$FullPath = "C:\Windows\System32\DriverStore\FileRepository\pantherpointsystem.inf_amd64_bde4cf569a728803\pantherpointsystem.inf"
$Required = $FullPath.Split("\")[-1]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...