使用Powershell,我想从文件中读取UNC路径。

huangapple go评论117阅读模式
英文:

With Powershell I want to read a UNC-path from within a file

问题

I'm still a powershell rookie, but thanks to you I'm learning ... 使用Powershell,我想从文件中读取UNC路径。

我仍然是PowerShell的新手,但多亏了你,我正在学习... 使用Powershell,我想从文件中读取UNC路径。

I have a simple text file, let's say its name is "text.txt".
In this text file there are some words (bla bla) followed by a UNC-path which is looking somewhat like this:

我有一个简单的文本文件,假设它的名字是"text.txt"。
在这个文本文件中,有一些单词(bla bla),后面跟着一个类似这样的UNC路径:

blah blah jaada jaada \domain.ad.corp\data\dir1\dir1a\dir1ab\filename.txt blah blah jaada jaada

从这个简单的示例文本文件中,我想将UNC文件名读入一个变量,假设变量的名字是$unc_content。

When powershell is asked to give me the content of the variable $unc_content, the answer should look like this:

当使用PowerShell要求提供变量$unc_content的内容时,答案应该如下所示:

$unc_content

\domain.ad.corp\data\dir1\dir1a\dir1ab\filename.txt

How could that be accomplished?
Many many thx for your helpful tipps.

如何完成这个任务?
非常感谢你的有用提示。

英文:

I'm still a powershell rookie, but thanks to you I'm learning ... 使用Powershell,我想从文件中读取UNC路径。

I have a simple text file, let's say its name is "text.txt".
In this text file there are some words (bla bla) followed by a UNC-path which is looking somewhat like this:

blah blah jaada jaada \\domain.ad.corp\data\dir1\dir1a\dir1ab\filename.txt blah blah jaada jaada

From this simple example text file I want to read the UNC-Filename into a variable, let's say the variable's name is $unc_content.

When powershell is asked to give me the content of the variable $unc_content, the answer should look like this:

$unc_content

\\domain.ad.corp\data\dir1\dir1a\dir1ab\filename.txt

How could that be accomplished?
Many many thx for your helpful tipps.

答案1

得分: 1

这段代码首先使用Get-Content读取了“zxc.txt”的内容。然后它定义了一个匹配UNC路径的正则表达式,这似乎是你要找的内容。最后,它使用Select-String来查找正则表达式的所有匹配项,并获取结果匹配的值使用.Matches.Value。

如果文件中只有一个UNC路径,$unc_content将包含该路径作为一个字符串。如果文件中有多个UNC路径,$unc_content将包含一个字符串数组。

请注意,如果文件中没有任何匹配提供的正则表达式的UNC路径,$unc_content将为空或为null。

Get-Content命令用于读取"text.txt"文本文件的内容。

Select-String应用于获取的内容,以查找所有匹配给定模式的子字符串。在这种情况下,我们寻找以两个反斜杠(\)开头,然后是任意数量的字母、数字和其他域字符,然后是目录和文件名(可能还包括其他字符,但不包括特殊路径分隔符字符)。

$unc_content.Matches.Value.Trim()选择使用模式找到的值,构建了一个对象的集合,然后从每个对象中选择只有值,并删除任何前导或尾随空格。

最终的值保存在$unc_content变量中,然后你可以在代码中随后使用这个变量。调用$unc_content将返回找到的UNC路径(\domain.ad.corp\data\dir1\dir1a\dir1ab\filename.txt),不包含额外的空格或其他字符。

英文:

This code first reads the content of "zxc.txt" using Get-Content. Then it defines a regular expression that matches UNC paths, which seems to be what you're looking for. Finally, it uses Select-String to find all occurrences of the regular expression, and gets the value of the resulting matches using .Matches.Value.

If there is only one UNC path in the file, $unc_content will contain that path as a string. If there are multiple UNC paths, $unc_content will contain an array of strings.

Note that if the file doesn't contain any UNC paths matching the provided regular expression, $unc_content will be empty or null.

$content = Get-Content -Path "zxc.txt" # Read the text file's content
    $unc_regex = '\\\\[\w\d.-]+\\([\w\d\s.-]+\\)*[\w\d\s-]+\.[\w\d]+'
    # This regex will match paths that start with \\ and end with a filename.extension, while allowing for spaces and various characters in the filename and directory names.
    
    $unc_content = ($content | Select-String -Pattern $unc_regex).Matches.Value
    Write-Host $unc_content

Or

$unc_content = Get-Content "zxc.txt" | Select-String '\\\\[\w\.\-]+\\[\w\-_\\.]+'
$unc_content = $unc_content.Matches.Value.Trim()
write-host $unc_content

The Get-Content command is used to read the contents of the "text.txt" text file.

Select-String is applied to the obtained content to find all substrings that match the given pattern. In this case, we're looking for a string that starts with two backslashes (\), followed by any number of letters, numbers, and additional domain characters, and then the directory and file name (additional characters may also be present, but not special path separator characters).

$unc_content.Matches.Value.Trim() selects the values found using the pattern, builds a collection of objects from them, and then selects only the values from each object while removing any leading or trailing whitespace.

The resulting value is saved in the $unc_content variable, and you can use this variable in your code thereafter. Calling $unc_content will return the found UNC path (\domain.ad.corp\data\dir1\dir1a\dir1ab\filename.txt) without any additional spaces or other characters.

huangapple
  • 本文由 发表于 2023年3月10日 01:36:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75688168.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定