How to trim the branch name "refs/heads/feature/branch name" in Powershell? - powershell

In Powershell, the result of Pipeline variable $(Build.SourceBranch)="refs/heads/feature/branchname".
But I need to trim the value and get the output as just "feature/branchname" and delete "refs/heads"
Note 1 : replace() function will solve the issue, but I am looking for other feasible solutions.
Note 2 : Tried with TrimStart() in powershell. But,it is giving the result as "ture/branchname" instead of "feature/branchname"
Can someone please suggest the best way of handling this?
Thanks in advance!

Related

Powershell: Compare filenames to a list of formats

I am not looking for a working solution but rather an idea to push me in the right direction as I was thrown a curveball I am not sure how to tackle.
Recently I wrote a script that used split command to check the first part of the file against the folder name. This was successful so now there is a new ask: check all the files against the naming matrix, problem is there are like 50+ file formats on the list.
So for example format of a document would be ID-someID-otherID-date.xls
So for example 12345678-xxxx-1234abc.xls as a format for the amount of characters to see if files have the correct amount of characters in each section to spot typos etc.
Is there any other reasonable way of tackling that besides Regex? I was thinking of using multiple splits using the hyphens but don't really have anything to reference that against other than the amount of characters required in each part.
As always, any (even vague) pointers are most welcome ;-)
Although I would use a regex (as also commented by zett42), there is indeed an other way which is using the ConvertFrom-String cmdlet with a template:
$template = #'
{[Int]Some*:12345678}-{[String]Other:xxxx}-{[DateTime]Date:2022-11-18}.xls
{[Int]Some*:87654321}-{[String]Other:yyyy}-{[DateTime]Date:18Nov2022}.xls
'#
'23565679-iRon-7Oct1963.xls' | ConvertFrom-String -TemplateContent $template
Some : 23565679
Other : iRon
Date : 10/7/1963 12:00:00 AM
RunspaceId : 3bf191e9-8077-4577-8372-e77da6d5f38d

Export multiple line in CSV (Not multi-property value into CSV)

First of all I would like to clarify that my question is NOT: How to export multi-valued properties (array) into csv file.
My use case is the following: I am creating a code to audit Hyper-V infrastructure and I would like to have in one CSV cell multiple lines. For example: NIC1, NIC2 ... Disk 1, Disk 2.
I do not want to use join operator and have it on a single line.
I am almost certain that there was an article about that and that i did managed to achieve the goal, but unfortunately I am not able to find neither the article, neither the scrip in which I have used it.
Any suggestions or ideas would be highly appreciated! :)
The hint was submitted by LeroyJD in the comments.
#LeroyJD, Thank you very much, helped a lot! :)
To summarize for future reference: Yes, it is possible by using the new line backtick n to present in multiple lines. Sample code:
[PSCustomObject]#{
Value1 = 'Hello'
Value2 = "Hello `nWorld"
} | Export-Csv C:\TEMP\multiline.csv -NoTypeInformation
Invoke-Item C:\TEMP\multiline.csv
Which results into:
Thanks again to LeroyJD for the hint!

Microsoft graph Mail Search Strict value

