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)<\/strong> and it appeared work, but it also ended up returning some duplicate values.<\/p>\n
I then tried name.tostring[0…13]<\/strong> 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? \nBelow is a snippet of the code I’m working with. “Name” is the field that I need shortened to 12 chars. Thanks in advance!!!<\/p>\nGet-ClusterGroup -Cluster $TargetComp | ft \n$Cluster = Get-ClusterGroup -Cluster $TargetComp\n$a= $null\n$a = \"Name\"+\" \"+\" OwnerNode\"+\" \"+\"Status\"+\"`n\"\n\nFor ($i=0; $i -lt $Cluster.Count ; $i++) {\n$b = $Cluster[$i].name.tostring()[0..13]+\" \"+$Cluster[$i].ownernode.tostring()+\" \"+$Cluster[$i].state.tostring() \n$a = $a + $b +\"`n\"\n<\/code><\/pre>","upvoteCount":3,"answerCount":13,"datePublished":"2017-09-01T16:25:57.000Z","author":{"@type":"Person","name":"thatguy2","url":"https://community.spiceworks.com/u/thatguy2"},"acceptedAnswer":{"@type":"Answer","text":"
Omg… I finally understand what you’re trying to do…<\/p>\n
You’re trying to build a custom table!<\/p>\n
$Cluster = Get-ClusterGroup -Cluster $clusternodename\n$obj = For ($i=0; $i -lt $Cluster.Count ; $i++) {\n $splat = @{\n Name = ($Cluster[$i].name.tostring()[0..13] -join \"\")\n OwnerNode = $Cluster[$i].ownernode.tostring()\n Status = $Cluster[$i].state.tostring()\n }\n New-Object psobject -Property $splat\n}\n\n$obj\n<\/code><\/pre>\nlooks like so:<\/p>\n
<\/p>","upvoteCount":1,"datePublished":"2017-09-01T20:03:47.000Z","url":"https://community.spiceworks.com/t/issues-trimming-string/603536/12","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},"suggestedAnswer":[{"@type":"Answer","text":"
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)<\/strong> and it appeared work, but it also ended up returning some duplicate values.<\/p>\nI then tried name.tostring[0…13]<\/strong> 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? \nBelow is a snippet of the code I’m working with. “Name” is the field that I need shortened to 12 chars. Thanks in advance!!!<\/p>\nGet-ClusterGroup -Cluster $TargetComp | ft \n$Cluster = Get-ClusterGroup -Cluster $TargetComp\n$a= $null\n$a = \"Name\"+\" \"+\" OwnerNode\"+\" \"+\"Status\"+\"`n\"\n\nFor ($i=0; $i -lt $Cluster.Count ; $i++) {\n$b = $Cluster[$i].name.tostring()[0..13]+\" \"+$Cluster[$i].ownernode.tostring()+\" \"+$Cluster[$i].state.tostring() \n$a = $a + $b +\"`n\"\n<\/code><\/pre>","upvoteCount":3,"datePublished":"2017-09-01T16:25:58.000Z","url":"https://community.spiceworks.com/t/issues-trimming-string/603536/1","author":{"@type":"Person","name":"thatguy2","url":"https://community.spiceworks.com/u/thatguy2"}},{"@type":"Answer","text":"\n\n
<\/div>\n
thatguy2:<\/div>\n
\nbut it also ended up returning some duplicate values<\/p>\n<\/blockquote>\n<\/aside>\n
Can you elaborate?<\/p>\n
Do you have a sample string ?<\/p>\n
$string = 'Server1isthebestserver'\n$string.Substring(0,12)\n$string[0..11] -join \"\"\n<\/code><\/pre>\nOutput:<\/p>\n
Server1isthe\nServer1isthe\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2017-09-01T17:07:44.000Z","url":"https://community.spiceworks.com/t/issues-trimming-string/603536/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"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.<\/p>\n
Node Name : xxxxxxx\n Uptime : 46 Days,2 Hours,48 Minutes,44 Seconds\n LastBootTime : 7/17/2017 8:33:15 AM\n _ : Name OwnerNode Status\n resource1 node1 Online\n resource2 node2 Online\n resource2 node2 Online\n resource3 node3 Online\n<\/code><\/pre>\n Node Name : xxxxxxx\n Uptime : 46 Days,2 Hours,48 Minutes,44 Seconds\n LastBootTime : 7/17/2017 8:33:15 AM\n _ : Name OwnerNode Status\n r e s o u r c e 1 node1 Online\n r e s o u r c e 2 node2 Online\n r e s o u r c e 3 node3 Online\n r e s o u r c e 4 node4 Online\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2017-09-01T17:16:05.000Z","url":"https://community.spiceworks.com/t/issues-trimming-string/603536/3","author":{"@type":"Person","name":"thatguy2","url":"https://community.spiceworks.com/u/thatguy2"}},{"@type":"Answer","text":"Ya, because it [0…13] makes basically each one an array element.<\/p>\n
Have you tried it with ‘join’ ?<\/p>\n
<\/p>","upvoteCount":1,"datePublished":"2017-09-01T17:22:56.000Z","url":"https://community.spiceworks.com/t/issues-trimming-string/603536/4","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
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 <\/p>","upvoteCount":0,"datePublished":"2017-09-01T17:41:02.000Z","url":"https://community.spiceworks.com/t/issues-trimming-string/603536/5","author":{"@type":"Person","name":"thatguy2","url":"https://community.spiceworks.com/u/thatguy2"}},{"@type":"Answer","text":"\n\n
<\/div>\n
thatguy2:<\/div>\n
\nYep, 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 <\/p>\n<\/blockquote>\n<\/aside>\n
Well, how / what have you tried?<\/p>","upvoteCount":0,"datePublished":"2017-09-01T17:46:36.000Z","url":"https://community.spiceworks.com/t/issues-trimming-string/603536/6","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
Something similar to the following:<\/p>\n
$b = $Cluster[$i].name.tostring()[0…13] -join “”<\/p>\n
$b = $Cluster[$i].name.tostring()[0..13] -join \"\"\n<\/code><\/pre>\nAgain, I’m not too familiar with that particular command.<\/p>","upvoteCount":0,"datePublished":"2017-09-01T19:02:55.000Z","url":"https://community.spiceworks.com/t/issues-trimming-string/603536/7","author":{"@type":"Person","name":"thatguy2","url":"https://community.spiceworks.com/u/thatguy2"}},{"@type":"Answer","text":"\n\n
<\/div>\n
thatguy2:<\/div>\n
\nSomething similar to the following:<\/p>\n
$b = $Cluster[$i].name.tostring()[0…13] -join “”<\/p>\n
$b = $Cluster[$i].name.tostring()[0..13] -join \"\"\n<\/code><\/pre>\nAgain, I’m not too familiar with that particular command.<\/p>\n<\/blockquote>\n<\/aside>\n
Since I don’t have values for all that stuff it’s tough to test. \nDid you get an error? or what happened when you tried that?<\/p>\n
maybe try like so:<\/p>\n
$b = ($Cluster[$i].name.tostring()[0..13]) -join \"\"\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2017-09-01T19:10:59.000Z","url":"https://community.spiceworks.com/t/issues-trimming-string/603536/8","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"Or actually maybe the whole thing in brackets<\/p>\n
$b = (($Cluster[$i].name.tostring()[0..13]) -join \"\") + \"otherstuff\"\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2017-09-01T19:36:41.000Z","url":"https://community.spiceworks.com/t/issues-trimming-string/603536/9","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"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.<\/p>\n
import-module failoverclusters\n\nClear-Host\n$Data = @()\n$TargetComps = get-content \"servers.txt\"\nforeach ($TargetComp in $TargetComps){ \nif (Test-Connection -ComputerName $TargetComp -BufferSize 16 -Count 5 -Quiet)\n{\n\n$wm = gwmi -computer $TargetComp Win32_OperatingSystem\n$lastbootime = $wm.ConvertToDateTime($wm.LastBootUpTime)\n\n\"============================================================================\"\n$sysuptime = (Get-Date) - $lastbootime\n$uptime = \"UPTIME is $($sysuptime.days) Days, $($sysuptime.hours) Hours, $($sysuptime.minutes) Minutes, $($sysuptime.seconds) Seconds\"\n\"Last boot time is $lastbootime\" |Write-Host\n\"$uptime for $TargetComp\" | Write-Host \n\"============================================================================\"\n$UPV = \"$($sysuptime.days) Days,$($sysuptime.hours) Hours,$($sysuptime.minutes) Minutes,$($sysuptime.seconds) Seconds\"\n$Object = New-Object -TypeName PSObject\n$Object | Add-Member -MemberType NoteProperty -Name \"Node Name\" -Value $TargetComp\n$Object | Add-Member -MemberType NoteProperty -Name \"Uptime\" -Value $UPV\n$Object | Add-Member -MemberType NoteProperty -Name \"LastBootTime\" -Value $lastbootime\n\n\"========Status of GROUPS are================================================\"\nGet-ClusterGroup -Cluster $TargetComp | ft \n$Cluster = Get-ClusterGroup -Cluster $TargetComp\n$a= $null\n$a = \"Name\"+\" \"+\" OwnerNode\"+\" \"+\"Status\"+\"`n\"\n\nFor ($i=0; $i -lt $Cluster.Count ; $i++) {\n$b = $Cluster[$i].name.tostring()[0..13]+\" \"+$Cluster[$i].ownernode.tostring()+\" \"+$Cluster[$i].state.tostring() \n$a = $a + $b +\"`n\"\n}\n$Object | Add-Member -MemberType NoteProperty -Name \"_\" -Value $a\n\n$Data += $Object\n} \n} \n$body = $Data | fl | out-string \n\n$Mailfrom=\"mail.com\"\n$SMTPServer=\"mail.com\"\n$Mailto=\"mail.com\"\nSend-MailMessage -From $Mailfrom -to $Mailto -Subject \"Cluster Report\" -SmtpServer $SMTPServer -Body $body\nExit\n\n<\/code><\/pre>\n Node Name : xxxxxxx\n Uptime : 46 Days,2 Hours,48 Minutes,44 Seconds\n LastBootTime : 7/17/2017 8:33:15 AM\n _ : Name OwnerNode Status\n resource1 node1 Online\n resource123456 node2 Online\n resource123456789 node3 Online\n resource4 node4 Online\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2017-09-01T19:42:48.000Z","url":"https://community.spiceworks.com/t/issues-trimming-string/603536/10","author":{"@type":"Person","name":"thatguy2","url":"https://community.spiceworks.com/u/thatguy2"}},{"@type":"Answer","text":"Maybe try again, after removing that ‘format-list’<\/p>\n
$body = $Data | fl | out-string \n<\/code><\/pre>","upvoteCount":0,"datePublished":"2017-09-01T19:55:09.000Z","url":"https://community.spiceworks.com/t/issues-trimming-string/603536/11","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"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<\/p>","upvoteCount":0,"datePublished":"2017-09-01T20:45:43.000Z","url":"https://community.spiceworks.com/t/issues-trimming-string/603536/13","author":{"@type":"Person","name":"thatguy2","url":"https://community.spiceworks.com/u/thatguy2"}}]}}
thatguy2
(thatguy2)
September 1, 2017, 4:25pm
1
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
Neally
(Neally)
September 1, 2017, 5:07pm
2
Can you elaborate?
Do you have a sample string ?
$string = 'Server1isthebestserver'
$string.Substring(0,12)
$string[0..11] -join ""
Output:
Server1isthe
Server1isthe
thatguy2
(thatguy2)
September 1, 2017, 5:16pm
3
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
Neally
(Neally)
September 1, 2017, 5:22pm
4
Ya, because it [0…13] makes basically each one an array element.
Have you tried it with ‘join’ ?
1 Spice up
thatguy2
(thatguy2)
September 1, 2017, 5:41pm
5
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
Neally
(Neally)
September 1, 2017, 5:46pm
6
thatguy2:
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
Well, how / what have you tried?
thatguy2
(thatguy2)
September 1, 2017, 7:02pm
7
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.
Neally
(Neally)
September 1, 2017, 7:10pm
8
thatguy2:
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 ""
Neally
(Neally)
September 1, 2017, 7:36pm
9
Or actually maybe the whole thing in brackets
$b = (($Cluster[$i].name.tostring()[0..13]) -join "") + "otherstuff"
thatguy2
(thatguy2)
September 1, 2017, 7:42pm
10
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
Neally
(Neally)
September 1, 2017, 7:55pm
11
Maybe try again, after removing that ‘format-list’
$body = $Data | fl | out-string
Neally
(Neally)
September 1, 2017, 8:03pm
12
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:
1 Spice up
thatguy2
(thatguy2)
September 1, 2017, 8:45pm
13
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