gpt4 book ai didi

regex - 使用 PowerShell 解析网络共享列表

转载 作者:行者123 更新时间:2023-12-01 11:39:50 25 4
gpt4 key购买 nike

我想解析一个“netuse”文件并在单行/文件中写入网络连接,并在它们之间填充一个“#”

像这样:

R:\\SERVER\SHARE1#S:\\SERVER\SHARE2#Y:\\SERVER\SHARE3\FOLDER

我的 netuse 文件是这样的:

New connections will be remembered.

Status Local Remote Network

-------------------------------------------------------------------------------
OK R: \\SERVER\SHARE1 Microsoft Windows Network
OK S: \\SERVER\SHARE2 Microsoft Windows Network
OK Y: \\SERVER\SHARE3\FOLDER Microsoft Windows Network

The command completed successfully.

我的测试代码:

$NetUseFile= Get-Content "C:\temp\netuse.txt"
$NetworShareStr = $NetUseFile | Select-String -Pattern "^.*\s+([a-zA-Z]):\s+(\\\\[a-zA-Z0-9_-]+\\.+)\s+Microsoft Windows Network$";

TrimStart(“确定”)?

我尝试了几种方法,但都没有成功。谁能帮我解决这个问题?

编辑:

@TheMadTechnician 我知道..但我需要解析那个文件...我又添加了两个字段

HOSTNAME:DESKTOP1
USERNAME:USER1

我想要这样的输出:

USER1,DESKTOP1,R:\\SERVER\SHARE1#M:\\SERVER\SHARE2#X:\\SERVER\SHARE3\FOLDER

$NetUseFile= Get-Content "C:\temp\netuse.txt"

$HostnameStr = $NetUseFile | Select-String -Pattern "HOSTNAME:"
$UsernameStr = $NetUseFile | Select-String -Pattern "USERNAME:"
$test = New-Object -TypeName PSObject
$test | Add-Member –MemberType NoteProperty –Name 'Hostname' –Value
$HostnameStr.ToString().Split(":")[1]
$test | Add-Member –MemberType NoteProperty –Name 'Username' –Value
$UsernameStr.ToString().Split(":")[1]

如何在对象中添加this成员? (对我来说太内联了:)

gc C:\temp\meilehj.txt |?{$_ -match "^.*\s+([a-zA-Z]:)\s+(.+?)\s"} 
| foreach {
Add-Member -InputObject $test –MemberType NoteProperty -Name 'Share' -Value
$Matches[1]
Add-Member -InputObject $test –MemberType NoteProperty -Name 'Path' -Value
$Matches[2]
??
}

如何使用“#”加入 Share 和 Path?

编辑:

@TheMadTechnician非常感谢!!

最佳答案

不需要你的输入文件; PowerShell 本身可以为您提供所有信息。

$drives = get-psdrive -psprovider FileSystem|Where-Object {$_.DisplayRoot -like '\\*'};
$AllMappings = "";
foreach ($Drive in $Drives) {
$AllMappings += "$($Drive.Name):$($Drive.DisplayRoot)#";
}
# Since there's a trailing hashmark, grab the full string minus the last character
$AllMappings.Substring(0,$AllMappings.Length-1);

关于regex - 使用 PowerShell 解析网络共享列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22308776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com