JDobbsy1987

Forum Replies Created

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • in reply to: Create HashTable With Multiple Values #222138
    Participant
    Topics: 6
    Replies: 10
    Points: 103
    Rank: Participant

    Cheers for the tip 🙂

    in reply to: Create HashTable With Multiple Values #222126
    Participant
    Topics: 6
    Replies: 10
    Points: 103
    Rank: Participant

    … 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

    in reply to: Create HashTable With Multiple Values #222117
    Participant
    Topics: 6
    Replies: 10
    Points: 103
    Rank: Participant

    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

    in reply to: Create HashTable With Multiple Values #222102
    Participant
    Topics: 6
    Replies: 10
    Points: 103
    Rank: Participant

    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

    $accounts = @'
    account,stringval,intval
    item1,string_value1,int_1
    item2,string_value2,int_2
    item3,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 3 months, 2 weeks ago by JDobbsy1987.
    in reply to: Advice around statistics from csv #196976
    Participant
    Topics: 6
    Replies: 10
    Points: 103
    Rank: Participant
    in reply to: Advice around statistics from csv #196955
    Participant
    Topics: 6
    Replies: 10
    Points: 103
    Rank: Participant

    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,
    Jamie

    in reply to: Advice around statistics from csv #196676
    Participant
    Topics: 6
    Replies: 10
    Points: 103
    Rank: Participant

    Hi,

    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

    $csv = @"
    date,time,store,service,user
    13/12/2019,10:50:18,Location1,Service1,user1
    17/12/2019,16:35:03,Location1,Service2,user2
    21/12/2019,15:26:17,Location2,Service1,user4
    21/12/2019,15:26:32,Location2,Service1,user4
    06/01/2020,15:26:32,Location1,Service2,user1
    06/01/2020,15:26:32,Location2,Service1,User3
    "@ | ConvertFrom-CSV

     

    This 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 1

    Total: 0 | 22
    Service1: 0 | 6
    Service2: 0 | 4
    Service3: 0 | 0
    Service4: 0 | 12

    Breakdown By User:
    User1: 0 | 0
    User2: 0 | 15
    User3: 0 | 0
    User4: 0 | 0

    #######################
    Location 2

    Total: 0 | 22
    Service1: 0 | 6
    Service2: 0 | 4
    Service3: 0 | 0
    Service4: 0 | 12

    Breakdown 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

    $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, Count

    This 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

    in reply to: Looping through variable and replace text #194675
    Participant
    Topics: 6
    Replies: 10
    Points: 103
    Rank: Participant

    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.

    in reply to: IF XML Node = Variable #194447
    Participant
    Topics: 6
    Replies: 10
    Points: 103
    Rank: Participant

    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.

    in reply to: IF XML Node = Variable #194435
    Participant
    Topics: 6
    Replies: 10
    Points: 103
    Rank: Participant

    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.28

    I am trying to check if $c (the price) matches nPrice (The new price from the XML API)

    # Loop through part numbers and prices
    Get-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’
    }
    }
Viewing 10 posts - 1 through 10 (of 10 total)