Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
powershell-循环输出中的神秘空白_Powershell_Powershell 5.0 - Fatal编程技术网

powershell-循环输出中的神秘空白

powershell-循环输出中的神秘空白,powershell,powershell-5.0,Powershell,Powershell 5.0,我从下面的代码部分将空白添加到我的输出中。我需要以某种方式格式化它,所以为了找出这个空白的来源,我只是输出变量。我甚至添加了.trim(),以确保它不是来自变量本身。这个空白是从哪里来的 #sort by Name and sort Values of each $output += foreach($icon in $table.GetEnumerator() | sort -Property Name) { $icon.Name.trim() foreach($type in

我从下面的代码部分将空白添加到我的输出中。我需要以某种方式格式化它,所以为了找出这个空白的来源,我只是输出变量。我甚至添加了
.trim()
,以确保它不是来自变量本身。这个空白是从哪里来的

#sort by Name and sort Values of each
$output += foreach($icon in $table.GetEnumerator() | sort -Property Name) {
    $icon.Name.trim()
    foreach($type in $icon.Value | sort) {
        $fa_types[$type].trim()
    }
}

#Output to file
"version: " + $fa_version + "`r`nicons:`r`n" + $output | Out-File $output_file
输出文件示例:

version: 5.13.0
icons:
arrow-circle-right solid regular calendar-week regular users solid usb-drive solid file-alt regular key solid user-graduate solid comment-dots regular plus solid calendar-check regular spinner regular stopwatch regular file-search solid user-chart solid map-marker-alt regular calculator regular apple brands 

在Windows 10上运行powershell版本5。

这是一种创建字符串的奇怪方式。。。我推荐一种更安全的方法,其中不涉及PowerShell的输出功能:

#按名称排序并对每个
$output=“”
foreach($table.GetEnumerator()中的图标| sort-属性名){
$output+=$icon.Name
foreach($输入$icon.Value |排序){
$output+=$fa_类型[$type]
}
}
#输出到文件
版本:“+$fa_version+”`r`nicons:`r`n”+$output | Out File$output_File

创建字符串的方法很奇怪。。。我推荐一种更安全的方法,其中不涉及PowerShell的输出功能:

#按名称排序并对每个
$output=“”
foreach($table.GetEnumerator()中的图标| sort-属性名){
$output+=$icon.Name
foreach($输入$icon.Value |排序){
$output+=$fa_类型[$type]
}
}
#输出到文件
版本:“+$fa_version+”`r`nicons:`r`n”+$output | Out File$output_File

发生这种情况的原因是您正在打印字符串中的数组。当您在项目上循环并仅打印
$fa\u types[$type]
时,它会将其作为数组项写入
$output

如果只打印
$output
,您将看到多个项目以新行分隔,但如果将其放在字符串中,则其由空格分隔符表示

示例:

$outp = foreach($var in (0..5)) { $var }
$outp

# shows the following output
# 0
# 1 
# 2
# 3
# 4
# 5

Write-Output "string  $outp  end"

# prints it in a single line
# string  0 1 2 3 4 5  end
您可以通过联接连接数组,以便在输出中不打印空格

"version: " + $fa_version + "`r`nicons:`r`n" + $output -join "" | Out-File $output_file
#or
"version: " + $fa_version + "`r`nicons:`r`n" + -join $output | Out-File $output_file

发生这种情况的原因是您正在打印字符串中的数组。当您在项目上循环并仅打印
$fa\u types[$type]
时,它会将其作为数组项写入
$output

如果只打印
$output
,您将看到多个项目以新行分隔,但如果将其放在字符串中,则其由空格分隔符表示

示例:

$outp = foreach($var in (0..5)) { $var }
$outp

# shows the following output
# 0
# 1 
# 2
# 3
# 4
# 5

Write-Output "string  $outp  end"

# prints it in a single line
# string  0 1 2 3 4 5  end
您可以通过联接连接数组,以便在输出中不打印空格

"version: " + $fa_version + "`r`nicons:`r`n" + $output -join "" | Out-File $output_file
#or
"version: " + $fa_version + "`r`nicons:`r`n" + -join $output | Out-File $output_file

谢谢你解释为什么会这样。我想尽一切办法。。。例如将变量转换为字符串、trim等,但无法理解空格的来源。感谢您对其实际发生原因的解释。我想尽一切办法。。。例如将变量转换为字符串、trim等,但无法理解空格的来源。谢谢。是的,我确实更喜欢这种方式,因为它更有意义地查看正在发生的事情,而不是输出函数。我还在习惯使用powershell,但我会慢慢习惯的。谢谢。是的,我确实更喜欢这种方式,因为它更有意义地查看正在发生的事情,而不是输出函数。仍然习惯于使用powershell,但随着时间的推移,我已经习惯了。