dongliming2416
dongliming2416
2014-11-04 19:19
浏览 93
已采纳

将powershell结果发送到HTTP

Sorry for being a newbie in this, but I want to learn something about sending powershell results to a PHP page for later processing.

I tried this code:

$test1 = "test"
$postParams = @{test=$test1}
Invoke-WebRequest -Uri http://bu01/stats/input.php -Method POST -Body $postParams

Which works fine, PHP receives the string test in $_POST['test']

But when trying to parse more data, like this:

$test1 = Get-WMIobject win32_logicaldisk | select-object DeviceID,FreeSpace,Size
$postParams = @{test=$test1}
Invoke-WebRequest -Uri http://bu01/stats/input.php -Method POST -Body $postParams

All result I got is "System.Object[]".

Please explain how to send it to PHP.

Thanks!

UPDATE: Thanks for your tips. I made the following script with foreach to post each "row" in a seperate web request. Which works fine for now. No longer testing with hdd space, but with whitespace in Exchange DB's, but the idea is the same.

$dbs = Get-MailboxDatabase -Status | select-object Name, DatabaseSize, AvailableNewMailboxSpace 
foreach ($a in $dbs){
  $data = $a.Name+','+$a.DatabaseSize.ToKB()+','+$a.AvailableNewMailboxSpace.ToKB()
  $postParams = @{data=$data}
  Invoke-WebRequest -Uri http://bu01/stats/input.php -Method POST -Body $postParams
}

Maybe someone has a good tip to transform this into one request? This is much more efficient for the web server.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongpaocuan7498
    dongpaocuan7498 2014-11-04 19:35
    已采纳

    Essentially, your first example could be coerced into a HTTP request, as it's a simple bit of text. Your second example, however, is resulting in a (guess what?) System.Object object. PowerShell is then failing to do type coercion for you, as the HTTP payload needs to be something textual. If you were to do something like $test1[0].DeviceID.toString(), that would most likely work - for the first object, atleast. BTW, the .toString() may not be needed - it depends on the 'type' of the DeviceID attribute. To determine this, do $test1[0].DeviceID.GetType().

    If you need to process each instance returned by your WMI call, you'll need to use Foreach-Object{}, posting each instance separately. OR... use the Foreach-Object{} to concatenate the relevant information, and post it as one, suitably delimited, text string.

    You may also need to use UrlEncode (.NET call) to encode any odd characters in your data.

    点赞 评论
  • dou18504707
    dou18504707 2014-11-04 19:34

    This is because $test1 is a System.Object array and not string data. A quick and dirty solution would be :

    $test1 = Get-WMIobject win32_logicaldisk | select-object DeviceID,FreeSpace,Size
    $postParams = @{test=($test1 | out-string)}
    Invoke-WebRequest -Uri http://bu01/stats/input.php -Method POST -Body $postParams
    

    In case you want to work with the data on php site the cleaner solution would be to split the data into multiple parameters so you can access them more easily.

    点赞 评论

相关推荐