PHP to Powershell script


PHP to Powershell script

我对PHP了解不多,但我继承了一个PHP脚本,我想将其转换为powershell。 我不确定这是否可能。但看起来脚本正在从服务中获取 XML 形式的产品列表。 然后,它执行一些 cr/lf 剥离,交换一些引号字符,然后将 xml 转换为 json。 似乎很简单。 我对powershell也不是很了解,所以我希望这对这里的某人来说是一项简单的任务。

<?php
    $fileContents= file_get_contents("http://svc.mysite.com/api/GetItems");
    $fileContents = str_replace(array("'n", "'r", "'t"), '', $fileContents);
    $fileContents = trim(str_replace('"', "'", $fileContents));
    $simpleXml = simplexml_load_string($fileContents);
    $map = json_decode(json_encode($simpleXml));
    print json_encode($map->item);
?>

下面是 XML 数据的示例

<?xml version="1.0" encoding="utf-8" ?>
<items xmlns="http://svc.tricorbraun.com/api/GetItems" version="1">
    <item><ItemID>000072</ItemID><ItemName>Dropper Tip Closure</ItemName><ItemGroup>Closures</ItemGroup><Shape>Round</Shape><Style>Fitment/Plug</Style></item>
    <item><ItemID>000182</ItemID><ItemName>28-480, P/P Push and Turn Closure</ItemName><ItemGroup>Closures</ItemGroup><Shape>Round</Shape><Style>Child Resistant</Style></item>
    <item><ItemID>000187</ItemID><ItemName>28-480, P/P Child Resistant Closure</ItemName><ItemGroup>Closures</ItemGroup><Shape>Round</Shape><Style>Child Resistant</Style></item>
    <item><ItemID>000190</ItemID><ItemName>28mm SPEC, P/P Dispensing Closure</ItemName><ItemGroup>Closures</ItemGroup><Shape>Round</Shape><Style>Dispensing</Style></item>
</items>

我想出了这个,但如果失败并出现"意外的命名空间错误"

    $doc = New-Object System.Xml.XmlDocument
$memStream = new-object System.IO.MemoryStream
$jsonWriter = [System.Runtime.Serialization.Json.JsonReaderWriterFactory]::CreateJsonWriter($memStream)
$doc.Load("http://svc.mysite.com/api/GetItems")
$doc.Save($jsonWriter)
$bytes = $memStream.ToArray()
[System.Text.Encoding]::UTF8.GetString($bytes,0,$bytes.Length)
Write-Output $jsonWriter.ToString()

我们需要从这一行看到回复是什么:

$fileContents= file_get_contents("http://svc.mysite.com/api/GetItems");

您可以在PowerShell中运行以下内容并将结果抛到PasteBin或其他地方吗?

Invoke-WebRequest "http://svc.mysite.com/api/GetItems" | 
  Export-CLIXMl C:'temp'pathTo.XML

完成此操作后,常规任务是去除这些字符:"'","''r","''t",这很容易,并将双引号替换为单引号,这很容易。PowerShell有一个ConvertFrom-Json命令,这也使这方面变得容易。

如果您回复我上面给出的 PowerShell 代码的示例输出,我可以帮助您解决其余问题。


感谢您回复完整的文件类型。

假设我们在.xml文本文件中具有.xml输出,此过程将为您提供 JSON 对象,所有对象都使用 PowerShell。 这真的很简单。

[xml]$string = get-content C:'temp'Pathto.xml
$string.items.Item  | Select-Object Item*,Shape,Style | ConvertTo-Json

输出

[
{
    "ItemID":  "000072",
    "ItemName":  "Dropper Tip Closure",
    "ItemGroup":  "Closures",
    "Shape":  "Round",
    "Style":  "Fitment/Plug"
},
{
    "ItemID":  "000182",
    "ItemName":  "28-480, P/P Push and Turn Closure",
    "ItemGroup":  "Closures",
    "Shape":  "Round",
    "Style":  "Child Resistant"
},
{
    "ItemID":  "000187",
    "ItemName":  "28-480, P/P Child Resistant Closure",
    "ItemGroup":  "Closures",
    "Shape":  "Round",
    "Style":  "Child Resistant"
},
{
    "ItemID":  "000190",
    "ItemName":  "28mm SPEC, P/P Dispensing Closure",
    "ItemGroup":  "Closures",
    "Shape":  "Round",
    "Style":  "Dispensing"
}
]

我不是每天都使用 JSOn,但这对我来说确实看起来像一个有效的 JSON 信封和内容,并且它会在 http://jsonlint.com/

还有一件事。 您很可能将第一行替换为

[xml]$string = Invoke-WebRequest "http://svc.mysite.com/api/GetItems"