VMware Cloud Community
billyjoel
Enthusiast
Enthusiast
Jump to solution

Split string until the end

Hi all,

I have the below code to get the alarm de finitions that are enabled and I want to split the name/string from the second empty space until the end:

   Get-AlarmDefinition | where-Object {$_.Enabled -eq 'True'} | Select-Object name,

        @{N="Short Name";E={ $_.Name.Split('')[2]}} |

        Export-Csv $outfile -NoTypeInformation -UseCulture

The alarms are for example:

Type:3 Team:Ops Virtual machine reboot or shut down (IP:192.168.0.1)

but I'm getting just:

Virtual

How can I split from the second empty space until the end of the string to get it like:

Virtual machine reboot or shut down (IP:192.168.0.1)

Thanks.

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

Get-AlarmDefinition | where-Object {$_.Enabled -eq 'True'} |

Select-Object name,

        @{N="Short Name";E={

            $d1,$d2,$t = $_.Name.split(' ')

            $t -join ' '}} | 

Export-Csv $outfile -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

5 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

Get-AlarmDefinition | where-Object {$_.Enabled -eq 'True'} |

Select-Object name,

        @{N="Short Name";E={

            $d1,$d2,$t = $_.Name.split(' ')

            $t -join ' '}} | 

Export-Csv $outfile -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

a_p_
Leadership
Leadership
Jump to solution

One altenative is to split the string into 3 substrings:

$_.Name.Split('',3)[2]

André

LucD
Leadership
Leadership
Jump to solution

I suspect you need to split on a blank, not an empty string


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
billyjoel
Enthusiast
Enthusiast
Jump to solution

LucDa.p.

Thanks guys both of them worked perfectly, do you know of any good video or lecture that covers this "split" topic in depth?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ed Wilson did a good intro on the split method, see Using the Split Method in PowerShell

And if you want to see all the possibilities, have a look at the .NET Split method documentation.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference