Powershell balance array elements
(PS 101 newbie here)
I want to make a powershell script to import-csv a simple "folder,computername" file (generated by an app) where the folder will have 8 possible values (catchall,sun,mon,tue...sat). 1 value is a catch all and the other 7 are days of the week. I want to make a list of computer names from the catch all group and export them to 7 files for the days of the week so that the resulting files make the 7 days balanced (so count each day of the week to know which ones need more items). My app will import the 7 files and then later generate a new systems.csv file.
$computers = import-csv “Systems.csv”
$newsched = $computers | Group "folder"
This gets me the counts but I don't get how to take the results and use them for the loops.
I would then take the count of catchall and /7 it, then count how many each day of the week has a and loop where I export-csv the catch all elements to the appropriate day of the week file to keep them balanced as much as possible.
powershell
add a comment |
(PS 101 newbie here)
I want to make a powershell script to import-csv a simple "folder,computername" file (generated by an app) where the folder will have 8 possible values (catchall,sun,mon,tue...sat). 1 value is a catch all and the other 7 are days of the week. I want to make a list of computer names from the catch all group and export them to 7 files for the days of the week so that the resulting files make the 7 days balanced (so count each day of the week to know which ones need more items). My app will import the 7 files and then later generate a new systems.csv file.
$computers = import-csv “Systems.csv”
$newsched = $computers | Group "folder"
This gets me the counts but I don't get how to take the results and use them for the loops.
I would then take the count of catchall and /7 it, then count how many each day of the week has a and loop where I export-csv the catch all elements to the appropriate day of the week file to keep them balanced as much as possible.
powershell
1
google.com/search?q=powershell+loops
– Mat
Nov 13 '18 at 16:00
Even after reading several times, I don't get what you want to balance how. You really should show more details and expected outcome in your edited question.
– LotPings
Nov 13 '18 at 17:20
add a comment |
(PS 101 newbie here)
I want to make a powershell script to import-csv a simple "folder,computername" file (generated by an app) where the folder will have 8 possible values (catchall,sun,mon,tue...sat). 1 value is a catch all and the other 7 are days of the week. I want to make a list of computer names from the catch all group and export them to 7 files for the days of the week so that the resulting files make the 7 days balanced (so count each day of the week to know which ones need more items). My app will import the 7 files and then later generate a new systems.csv file.
$computers = import-csv “Systems.csv”
$newsched = $computers | Group "folder"
This gets me the counts but I don't get how to take the results and use them for the loops.
I would then take the count of catchall and /7 it, then count how many each day of the week has a and loop where I export-csv the catch all elements to the appropriate day of the week file to keep them balanced as much as possible.
powershell
(PS 101 newbie here)
I want to make a powershell script to import-csv a simple "folder,computername" file (generated by an app) where the folder will have 8 possible values (catchall,sun,mon,tue...sat). 1 value is a catch all and the other 7 are days of the week. I want to make a list of computer names from the catch all group and export them to 7 files for the days of the week so that the resulting files make the 7 days balanced (so count each day of the week to know which ones need more items). My app will import the 7 files and then later generate a new systems.csv file.
$computers = import-csv “Systems.csv”
$newsched = $computers | Group "folder"
This gets me the counts but I don't get how to take the results and use them for the loops.
I would then take the count of catchall and /7 it, then count how many each day of the week has a and loop where I export-csv the catch all elements to the appropriate day of the week file to keep them balanced as much as possible.
powershell
powershell
edited Nov 13 '18 at 16:00
SW41
asked Nov 13 '18 at 15:54
SW41SW41
11
11
1
google.com/search?q=powershell+loops
– Mat
Nov 13 '18 at 16:00
Even after reading several times, I don't get what you want to balance how. You really should show more details and expected outcome in your edited question.
– LotPings
Nov 13 '18 at 17:20
add a comment |
1
google.com/search?q=powershell+loops
– Mat
Nov 13 '18 at 16:00
Even after reading several times, I don't get what you want to balance how. You really should show more details and expected outcome in your edited question.
– LotPings
Nov 13 '18 at 17:20
1
1
google.com/search?q=powershell+loops
– Mat
Nov 13 '18 at 16:00
google.com/search?q=powershell+loops
– Mat
Nov 13 '18 at 16:00
Even after reading several times, I don't get what you want to balance how. You really should show more details and expected outcome in your edited question.
– LotPings
Nov 13 '18 at 17:20
Even after reading several times, I don't get what you want to balance how. You really should show more details and expected outcome in your edited question.
– LotPings
Nov 13 '18 at 17:20
add a comment |
1 Answer
1
active
oldest
votes
this is an OLD bit of code that takes an array of items and generates a set of equal-as-possible batches. if i was to rewrite it, instead of an array of strings, i would likely use a hashtable containing Key = $BatchNumber
& Value = $ArrayOfItems
. [grin]
however, it does demo one way to split a list into several smaller lists that are nearly the same size.
#$GroupList = "test1", "test2", "test3"
#$GroupList = "test1", "test2", "test3", "test4", "test5", "test6"
#$GroupList = "test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8"
$GroupList = "test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8", "test9", "test10", "test11"
$MaxPerBatch = 4
$Delimiter = ", " # one comma plus one space
$Batches = @()
Write-Output "----------"
$NextBatch = ""
$Remaining = $GroupList.Count
$Count = 0
foreach ($Group in $GroupList) {
$Count++
if (($Count -lt $MaxPerBatch) -and ($Remaining -gt 0)) {
$NextBatch += $Group + $Delimiter
}
else {
$NextBatch += $Group
$Batches += $NextBatch
$NextBatch = ""
$Count = 0
}
$Remaining--
if ($Remaining -eq 0) {
$NextBatch = $NextBatch.TrimEnd($Delimiter)
$Batches += $NextBatch
}
}
Write-Output "batch[es] of groups = $($Batches.Count)"
Write-Output ""
Write-Output "collection displayed below ..."
$Batches
Write-Output "++++++++++"
output ...
----------
batch[es] of groups = 3
collection displayed below ...
test1, test2, test3, test4
test5, test6, test7, test8
test9, test10, test11
++++++++++
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284763%2fpowershell-balance-array-elements%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
this is an OLD bit of code that takes an array of items and generates a set of equal-as-possible batches. if i was to rewrite it, instead of an array of strings, i would likely use a hashtable containing Key = $BatchNumber
& Value = $ArrayOfItems
. [grin]
however, it does demo one way to split a list into several smaller lists that are nearly the same size.
#$GroupList = "test1", "test2", "test3"
#$GroupList = "test1", "test2", "test3", "test4", "test5", "test6"
#$GroupList = "test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8"
$GroupList = "test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8", "test9", "test10", "test11"
$MaxPerBatch = 4
$Delimiter = ", " # one comma plus one space
$Batches = @()
Write-Output "----------"
$NextBatch = ""
$Remaining = $GroupList.Count
$Count = 0
foreach ($Group in $GroupList) {
$Count++
if (($Count -lt $MaxPerBatch) -and ($Remaining -gt 0)) {
$NextBatch += $Group + $Delimiter
}
else {
$NextBatch += $Group
$Batches += $NextBatch
$NextBatch = ""
$Count = 0
}
$Remaining--
if ($Remaining -eq 0) {
$NextBatch = $NextBatch.TrimEnd($Delimiter)
$Batches += $NextBatch
}
}
Write-Output "batch[es] of groups = $($Batches.Count)"
Write-Output ""
Write-Output "collection displayed below ..."
$Batches
Write-Output "++++++++++"
output ...
----------
batch[es] of groups = 3
collection displayed below ...
test1, test2, test3, test4
test5, test6, test7, test8
test9, test10, test11
++++++++++
add a comment |
this is an OLD bit of code that takes an array of items and generates a set of equal-as-possible batches. if i was to rewrite it, instead of an array of strings, i would likely use a hashtable containing Key = $BatchNumber
& Value = $ArrayOfItems
. [grin]
however, it does demo one way to split a list into several smaller lists that are nearly the same size.
#$GroupList = "test1", "test2", "test3"
#$GroupList = "test1", "test2", "test3", "test4", "test5", "test6"
#$GroupList = "test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8"
$GroupList = "test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8", "test9", "test10", "test11"
$MaxPerBatch = 4
$Delimiter = ", " # one comma plus one space
$Batches = @()
Write-Output "----------"
$NextBatch = ""
$Remaining = $GroupList.Count
$Count = 0
foreach ($Group in $GroupList) {
$Count++
if (($Count -lt $MaxPerBatch) -and ($Remaining -gt 0)) {
$NextBatch += $Group + $Delimiter
}
else {
$NextBatch += $Group
$Batches += $NextBatch
$NextBatch = ""
$Count = 0
}
$Remaining--
if ($Remaining -eq 0) {
$NextBatch = $NextBatch.TrimEnd($Delimiter)
$Batches += $NextBatch
}
}
Write-Output "batch[es] of groups = $($Batches.Count)"
Write-Output ""
Write-Output "collection displayed below ..."
$Batches
Write-Output "++++++++++"
output ...
----------
batch[es] of groups = 3
collection displayed below ...
test1, test2, test3, test4
test5, test6, test7, test8
test9, test10, test11
++++++++++
add a comment |
this is an OLD bit of code that takes an array of items and generates a set of equal-as-possible batches. if i was to rewrite it, instead of an array of strings, i would likely use a hashtable containing Key = $BatchNumber
& Value = $ArrayOfItems
. [grin]
however, it does demo one way to split a list into several smaller lists that are nearly the same size.
#$GroupList = "test1", "test2", "test3"
#$GroupList = "test1", "test2", "test3", "test4", "test5", "test6"
#$GroupList = "test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8"
$GroupList = "test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8", "test9", "test10", "test11"
$MaxPerBatch = 4
$Delimiter = ", " # one comma plus one space
$Batches = @()
Write-Output "----------"
$NextBatch = ""
$Remaining = $GroupList.Count
$Count = 0
foreach ($Group in $GroupList) {
$Count++
if (($Count -lt $MaxPerBatch) -and ($Remaining -gt 0)) {
$NextBatch += $Group + $Delimiter
}
else {
$NextBatch += $Group
$Batches += $NextBatch
$NextBatch = ""
$Count = 0
}
$Remaining--
if ($Remaining -eq 0) {
$NextBatch = $NextBatch.TrimEnd($Delimiter)
$Batches += $NextBatch
}
}
Write-Output "batch[es] of groups = $($Batches.Count)"
Write-Output ""
Write-Output "collection displayed below ..."
$Batches
Write-Output "++++++++++"
output ...
----------
batch[es] of groups = 3
collection displayed below ...
test1, test2, test3, test4
test5, test6, test7, test8
test9, test10, test11
++++++++++
this is an OLD bit of code that takes an array of items and generates a set of equal-as-possible batches. if i was to rewrite it, instead of an array of strings, i would likely use a hashtable containing Key = $BatchNumber
& Value = $ArrayOfItems
. [grin]
however, it does demo one way to split a list into several smaller lists that are nearly the same size.
#$GroupList = "test1", "test2", "test3"
#$GroupList = "test1", "test2", "test3", "test4", "test5", "test6"
#$GroupList = "test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8"
$GroupList = "test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8", "test9", "test10", "test11"
$MaxPerBatch = 4
$Delimiter = ", " # one comma plus one space
$Batches = @()
Write-Output "----------"
$NextBatch = ""
$Remaining = $GroupList.Count
$Count = 0
foreach ($Group in $GroupList) {
$Count++
if (($Count -lt $MaxPerBatch) -and ($Remaining -gt 0)) {
$NextBatch += $Group + $Delimiter
}
else {
$NextBatch += $Group
$Batches += $NextBatch
$NextBatch = ""
$Count = 0
}
$Remaining--
if ($Remaining -eq 0) {
$NextBatch = $NextBatch.TrimEnd($Delimiter)
$Batches += $NextBatch
}
}
Write-Output "batch[es] of groups = $($Batches.Count)"
Write-Output ""
Write-Output "collection displayed below ..."
$Batches
Write-Output "++++++++++"
output ...
----------
batch[es] of groups = 3
collection displayed below ...
test1, test2, test3, test4
test5, test6, test7, test8
test9, test10, test11
++++++++++
answered Nov 13 '18 at 16:53
Lee_DaileyLee_Dailey
1,745178
1,745178
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284763%2fpowershell-balance-array-elements%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
google.com/search?q=powershell+loops
– Mat
Nov 13 '18 at 16:00
Even after reading several times, I don't get what you want to balance how. You really should show more details and expected outcome in your edited question.
– LotPings
Nov 13 '18 at 17:20