在 powershell 中的路径之间添加 space

Adding space between paths in powershell

我的脚本有问题,它会过滤我的日志并在 baretail 中打开相关的日志。我目前的问题是一些文件路径在打印时中间没有 space,而有些文件路径有 space。我一直试图通过输入 + "" + 来获取 spaces,但这绝对没有任何作用。

picture of output

我的代码

$files = ""
[xml]$photonconfig = Get-Content 
C:\Users\Administrator\Desktop\PhotonServer.config

$photonconfig.SelectNodes("Configuration/*")  | Select-Object -Expand Name | 
% {$_.replace("CriticalOps","")} | ForEach {
$files+= Write-Host ""
$files+= Get-ChildItem C:\Users\Administrator\Desktop\log\log/*$_*.log |sort -property LastWriteTime -Descending | Select-Object -first 3 


}

$clr= Get-ChildItem  C:\Users\Administrator\Desktop\log\log/PhotonCLR.log | 
Select-Object 

$all = $files + $clr 

$all

完整代码:

 $files = @()
 [xml]$photonconfig = Get-Content 
 C:\Users\Administrator\Desktop\PhotonServer.config

 $photonconfig.SelectNodes("Configuration/*")  | Select-Object -Expand Name | % {$_.replace("CriticalOps","")} | ForEach {
 $files+= Write-Output ""
 $files+= Get-ChildItem C:\Users\Administrator\Desktop\log\log/*$_*.log |sort -property LastWriteTime -Descending | Select-Object -first 3 


}

$clr= Get-ChildItem  C:\Users\Administrator\Desktop\log\log/PhotonCLR.log | Select-Object 

$all = "$clr " + "$files" 

$cmd=Start-Process C:\Users\Administrator\Desktop\baretail\baretail.exe $all

考虑以下对象类型:

PS D:\PShell> (Get-ChildItem).GetType().FullName
System.Object[]

PS D:\PShell> (Get-ChildItem)[0].GetType().FullName
System.IO.DirectoryInfo

PS D:\PShell> (Get-ChildItem)[-1].GetType().FullName
System.IO.FileInfo

PS D:\PShell> "".GetType().FullName
System.String

PS D:\PShell> ( Write-Host "" ) -eq $null

True

因此,出现了一些自动类型转换,例如在 $files+= Get-ChildItem …

  1. 使用数组$files = @()代替字符串$files = "".
  2. Avoid using Write-Host at all.
  3. 考虑两种 类型转换方法 之间的区别:
    • [xml]$photonconfig = Get-Content C:\…\Desktop\PhotonServer.configstrongly type the variable $photonconfig
    • $photonconfig = [xml]$( Get-Content C:\…\Desktop\PhotonServer.config )(我更喜欢这个变体)