PHP 到 Powershell 脚本
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>
我想到了这个,但如果失败了 'Unexpected namespace error'
$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
完成后,一般任务是去除这些字符:"\n"、"\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"
我不太了解 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>
我想到了这个,但如果失败了 'Unexpected namespace error'
$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
完成后,一般任务是去除这些字符:"\n"、"\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"