Pass get content to hash table

Hi, i want to construct a hash table to be used in a invoke-webrequest post as json

part of that table are phonenumbers

I have a txt file with those phonenumbers but if it try to pass them as:

[array]$numbers = gc c:\numbers.txt 
foreach ($number in $numbers) { 

$hash = @{  type    = "critical"
            text = "TEST text alert"
            Date = (get-date -Format s)
            numbers= $number
            simulate= "false"

            }

$JSON = $hash | convertto-json 

$url='http://myapi/
Invoke-WebRequest -uri $url -Method POST -Body $JSON  -ContentType "application/json"
}

It seems that if ido that i add all the properies of the file and my JSON reads like:

$JSON
}
{
    "simulate":  "false",

    "text":  "TEST text alert",
    "numbers":  {
                    "value":  "447711111112",
                    "PSPath":  "C:\\numbers.txt",
                    "PSParentPath":  "C:\\",
                    "PSChildName":  "numbers.txt",
                    "PSDrive":  {
                                    "CurrentLocation":  "",
                                    "Name":  "C",
                                    "Provider":  "Microsoft.PowerShell.Core\\FileSystem",
                                    "Root":  "C:\\",
                                    "Description":  "",
                                    "Credential":  "System.Management.Automation.PSCredential",
                                    "DisplayRoot":  null
                                },
                    "PSProvider":  {
                                       "ImplementingType":  "Microsoft.PowerShell.Commands.FileSystemProvider",
                                       "HelpFile":  "System.Management.Automation.dll-Help.xml",
                                       "Name":  "FileSystem",
                                       "PSSnapIn":  "Microsoft.PowerShell.Core",
                                       "ModuleName":  "Microsoft.PowerShell.Core",
                                       "Module":  null,
                                       "Description":  "",
                                       "Capabilities":  52,
                                       "Home":  "C:\\Users\\user",
                                       "Drives":  "C E F"
                                   },
                    "ReadCount":  1
                },
    "type":  "critical",
    "Date":  "2015-07-27T14:52:37"
}
{
    "simulate":  "false",

    "text":  "TEST text alert",
    "numbers":  {
                    "value":  "447722333322",
                    "PSPath":  "C:\\numbers.txt",
                    "PSParentPath":  "C:\\",
                    "PSChildName":  "numbers.txt",
                    "PSDrive":  {
                                    "CurrentLocation":  "",
                                    "Name":  "C",
                                    "Provider":  "Microsoft.PowerShell.Core\\FileSystem",
                                    "Root":  "C:\\",
                                    "Description":  "",
                                    "Credential":  "System.Management.Automation.PSCredential",
                                    "DisplayRoot":  null
                                },
                    "PSProvider":  {
                                       "ImplementingType":  "Microsoft.PowerShell.Commands.FileSystemProvider",
                                       "HelpFile":  "System.Management.Automation.dll-Help.xml",
                                       "Name":  "FileSystem",
                                       "PSSnapIn":  "Microsoft.PowerShell.Core",
                                       "ModuleName":  "Microsoft.PowerShell.Core",
                                       "Module":  null,
                                       "Description":  "",
                                       "Capabilities":  52,
                                       "Home":  "C:\\Users\\users",
                                       "Drives":  "C E F"
                                   },
                    "ReadCount":  2
                },
    "type":  "critical",
    "Date":  "2015-07-27T14:52:37"
}

ideally i need to get just the value with the number but i can't get it to work.

Any ideas?

Thanks


July 27th, 2015 9:14am

Hi SS,

sorry, but I can't reproduce this with a list of phone numbers using your own code.
But using whatever way you do this actually, you probably can fix this by specifying the value property.

$hash = @{
    type     = "critical"
    text     = "TEST text alert"
    Date     = (get-date -Format s)
    numbers  = $number.Value
    simulate = "false"
}

Cheers,
Fred

Free Windows Admin Tool Kit Click here and download it now
July 27th, 2015 9:29am

Hi, that is the first thing i tried but then i get..

    "numbers":  null,
    "type":  "critical",
    "Date":  "2015-07-27T15:38:42"

July 27th, 2015 9:42am

When reading file do not add [array]

[array]$numbers = gc c:\numbers.txt

This is all:
$numbers = gc c:\numbers.txt

Free Windows Admin Tool Kit Click here and download it now
July 27th, 2015 10:00am

Hm,

just noticed, PowerShell attaches additional information to the import value (those PSSomething properties).

Try this instead:

$hash = @{
    type     = "critical"
    text     = "TEST text alert"
    Date     = (get-date -Format s)
    numbers  = $number.ToString()
    simulate = "false"
}

At least, that got rid of those properties for me.

Cheers,
Fred

July 27th, 2015 10:01am

Try this cleaned up version:

$url='http://myapi/'
$numbers = gc c:\numbers.txt 
foreach ($number in $numbers) { 
    $hash=@{
        type='critical'
        text='TEST text alert'
        Date=(get-date -Format s)
        numbers="$number"
        simulate=$false
    }

    $JSON=$hash | convertto-json 
    Invoke-WebRequest -uri $url -Method POST -Body $JSON  -ContentType 'application/json'
}
Free Windows Admin Tool Kit Click here and download it now
July 27th, 2015 10:03am

It is a bug. 
July 27th, 2015 10:12am

Hi your solution and the one above seem to work.

Thanks for your help.

Regards

Free Windows Admin Tool Kit Click here and download it now
July 27th, 2015 10:44am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics