split string with variable length in powershell - 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!

Related

How to trim the branch name "refs/heads/feature/branch name" in 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!

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!

Convert a string of ones and zeros to hex

I'm looking for a simple way to convert a string of 1's and 0's to its hex equivalent.
[string]$bintest = "1100110101011111"
[long]$testnumber = [System.Convert]::ToInt32($bintest,2)
"{0:x4}" -f $testnumber
The code above works as needed but it seems like too many steps.
Is there a way to avoid converting the string to decimal before convering it to hex?
the shortest answer I could create
'{0:x}' -f [Convert]::ToInt32('1100110101011111',2)
Hope it helps
Edit: As I refreshed I saw the comment of #Ansgar Wiechers, two persons one idea I guess.

qpython3 printing variable after string

I'm wanting to print a variable after a string but the variable doesn't show.
I'm following a book called learning python the hard way.
In it, it asks to declare a variable:
cars = 100
Print ("how many cars are there"), (cars)
And print it after a string.
However, it only prints the string. I've tried doing it in different ways with brackets, parentheses, etc. to no avail. Can someone help me out please?
cars = 100
print ("text", (cars) ,"text")
Thanks anyways

converting string to number in powershell

I am trying to convert a string to a 2 decimal place value
I have a script that has a line of code which is
(get-mailboxdatabase xxx -status).databasesize
This returns the size something like
1.008 GB (1,082,195,968 bytes)
I want to be able to convert this to a number (1.008) and can't work out how to do it.
I know about ToGB() but that only works when running the script from EMS.
I need to be able to run the script in Powershell and not EMS as the script does other things.
How do I convert the value to a number?
TIA
Andy
If you're using implicit remoting (it sounds like you are), that's going to be a [string], so you'll need to use string methods.
'1.008 GB (1,082,195,968 bytes)' -replace '^([0-9.]+).+','$1'
1.008
This is very easy, just make a cast and divide with 1GB.
PS C:\> [int]"1082195968"/1GB
1,00787353515625
Or in your case
([int](get-mailboxdatabase xxx -status).databasesize)/1GB
You can convert numbers to the various number types using standard .NET framework types and conversions. For example:
> $var = [decimal]::Parse("1.008")
> $var.GetType().Name
Decimal
Extracting just that portion of the string from the input is a separate issue, and it's hard to tell from your question which is giving you trouble. To get just the number, you might use something like this:
$input = (get-mailboxdatabase xxx -status).databasesize
$strSize = $input.Split(' ')[0]
Or you can use a regular expression. It all depends on how variable your size is.
try this:
$val= (get-mailboxdatabase xxx -status).databasesize
[decimal]([regex]::Match( $val, '(^.+)GB' )).groups[1].value