Hello - So I’m working on a script where I’d like to limit the length of content stored inside of a variable to 12 chars. I already tried to do a name.substring(0,13) and it appeared work, but it also ended up returning some duplicate values.

I then tried name.tostring[0…13] and it worked perfectly, except for the fact that it spaced out the resultant values like the following: S E R V E R 1. Outside of that though, it worked perfectly, so does anyone know how to rectify that particular condition?
Below is a snippet of the code I’m working with. “Name” is the field that I need shortened to 12 chars. Thanks in advance!!!

Get-ClusterGroup -Cluster $TargetComp | ft 
$Cluster = Get-ClusterGroup -Cluster $TargetComp
$a= $null
$a = "Name"+"             "+"      OwnerNode"+"             "+"Status"+"`n"

For ($i=0; $i -lt $Cluster.Count ; $i++) {
$b = $Cluster[$i].name.tostring()[0..13]+"          "+$Cluster[$i].ownernode.tostring()+"          "+$Cluster[$i].state.tostring() 
$a = $a + $b +"`n"
3 Spice ups

Can you elaborate?

Do you have a sample string ?

$string = 'Server1isthebestserver'
$string.Substring(0,12)
$string[0..11] -join ""

Output:

Server1isthe
Server1isthe

The output is being sent to an email body and is similar to below. The first is example is what it looks like when I use substring (notice the duplicates under name) and the second example is through using the [0…13] method, which is very close to what I need except for the strange font.

 Node Name    : xxxxxxx
 Uptime       : 46 Days,2 Hours,48 Minutes,44 Seconds
 LastBootTime : 7/17/2017 8:33:15 AM
 _            : Name                   OwnerNode             Status
                resource1                  node1          Online
                resource2                  node2          Online
                resource2                  node2          Online
                resource3                  node3          Online
 Node Name    : xxxxxxx
 Uptime       : 46 Days,2 Hours,48 Minutes,44 Seconds
 LastBootTime : 7/17/2017 8:33:15 AM
 _            : Name                   OwnerNode             Status
                r e s o u r c e 1                  node1          Online
                r e s o u r c e 2                  node2          Online
                r e s o u r c e 3                  node3          Online
                r e s o u r c e 4                  node4          Online

Ya, because it [0…13] makes basically each one an array element.

Have you tried it with ‘join’ ?

arr.png

1 Spice up

Yep, that’s what I figured. I did try to use it conjunction with join, but the output was even worse. That being said, there is a fair chance that I was doing it wrong :slight_smile:

Well, how / what have you tried?

Something similar to the following:

$b = $Cluster[$i].name.tostring()[0…13] -join “”

$b = $Cluster[$i].name.tostring()[0..13] -join ""

Again, I’m not too familiar with that particular command.

Since I don’t have values for all that stuff it’s tough to test.
Did you get an error? or what happened when you tried that?

maybe try like so:

$b = ($Cluster[$i].name.tostring()[0..13]) -join ""

Or actually maybe the whole thing in brackets

$b = (($Cluster[$i].name.tostring()[0..13]) -join "") +  "otherstuff"

No luck there wither unfortunately. Below is the script in totality, it sends the exact info I need to email, but I just need to trim the “name” field to make the output more readable / uniform, which has spun me down this rabbit hole. An example of that output is below the script.

import-module failoverclusters

Clear-Host
$Data = @()
$TargetComps = get-content "servers.txt"
foreach ($TargetComp in $TargetComps){ 
if (Test-Connection -ComputerName $TargetComp  -BufferSize 16 -Count 5 -Quiet)
{

$wm = gwmi -computer $TargetComp Win32_OperatingSystem
$lastbootime = $wm.ConvertToDateTime($wm.LastBootUpTime)

"============================================================================"
$sysuptime = (Get-Date) - $lastbootime
$uptime = "UPTIME is $($sysuptime.days) Days, $($sysuptime.hours) Hours, $($sysuptime.minutes) Minutes, $($sysuptime.seconds) Seconds"
"Last boot time is $lastbootime" |Write-Host
"$uptime for $TargetComp" | Write-Host 
"============================================================================"
$UPV = "$($sysuptime.days) Days,$($sysuptime.hours) Hours,$($sysuptime.minutes) Minutes,$($sysuptime.seconds) Seconds"
$Object = New-Object -TypeName PSObject
$Object | Add-Member -MemberType NoteProperty -Name "Node Name" -Value $TargetComp
$Object | Add-Member -MemberType NoteProperty -Name "Uptime" -Value $UPV
$Object | Add-Member -MemberType NoteProperty -Name "LastBootTime" -Value $lastbootime

"========Status of GROUPS are================================================"
Get-ClusterGroup -Cluster $TargetComp | ft 
$Cluster = Get-ClusterGroup -Cluster $TargetComp
$a= $null
$a = "Name"+"             "+"      OwnerNode"+"             "+"Status"+"`n"

For ($i=0; $i -lt $Cluster.Count ; $i++) {
$b = $Cluster[$i].name.tostring()[0..13]+"          "+$Cluster[$i].ownernode.tostring()+"          "+$Cluster[$i].state.tostring() 
$a = $a + $b +"`n"
}
$Object | Add-Member -MemberType NoteProperty -Name "_" -Value $a

$Data += $Object
}                
} 
$body = $Data | fl | out-string 

$Mailfrom="mail.com"
$SMTPServer="mail.com"
$Mailto="mail.com"
Send-MailMessage -From $Mailfrom -to $Mailto -Subject "Cluster Report" -SmtpServer $SMTPServer -Body $body
Exit

 Node Name    : xxxxxxx
 Uptime       : 46 Days,2 Hours,48 Minutes,44 Seconds
 LastBootTime : 7/17/2017 8:33:15 AM
 _            : Name                   OwnerNode             Status
                resource1                  node1          Online
                resource123456                  node2          Online
                resource123456789                  node3          Online
                resource4                  node4          Online

Maybe try again, after removing that ‘format-list’

$body = $Data | fl | out-string 

Omg… I finally understand what you’re trying to do…

You’re trying to build a custom table!

$Cluster = Get-ClusterGroup -Cluster $clusternodename
$obj = For ($i=0; $i -lt $Cluster.Count ; $i++) {
    $splat = @{
        Name      = ($Cluster[$i].name.tostring()[0..13] -join "")
        OwnerNode = $Cluster[$i].ownernode.tostring()
        Status    = $Cluster[$i].state.tostring()
    }
    New-Object psobject -Property $splat
}

$obj

looks like so:

cluster.png

1 Spice up

Yep, exactly - sorry for not explaining in more detail initially but there is a lot to this one. What it is also doing as part of the custom table is appending the last reboot time / uptime, so it is a tough nut to crack. It’s frustrating because I’m 99% of the way there and I’m spinning my wheels on alignment issues…ugh