Forum Replies Created
-
AuthorPosts
-
December 12, 2020 at 5:02 pm in reply to: Find and replace strings with REGEX and incremental replacement string #279129
I recommend using regex101.com to test and build your regular expressions:
It’s not clear if you want the RequestId and Order in the same string or not?
PowerShell1<requestId>\w{6}-\w{6}-\w{6}.+Order:\s\w{6}-\w{6}-\w{6}Would match the whole string up to the end of the order number.
PowerShell1(Order:\s\w{6}-\w{6}-\w{6})Would match just the order portion.
For the users, you just need to find a regular expression that matches your examples.
PowerShell1User.\d+Would match User, followed by any character, followed by 1 or more digits.
December 11, 2020 at 12:29 pm in reply to: Powershell – Delete line from text file if it contains certain string #278895You could try Select-String which is usually faster than Get-Content:
PowerShell1(Select-String -Path .\test.txt -Pattern $strings -NotMatch).Line | Set-Content .\file2.txtIf it’s only two columns, you could import the CSV into a hashtable and see if the key (column 1) already exists, if it does, add it to existing key (hashtables can’t contain duplicate keys).
PowerShell12345678910111213141516171819202122232425262728293031323334353637383940$data = @"col1,col2ab,data1cd,data2ef,data1ab,data2cd,data3"@$csv = ConvertFrom-Csv $data$HashTable = @{}foreach($row in $csv) {if ($row.col1 -in $hashTable.Keys) {$hashTable[$row.col1] = $hashTable[$row.col1] + " | $($row.col2)"}else {$hashTable[$row.col1] = $row.col2}}$hashTableOutput:Name Value---- -----cd data2 | data3ef data1ab data1 | data2December 11, 2020 at 10:22 am in reply to: Question on Unicode characters display in PS native console. #278817Windows Terminal is what you’re after. Unicode support and much, much more besides:
December 11, 2020 at 7:02 am in reply to: Find and replace strings with REGEX and incremental replacement string #278742For sure. Just add the variable at the top of the script:
PowerShell1$export = $true #change to $false to turn off exportingThen modify the export section:PowerShell12345678if ($export) {[PSCustomObject] @{'Replacement Value' = $replace'Original Value' = $find} | Export-csv $changeLog -NoTypeInformation -Append -NoClobber}Just a case of looping over the files in $FileBrowser.FileNames
PowerShell12345678910111213141516171819202122232425262728$wordtochange = New-TemporaryFile@'Anna:AnnamariaSugar:AgaveAndroid:Phone'@ | Set-Content $wordtochangeAdd-Type -AssemblyName System.Windows.Forms$fileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{Multiselect = $true}$null = $fileBrowser.ShowDialog()if ($fileBrowser.FileNames) {foreach ($file in $fileBrowser.FileNames) {$textchange = Get-Content $file -RawGet-Content $wordtochange | foreach {$find,$replace = $_ -split ':'$textchange = $textchange -creplace "\b$find(?=\W)\b",$replace}$textchange | Set-Content $file #Warning: this overwrites the original file.}}Specify the same domain controller with the -Server parameter for both cmdlets. You’re probably hitting a different DC when you run Set-ADAccountExpiration and the new account hasn’t replicated.
It’s \b \b to set the boundary, not /b /b.
That works OK for me:
PowerShell1$textchange = $textchange -creplace "\b$find(?=\W)\b",$replaceDecember 10, 2020 at 5:24 am in reply to: Find and replace strings with REGEX and incremental replacement string #278325In that case, I would match the tag as well then use multiple counters:
PowerShell12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758$log = 'E:\Temp\log.txt'$changeLog = 'E:\Temp\ChangeLog.csv'@'<requestId>qwerty-qwer12-qwer56</requestId>Ace of Base Order: Q2we45-Uj87f6-gh65De<something else...<requestId>zxcvbn-zxcv12-zxcv56</requestId><requestId>1234qw-12qw12-123456</requestId> Stevie Wonder <messageId>1234qw-12qw12-123456</msg<reportId>plmkjh8765FGH4rt6As</msg:reportId>something here.,. <requestId>zxcvbn-zxcv12-zxcv56</requestId><reportId>poGd56Hnm9q3Dfer6Jh</msg:reportId> uraaa 123 <keyID>poU6Ghk89edfTG78Jk45GrRt23HzW4pl</msgdc'@ | Set-Content $log -Encoding UTF8$tmp = Get-Content $log -Raw$results = $tmp |Select-String -Pattern '(<ReportId>\w{19})|(<RequestId>\w{6}-\w{6}-\w{6})|(<KeyID>\w{32})' -AllMatches |Select-Object -ExpandProperty Matches | Select-Object -ExpandProperty Value | Select-Object -Unique$reportCount = 1$requestCount = 1$keyCount = 1foreach ($result in $results) {if ($result -like '<ReportId>*') {$find = ($result -replace '<ReportId>')$replace = "ReportID-$reportCount"$tmp = $tmp -replace $find,$replace$reportCount++}if ($result -like '<RequestId>*') {$find = ($result -replace '<RequestId>')$replace = "RequestID-$requestCount"$tmp = $tmp -replace $find,$replace$requestCount++}if ($result -like '<KeyID>*') {$find = ($result -replace '<KeyID>')$replace = "KeyID-$keyCount"$tmp = $tmp -replace $find,$replace$keyCount++}[PSCustomObject] @{'Replacement Value' = $replace'Original Value' = $find} | Export-csv $changeLog -NoTypeInformation -Append -NoClobber}$tmp | Set-Content $log -Encoding UTF8-
This reply was modified 1 month, 2 weeks ago by
Matt Bloomfield. Reason: Reformatted a long line for readability
December 9, 2020 at 6:12 pm in reply to: Find and replace strings with REGEX and incremental replacement string #278211Select-String should accept an array of strings for the Pattern, although that didn’t seem to work. However, you can put two patterns in the regex which is what I’ve done.
I also added a Select-Object -Unique so that the count matches the replacement value if there’s more than one of the same value.
To keep the changes, I opted to use a custom object and export it to a CSV file as it’s easy to read.
PowerShell1234567891011121314151617181920212223242526272829$log = 'E:\Temp\log.txt'$changeLog = 'E:\Temp\ChangeLog.csv'@'<requestId>qwerty-qwer12-qwer56</requestId>Ace of Base Order: Q2we45-Uj87f6-gh65De<something else...<requestId>zxcvbn-zxcv12-zxcv56</requestId><requestId>1234qw-12qw12-123456</requestId> Stevie Wonder <messageId>1234qw-12qw12-123456</msg<reportId>plmkjh8765FGH4rt6As</msg:reportId>something here.,. <requestId>zxcvbn-zxcv12-zxcv56</requestId>reportId>poGd56Hnm9q3Dfer6Jh</msg:reportId> uraaa 123 <keyID>poU6Ghk89edfTG78Jk45GrRt23HzW4pl</msgdc'@ | Set-Content $log -Encoding UTF8$tmp = Get-Content $log -Raw$results = $tmp | Select-String -Pattern '(\w{6}-\w{6}-\w{6})|(<keyID>\w{32})' -AllMatches | Select-Object -ExpandProperty Matches | Select-Object -ExpandProperty Value | Select-Object -Unique$count = 1foreach ($result in $results) {$tmp = $tmp -replace $result,"Replace-$count"[PSCustomObject] @{'Replacement Value' = "Replace-$count"'Original Value' = $result} | Export-csv $changeLog -NoTypeInformation -Append -NoClobber$count++}$tmp | Set-Content $log -Encoding UTF8Can be done on one line but I’ve split it at the | for readability on the forum.
PowerShell123Get-Content E:\Temp\dictionary.txt |foreach {$_ -replace $_,(($_ -split ':')[1,0] -join ':') |Out-File E:\Temp\reversedDictionary.txt -Append}-
This reply was modified 1 month, 2 weeks ago by
Matt Bloomfield. Reason: Formatting
Thanks for reformatting, it’s working OK now.
December 9, 2020 at 5:49 am in reply to: Find and replace strings with REGEX and incremental replacement string #277932PowerShell12345678910111213141516171819202122$log = 'E:\Temp\log.txt'@'<requestId>qwerty-qwer12-qwer56</requestId>Ace of Base Order: Q2we45-Uj87f6-gh65De<something else...<requestId>zxcvbn-zxcv12-zxcv56</requestId><requestId>1234qw-12qw12-123456</requestId> Stevie Wonder <messageId>1234qw-12qw12-123456</msg'@ | Set-Content $log -Encoding UTF8$tmp = Get-Content $log -Raw$results = $tmp | Select-String '\w{6}-\w{6}-\w{6}' -AllMatches | Select-Object -ExpandProperty Matches | Select-Object -ExpandProperty Value$count = 1foreach ($result in $results) {$tmp = $tmp -replace $result,"Replace-$count"$count++}$tmp | Set-Content $log -Encoding UTF8-contains is for checking if a collection contains a object, it’s not meant for string comparisons. Try changing your statement to
if ($user -notlike ‘*.adm’) {}
Looks like your variables are mixed up in the else statement too.
-
This reply was modified 1 month, 2 weeks ago by
Matt Bloomfield. Reason: Added a missing word
-
This reply was modified 1 month, 2 weeks ago by
Matt Bloomfield. Reason: Changed a word for clarity
December 7, 2020 at 7:13 am in reply to: Invoke-Command with Try/Catch and adding to an external array #277128If the button is missing, you can either switch to Text view and use the CRAYON button or refresh the page a few times. Usually after two or three refreshes the <> comes back for me.
-
This reply was modified 1 month, 2 weeks ago by
-
AuthorPosts