I have an issue with the search parameters. I want to pass a phrase in my query. For exemple i'm looking for emails where the subject is "Test 1".
For this i'm doing a get on this ressource.
https://graph.microsoft.com/v1.0/me/messages?$search="subject:Test 1"
But the behaviour of this query is : Looking for mails that contains "Test" in the subject OR 1 in any other fields.
Refering to the KQL Syntax
A phrase (includes two or more words together, separated by spaces; however, the words must be enclosed in double quotation marks)
So, to do what i want i have to put double quotes (") around my phrase to do a strict value search. Like below
subject:"Test 1"
The problem it's at this point. Microsoft graph api already use double quotes (") after the parameters $search.
?$search="Key words"
So I can't do what is mentioned in the KQL doc.
https://graph.microsoft.com/v1.0/me/messages?$search="subject:"Test 1""
It's throwing an error :
"Syntax error: character '1' is not valid at position 15 in '\"subject:\"test 1\"\"'.",
It's an expected behaviour. I was pretty sure it will not work.
If someone has any suggestions for a solution or a workaround, I'm a buyer.
What I've already tried so far :
Use simple quote
Remove the quotes right after $select=
Remove the subject part $select="Test 1", same behaviour as the first request mentioned in this post. It will looks for emails that contain "test" or "1".
Best regards.
EDIT :
After sasfrog's anwser :
I used $filter : It works well with simple operator AND, OR.I have some errors by using the Not Operator. And btw you have to use the orderby parameter to show the result by date and add the field in filter parameters.
Exemple 1 (working, what I asked for first) :
https://graph.microsoft.com/v1.0/me/messages/?$orderby=receivedDateTime desc &$filter=receivedDateTime ge 1900-01-01T00:00:00Z AND contains(subject,'test 1')
Exemple 2 (not working)
https://graph.microsoft.com/v1.0/me/messages/?$orderby=receivedDateTime desc &$filter=(receivedDateTime ge 1900-01-01T00:00:00Z AND contains(subject,'test 1')) NOT(contains(from/EmailAddress/address,[specific address]))
EDIT 2
After some test with the filter parameters.
The NOT operator is still not working so to workaround use "ne" (non-equals)
the example 2 becomes :
https://graph.microsoft.com/v1.0/me/messages/?$orderby=receivedDateTime desc&$filter=(receivedDateTime ge 1900-01-01T00:00:00Z AND contains(subject,'test 1')) AND (from/EmailAddress/address ne [specific address])
UPDATE : OTHER SOLUTION WITH $search
Using $filter is great but it looks like it was sometimes pretty slow. So I found a workaround aboutmy issue.
It's to use AND operator between all terms.
Exemple 4 :
I'm looking for the mails where the subject is test 1;
Let value = "test 1". So you have to splice it by using space separator. And after write some code to manipulate this array, to obtain something like below.
$search="(subject:test AND subject:1)"
The brackets can be important if you use a multiple fields search. And VoilĂ .
Not sure if it's sufficient for what you're doing, but how about using the contains function within a filter query instead:
https://graph.microsoft.com/v1.0/me/messages?$filter=contains(subject,'Test 1')
Sounds like you're already looking at the doco but here it is just in case.
Update also, this worked for me using the search method:
https://graph.microsoft.com/v1.0/me/messages?$search="subject:'Test 1'"

split string with variable length in powershell

I guess my question is a bit stupid but I am a total newbie. I have to get substrings out of strings like this:
DistinguishedName : CN=xyz,OU=Users,OU=abc,OU=asd,DC=qwe,DC=yxc
that should look like CN=xyz,OU=Users,OU=abc,OU=asd,DC=qwe,DC=yxc. They are variable in length. Do you guys have any suggestions how to do it?
Thanks a lot for your help!

Powershell and logparser arguments

Im trying to run some logparser commands from powershell but Im having issues with passing the arguments across correctly, heres the excert from my script;
d:\scripting\smtplogs\logparser\logparser.exe "SELECT TOP 50 Receiver, COUNT() INTO %TMPOutput%\TopReceiversNDRALL.gif FROM %TempDir%\PostAll.log WHERE Sender LIKE '<>' AND Receiver NOT LIKE '%%go-fmtopper%%' GROUP BY Receiver ORDER BY COUNT() DESC" -i:TSV -iSeparator:space -headerRow:OFF -iHeaderFile:"header3.tsv" -lineFilter:"+10." -o:CHART -chartType:ColumnClustered -config:MyScript.js -chartTitle:"Receivers for NULL messages ALL for %DateGraph%"
Ive read loads about encapsulating arguments but cant seem to figure out how to make this work!
Any help that you guys could provide would be very appreciated.
Thanks
For a complex string parameter, try to pass the argument using powershell here-strings so that you wouldn't have to worry about escaping single/double quotes
UPDATE1: I couldn't get the fomratting working so here is the screenshot.
UPDATE2: I was able to format the code finally.
d:\scripting\smtplogs\logparser\logparser.exe #"
SELECT TOP 50 Receiver, COUNT()
INTO %TMPOutput%\TopReceiversNDRALL.gif
FROM %TempDir%\PostAll.log
WHERE Sender LIKE ''
AND Receiver NOT LIKE '%%go-fmtopper%%'
GROUP BY Receiver
ORDER BY COUNT() DESC"
-i:TSV
-iSeparator:space
-headerRow:OFF
-iHeaderFile:"header3.tsv"
-lineFilter:"+10."
-o:CHART
-chartType:ColumnClustered
-config:MyScript.js
-chartTitle:"Receivers for NULL messages ALL for %DateGraph%
"#
Make sure that you add a new line between the here-string monikers #" and "#.
FYI, if you don't need any PowerShell variable expansion then you are better off using single quoted here strings. For example the following double quoted here string might cause you some grief:
#"
$(get-process <some_core_os_process> | stop-process)
"#
where the following is harmless:
#'
$(get-process <some_core_os_process> | stop-process)
'#
It's not likely your here string would contain something so obvious but a simple $f would resolve to nothing i.e. it would disappear from the original string. Unless, of course, $f was defined and set to something other than null or empty.