select 行开头和结尾
select lines begin with and finish with
我有一个带有 XAML 代码的 Powershell 脚本(用于显示一些 windows 和按钮)
我尝试在文本文件中替换所有以 $computer.selectedvalue 开头和芬兰语为“,1”的行的值。
我的文本文件内容:
COMPUTER01,SITE01,PRINTER01,1
COMPUTER05,SITE02,PRINTER01,0
COMPUTER01,SITE03,PRINTER08,1
end with ,1
默认是打印机,,0
是辅助打印机
Powershell 代码:
$PLPrinterArray = Get-Printer -ComputerName srvprint | select name
foreach ($PLPrinterArray in $PLPrinterArray) {
[void]$WPFCboPL.items.add(",PL,$($PLPrinterArray.Name)")
}
$LDAPComputerArray = Get-ADComputer -filter * -SearchBase"OU=Ordinateurs,DC=MyDC,DC=Mydomain,DC=org" -Properties name
foreach ($LDAPComputerArray in $LDAPComputerArray) {
[void]$WPFCboComputer.items.add($($LDAPComputerArray.Name))
}
$WPFBoutonDefaut.Add_Click({
#If (Get-Content C:\Users\User\desktop\test.txt
$($WPFCboComputer.selectedvalue)^ ",1$") {
#-replace ",1",",0"}
Add-Content -path "C:\Users\User\Desktop\test.txt"
-value"`n$($WPFCboComputer.selectedvalue)$($WPFCboPL.SelectedValue)$($WPFCboLN.SelectedValue),1"
})
是否可以这样做,尤其是在 Button.Add.Click
中?
提前致谢;-)
我建议使用带有 -Regex
和 -File
选项的 switch 语句:
$WPFBoutonDefaut.Add_Click({
$file = 'C:\Users\User\desktop\test.txt'
# Process the input file line by line with regex matching,
# and collect the potentially modified lines in an array.
[array] $modifiedlines =
switch -Regex -File $file {
"^($($WPFCboComputer.SelectedValue),.+,)1$" { $Matches[1] + '0' }
default { $_ } # pass through
}
# Save the modified lines plus the new line back to the file.
# Note: Use -Encoding as needed.
Set-Content $file -Value (
$modifiedLines + "$($WPFCboComputer.SelectedValue)$($WPFCboPL.SelectedValue)$($WPFCboLN.SelectedValue),1"
)
})
我有一个带有 XAML 代码的 Powershell 脚本(用于显示一些 windows 和按钮) 我尝试在文本文件中替换所有以 $computer.selectedvalue 开头和芬兰语为“,1”的行的值。
我的文本文件内容:
COMPUTER01,SITE01,PRINTER01,1
COMPUTER05,SITE02,PRINTER01,0
COMPUTER01,SITE03,PRINTER08,1
end with ,1
默认是打印机,,0
是辅助打印机
Powershell 代码:
$PLPrinterArray = Get-Printer -ComputerName srvprint | select name
foreach ($PLPrinterArray in $PLPrinterArray) {
[void]$WPFCboPL.items.add(",PL,$($PLPrinterArray.Name)")
}
$LDAPComputerArray = Get-ADComputer -filter * -SearchBase"OU=Ordinateurs,DC=MyDC,DC=Mydomain,DC=org" -Properties name
foreach ($LDAPComputerArray in $LDAPComputerArray) {
[void]$WPFCboComputer.items.add($($LDAPComputerArray.Name))
}
$WPFBoutonDefaut.Add_Click({
#If (Get-Content C:\Users\User\desktop\test.txt
$($WPFCboComputer.selectedvalue)^ ",1$") {
#-replace ",1",",0"}
Add-Content -path "C:\Users\User\Desktop\test.txt"
-value"`n$($WPFCboComputer.selectedvalue)$($WPFCboPL.SelectedValue)$($WPFCboLN.SelectedValue),1"
})
是否可以这样做,尤其是在 Button.Add.Click
中?
提前致谢;-)
我建议使用带有 -Regex
和 -File
选项的 switch 语句:
$WPFBoutonDefaut.Add_Click({
$file = 'C:\Users\User\desktop\test.txt'
# Process the input file line by line with regex matching,
# and collect the potentially modified lines in an array.
[array] $modifiedlines =
switch -Regex -File $file {
"^($($WPFCboComputer.SelectedValue),.+,)1$" { $Matches[1] + '0' }
default { $_ } # pass through
}
# Save the modified lines plus the new line back to the file.
# Note: Use -Encoding as needed.
Set-Content $file -Value (
$modifiedLines + "$($WPFCboComputer.SelectedValue)$($WPFCboPL.SelectedValue)$($WPFCboLN.SelectedValue),1"
)
})