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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