PHP到Powershell脚本
我对PHP了解不多,但我继承了一个PHP脚本,我想将其转换为powershell。我不确定这是否可行。但脚本似乎是从服务中获取一个XML形式的产品列表。然后它进行一些cr/lf剥离,交换几个引号字符,然后将xml转换为json。看起来很直截了当。我对powershell也不是很了解,所以我希望这对这里的人来说是一个简单的任务PHP到Powershell脚本,php,powershell,Php,Powershell,我对PHP了解不多,但我继承了一个PHP脚本,我想将其转换为powershell。我不确定这是否可行。但脚本似乎是从服务中获取一个XML形式的产品列表。然后它进行一些cr/lf剥离,交换几个引号字符,然后将xml转换为json。看起来很直截了当。我对powershell也不是很了解,所以我希望这对这里的人来说是一个简单的任务 <?php $fileContents= file_get_contents("http://svc.mysite.com/api/GetItems");
<?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);
?>
我们需要看看这行的回复是什么:
$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有一个convertfromJSON命令,这也使得这方面变得简单
如果您回复我上面给出的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信封和内容,并且在
还有一件事。您很可能会将第一行替换为
[xml]$string = Invoke-WebRequest "http://svc.mysite.com/api/GetItems"
如果你不懂这两种语言/系统,听起来你需要一个自由职业者,而不是一个问答网站。我不是提出要求的人。我们已经将工作分包出去,并收到了这份PHP。我们明确要求使用powershell。如果它更复杂,我就不会在这里提交了。但如果需要,我会自己解决的。不需要自由职业者。听起来你的分包商需要一个脚印,直到他吐出指定的货物。承包商提出的理由是,使用PowerShell很难做到这一点。我正试图证明他错了。我开始寻找解决方案,但遇到了一些问题,所以我来到这里。但是,我没想到会有负面反馈和拒绝帮助。我已经分享了xml。我正在用完整的ConvertToJSON过程更新我的另一个回复。我认为这足以让我完成它。谢谢
[
{
"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"
}
]
[xml]$string = Invoke-WebRequest "http://svc.mysite.com/api/GetItems"