简体   繁体   English

从文本文件中选择字符串的一部分,然后使用powershell将其写入另一个字符串

[英]Select part of string from text file and write it to another with powershell

i´m currently working on a script that reads out the standard printer on a workstation and write it to a textfile. 我目前正在处理一个脚本,该脚本可读取工作站上的标准打印机并将其写入文本文件。 Now i need to convert or cut the output string and write it to another file in order to set the cutted string as new default printer. 现在,我需要转换或剪切输出字符串并将其写入另一个文件,以便将剪切的字符串设置为新的默认打印机。

eg first textfile content is something like this: 例如,第一个文本文件的内容是这样的:

\\NDPS-mydomain\\myprinter.ou.cn.mydomain.de \\ NDPS-mydomain \\ myprinter.ou.cn.mydomain.de

I'd like to get this: 我想得到这个:

PS-myprinter PS打印机

The amount of characters of "myprinter" is different from printer to printer. 每个打印机的“ myprinter”字符数不同。

I'm stuck at cutting the parts I need out from the complete string. 我被困在从完整的琴弦中切出需要的零件。

Background:I´ve did a big printserver migration from novell to microsoft. 背景:我已经完成了从Novell到Microsoft的大型打印服务器迁移。 In order to affect the users less than possible, i want to set the new microsoft deployed printer with this script as default printer again. 为了尽可能少地影响用户,我想再次将此脚本设置为新的Microsoft部署的打印机作为默认打印机。 Hope this is understandable. 希望这是可以理解的。

Sorry for my bad english. 对不起,我的英语不好。

Thanks in advance 提前致谢

sample code so far: 到目前为止的示例代码:

$name = (get-content env:Computername)

Get-WmiObject -Class Win32_Printer -Filter "Default = $true" | 
    Select-Object - expandProperty Name | 
    Out-File  -filepath C:\"$name"_defaultprinter.txt

Get-Content "C:\"$name"_defaultprinter.txt" | 
    Foreach-Object {$_.Replace("NDPS","PS")} | 
    Set-Content "C:\"$name"_defaultprinter.txt"

Here is the output: 这是输出:

\\\\mydomain\\NDPS-MJP1-0014-SamsungML3471.A-Printers.MJP1.xx-xxxx.xxxxx-xxxxx.DE \\\\mydomain\\NDPS-MJP1-0014-SamsungML3471.A-Printers.MJP1.xx-xxxx.xxxxx-xxxxx.DE \\\\mydomain\\NDPS-MJP1-0014-SamsungML3471.A-Printers.MJP1.xx-xxxx.xxxxx-xxxxx.DE \\\\mydomain\\NDPS-MJP1-0014-SamsungML3471.A-Printers.MJP1.xx-xxxx.xxxxx-xxxxx.DE

Output should be only the PS-MJP1-0014-SamsungML3471 输出应仅为PS-MJP1-0014-SamsungML3471

Try this script instead: 尝试使用以下脚本:

Get-WmiObject -Class Win32_Printer -Filter "Default = $true" | % {
    $_.Name -replace '(?:.*)\\NDPS-([^\.]+)(?:.*)', 'PS-$1'
} | Out-File  -FilePath "C:\$($env:COMPUTERNAME)_defaultprinter.txt"

EDIT : Try running this on one computer. 编辑 :尝试在一台计算机上运行它。 Post all the output as an edit to YOUR question(not in a comment). 将所有输出发布为您的问题的编辑内容(不在评论中)。 If you need to hide some sensitive information, just replace the words(leave special characters etc.): 如果您需要隐藏一些敏感信息,只需替换单词(保留特殊字符等)即可:

Get-WmiObject -Class Win32_Printer -Filter "Default = $true" | % {
    $_.Name

    $_.Name -replace '(?:.*)\\NDPS-(?:[^\\]+)\\(.+?)\.(?:.*)', 'PS-$1'
}

The regex I provided works perfect with one of your samplestrings, so it seems the actual value of $_.Name is not equal to the sample you provided. 我提供的正则表达式可以完美地与您的样本字符串之一配合使用,因此$_.Name _。Name的实际值似乎不等于您提供的样本。 So run the code above and provide output as I said so we get a correct sample. 因此,运行上面的代码并按我所说的提供输出,以便获得正确的样本。

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

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