簡體   English   中英

在Powershell中的路徑之間添加空間

[英]Adding space between paths in powershell

我的腳本出現問題,該腳本過濾了我的日志並裸露地打開了相關日志。 目前,我的問題是某些文件路徑在打印時之間沒有空格,而有些有空格。 我一直在嘗試通過添加+“” +來獲得空格,但這絕對沒有任何作用。

輸出圖片

我的密碼

$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. 完全避免使用Write-Host
  3. 考慮兩種類型轉換方法之間的區別:
    • [xml]$photonconfig = Get-Content C:\\…\\Desktop\\PhotonServer.config強烈鍵入變量 $photonconfig
    • $photonconfig = [xml]$( Get-Content C:\\…\\Desktop\\PhotonServer.config ) (我希望使用此變體)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM