Forum Replies Created
-
AuthorPosts
-
Thank you so much, that worked!
I can’t believe how close my code was all this time, i actually swapped @{ for @( and played with different variations of that etc but never tried them together, although i now see whats happening and how that works so thank you very much.
– Jamie
Released. Its mostly because of frequent edits I think. Thanks for letting us know.
I thought that might be the case, i kept messing up the code formatting but then saw the pinned post explaining the new code formatting option 🙂
Thank you for releasing it
Cheers for the tip 🙂
… but that uses $_.var which didn’t work, still getting used to powershell :).
… difference between foreach and ForEach-Object … 😉
Ah i kind of thought foreach was an alias of foreach-object (and to be fair i try not to use aliases in powershell but i keep seeing foreach) which i can now see they are not exactly the same, thank you for your help.
Having read up on it a little now, i came across another post here which helps too (https://powershell.org/forums/topic/foreach-vs-foreach-object/), thank you.
Cheers,
Jamie
Thank you so much, it is working now
I played with those variables based on a previous script i had written but that uses $_.var which didn’t work, still getting used to powershell :).
Thank you for your help
Cheers
Jamie
Hi Olaf,
Thank you for your reply, i was considering a CSV format as had used that in the past for something but wasn’t sure that was the right thing with not actually reading from or outputting to a CSV.
I have used your example script and it works but when i apply it to my script it doesn’t work as the vales are passed to the function as 0 and 1 rather than the variable value i.e. item1 and string_value1
PowerShell12345678910[crayon-6001a46eea669449606007 inline="true" ]$accounts = @'account,stringval,intvalitem1,string_value1,int_1item2,string_value2,int_2item3,string_value3,int_3'@ | ConvertFrom-Csv -Delimiter ','foreach ($line in $accounts){Get-MyBalanceFunction -account {0} -string {1} -f $line.account,$line.stringval}Sorry if i have missed something obvious.
-
This reply was modified 8 months, 3 weeks ago by
JDobbsy1987.
You may open a ticket about this in the feedback forum: https://powershell.org/forums/forum/community-discussion-site-feedback/
I have done so here – https://powershell.org/forums/topic/message-awaiting-review/
Thank you
Hi Olaf and Rob,
I appreciate your replies, they have helped a lot however my original reply that i tried to post 2 days ago still isn’t showing up but i can’t post it again as it says i have already said that so hopefully my reply with a detailed response will show or be approved soon.
Regards,
JamieHi,
Thank you for your replies, i really appreciate your support and the directions you give me.
The Group-Object cmdlet i think is the way to go with this, i have done a little bit of playing with it and hopefully will be able to make it fit however here is some extra details / csv that will help you understand what i’m trying to achieve in case there is a better way, i have also added what i currently have to the bottom of this post.
CSV File
PowerShell123456789$csv = @"date,time,store,service,user13/12/2019,10:50:18,Location1,Service1,user117/12/2019,16:35:03,Location1,Service2,user221/12/2019,15:26:17,Location2,Service1,user421/12/2019,15:26:32,Location2,Service1,user406/01/2020,15:26:32,Location1,Service2,user106/01/2020,15:26:32,Location2,Service1,User3"@ | ConvertFrom-CSVThis is how the statistics are currently output to email (based on a batch file rather than Powershell)
It doesn’t need to stay in this format but just needs to be clear.The numbers below are random to show you how they output and not a reflection of the test csv data above.
#######################
Location 1Total: 0 | 22
Service1: 0 | 6
Service2: 0 | 4
Service3: 0 | 0
Service4: 0 | 12Breakdown By User:
User1: 0 | 0
User2: 0 | 15
User3: 0 | 0
User4: 0 | 0#######################
Location 2Total: 0 | 22
Service1: 0 | 6
Service2: 0 | 4
Service3: 0 | 0
Service4: 0 | 12Breakdown By User:
User1: 0 | 0
User2: 0 | 15
User3: 0 | 0
User4: 0 | 0#######################
Forgetting the format of the output for now, i can pretty much get what i’m after with group-object with the following code
PowerShell123$filterDate = Get-Date -Format "dd/MM/yyyy"$service = $logTable | Where-Object date -eq $filterDate | Group-Object -Property location, service | Select-Object Name, Count$users = $logTable | Where-Object date -eq $filterDate | Group-Object -Property user | Select-Object Name, CountThis matches today’s date which is good for the first number 0|0 but to get the 2nd number 0|0 i need to search for any records matching $currentMonth
Can you advise if this is the best way to collect such data and how i would get currentMonth
Regards,
Jamie
The -replace statement uses regex for the search pattern and in regex the “|” represents a logical “or” – that’s why you fail at the moment. 😉 If you have a valid csv file why don’t you treat it as such and use Import-CSV instead of get content?
Hi Olaf,
Thank you for highlighting that -replace uses regex, i should have picked up on this but it explains the issue i was having.
With regards to why not using Import-Csv… I wasn’t actually aware of it, I’m new to Powershell, I have always used batch but trying to break that habit (finally).After tinkering with Import-Csv I have now managed to get the values to update as intended so thank you very much for that pointer.
Being able to reference specific rows / columns using an array is awesome, having used batch files for so long i have been super limited and feel like an idiot for not jumping ship years ago :).
Here’s a bit of a different approach. Powershell is strongest when you are leveraging object. While you can do text manipulation, the other file is in a CSV format, so it will be more predictable leveraging it as an object. This approach would take an update file and consider it autho:
Hi Rob,
Thank you for your input again, I hadn’t actually seen your reply until now sorry but will certainly play with this approach too.
Thank you both.
Thank you Rob, dot notation worked perfect, I can’t believe i didn’t try that as i was using dot notation before but removed it when troubleshooting an earlier error that i thought was attributed to that.
Thank you for your support.
Hi,
Thank you for your reply, sorry i meant to add the values to the first post, they are currency values but without £ such as 160.49 and 160.28
The csv file is made up of 3 columns separated by a pipe:
Part 1|”Part 1 Description”|160.49
Part 2|”Part 2 Description”|160.28I am trying to check if $c (the price) matches nPrice (The new price from the XML API)
PowerShell123456789101112131415161718192021# Loop through part numbers and pricesGet-Content “UNCPath\fileName.csv” | ForEach {$a,$b,$c = $_.Split(“|”)# Create XML Request$convID = New-Guid[xml]$xreq = get-content “UNCPath\Template.xml”$xreq.message.header.conversationID = $convID.ToString()$xreq.message.body.poAvailabilityRequest.partNo = $a# Send XML request and get response$response = Invoke-RestMethod -Headers @{Authorization=(“Basic {0}” -f $base64AuthInfo)} ‘URL.com/API’ -Method Post -Body $xreq -ContentType ‘text/xml’$nPrice = $response.SelectSingleNode(“//salesPrice”)IF ($c -eq $nPrice){‘Yes’} Else {‘No’}} -
This reply was modified 8 months, 3 weeks ago by
-
AuthorPosts