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

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

图片转代码服务由CSDN问答提供 功能建议

很抱歉这是一个新手,但我想学习一些关于将PowerShell结果发送到PHP页面的内容 我尝试了这段代码:

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

哪个工作正常,PHP收到字符串测试 在$ _POST ['test']

但是在尝试解析更多数据时,如下所示:

  $ 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 
    
 
 

我得到的所有结果都是“System.Object []”。

请解释如何将其发送给PHP。

谢谢!

更新: 感谢您提示。 我使用foreach制作了以下脚本,以便在单独的Web请求中发布每个“行”。 哪个现在好用。 不再使用hdd空间进行测试,而是在Exchange数据库中使用空格,但想法是一样的。

  $ dbs = Get-MailboxDatabase -Status |  select-object Name,DatabaseSize,AvailableNewMailboxSpace 
foreach($ d 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 
} 
   
 
 

也许有人有一个很好的提示将其转换为一个请求? 这对Web服务器来说效率更高。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • 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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题