为什么Windows Server 2016中的“添加内容”是在一行上而不是在Windows 10 PC这样的多行上写文本?

我正在使用脚本来使用其API在Google Maps中搜索地点。我使用本地PC(Windows 10)作为开发环境。重要的代码部分如下:

$api_call_url ="https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=$lat,$lon&radius=$rad&type=$place_type&key=$Api_Key"
$api_call_response=Invoke-WebRequest $api_call_url
$api_call_obj = ConvertFrom-Json -InputObject $api_call_response
Add-Content "$file_name_json" $api_call_response

Google返回的json存储在$ api_call_response变量中,然后使用Add-Content附加到文本文件中。脚本完成并经过测试后,我将其移至生产环境(Windows Server 2016 Std)。运行脚本,发现将json写入文本文件的方式不同。

这就是从Windows 10 PC写入文本文件的内容(每个结果多行)

{
    "html_attributions" : [],"results" : [],"status" : "ZERO_RESULTS"
}

{

    "html_attributions" : [],"status" : "ZERO_RESULTS"
}

这是从Windows Server 2016中写入文本文件的内容(每个结果一行)

 {   "html_attributions" : [],"status" : "ZERO_RESULTS"}
 {   "html_attributions" : [],"status" : "ZERO_RESULTS"}

如果我将变量$ api_call_response写输出到屏幕,它将显示多行。

我需要多行输出,因为还有另一个脚本可以从不需要的结果中清除文件,并且由于生成的文本文件中附加了许多json,因此只能从所有json中创建一个json文件。但是它希望结果文件能像我的开发环境编写它那样。显然,我试图避免修改清理脚本。

PSVersion on Windows Server 2016: 5.1.14393.3053
PSVersion on Windows 10: 5.1.17763.771

有什么想法吗?

shiliuguo 回答:为什么Windows Server 2016中的“添加内容”是在一行上而不是在Windows 10 PC这样的多行上写文本?

好吧,当使用写字板打开文件时,它是按照我所需的方式编写的,所以我想这必须是记事本显示文件的方式。

,

如果要将Unix文本(\ n)转换为Windows文本(\ r \ n):

get-content file1 | set-content file2

或具有相同文件:

(get-content file) | set-content file

记事本不能正确显示UNIX文本,但写字板可以。 (尽管记事本可以处理unicode no bom。)

其他转换方法:Unix newlines to windows newlines (on Windows)

本文链接:https://www.f2er.com/2910703.html

大家都在问