需要格式化 power shell 输出

需要格式化 power shell 输出

我对 powershell 还很陌生,尝试过各种方法来拆分我找到的以下 powershell 脚本的结果,但都没有成功。目前,脚本将所有内容列在 4 列中,每个服务器的结果之间没有间隙。

(当它完成一台服务器的结果时,我希望每个结果之间有一个间隙或甚至一个带有服务器名称的标题,以便在针对多台服务器运行时更​​容易阅读)

任何帮助进行格式化的帮助都将不胜感激。

Get-WmiObject -ComputerName ("server1","server2") Win32_UserProfile | % {
  $userinfo = [WMI] ($_.__Path -Replace "Win32_UserProfile", "Win32_SID")
  New-Object PsObject -Property @{
    ComputerName= $_.__Server 
    Domain      = $userinfo.ReferencedDomainName
    User        = $userinfo.AccountName
    LastUsed    = $_.ConvertToDatetime($_.LastUseTime)

  }
} | export-csv -NoType c:\Results.csv

答案1

我假设,当您说想要“更易读”的输出时,您的意思是希望更容易在控制台上读取输出,而不是在 CSV 文件中读取输出。因为 CSV 文件不是为了美观或便于人类阅读 - CSV 文件是为了便于计算机处理。

因此,我最后忽略了 Export-CSV cmdlet。在我看来,获得良好的控制台输出和获得高效的 CSV 数据是两个完全不同的目标。

当我运行你的脚本时,输出如下所示:

User            ComputerName    Domain          LastUsed             
----            ------------    ------          --------             
Ryan            SERVER1         SERVER1         9/25/2014 11:53:54 PM
NETWORK SERVICE SERVER1         NT AUTHORITY    9/25/2014 11:53:54 PM
LOCAL SERVICE   SERVER1         NT AUTHORITY    9/25/2014 11:53:54 PM
SYSTEM          SERVER1         NT AUTHORITY    9/25/2014 11:53:54 PM
Ryan            SERVER2         SERVER2         9/25/2014 11:53:54 PM
NETWORK SERVICE SERVER2         NT AUTHORITY    9/25/2014 11:53:54 PM
LOCAL SERVICE   SERVER2         NT AUTHORITY    9/25/2014 11:53:54 PM
SYSTEM          SERVER2         NT AUTHORITY    9/25/2014 11:53:54 PM

我明白您的意思,即控制台没有将每台计算机分开或在每台计算机之间留出任何“间隙”。

所以我对你的脚本做了一些修改:

:NextComputer Foreach ($Computer In "localhost", "localhost")
{
    Write-Host "                                      " -ForegroundColor Green -BackgroundColor Black
    Write-Host " Massive Flashy Header For $Computer! " -ForegroundColor Green -BackgroundColor Black
    Try
    {
        $Profiles = Get-WMIObject -ClassName Win32_UserProfile -ComputerName $Computer -ErrorAction Stop
    }
    Catch
    {
        Write-Error "Error while getting user profiles from $Computer`: $($_.Exception.Message)"
        Continue NextComputer
    }    
    Write-Host " User profiles found: $($Profiles.Count)               " -ForegroundColor Green -BackgroundColor Black
    If ($Profiles.Count -LT 1)
    {
        Continue NextComputer
    }
    $ProfileCollection = @()
    :NextProfile Foreach ($Profile In $Profiles)
    {
        $UserInfo = [WMI] ($Profile.__PATH -Replace "Win32_UserProfile", "Win32_SID")
        $ProfileCollection += New-Object PsObject -Property @{
            ComputerName = $Computer 
            Domain       = $UserInfo.ReferencedDomainName
            User         = $UserInfo.AccountName
            LastUsed     = $Profile.ConvertToDatetime($Profile.LastUseTime)
        }
    }
    $ProfileCollection | Format-Table -AutoSize
}

现在输出看起来更易读了,因为每台计算机的结果现在彼此分开了:

 Massive Flashy Header For SERVER1! 
 User profiles found: 4               

User            ComputerName Domain          LastUsed             
----            ------------ ------          --------             
Ryan            SERVER1      SERVER1         9/25/2014 11:56:50 PM
NETWORK SERVICE SERVER1      NT AUTHORITY    9/25/2014 11:56:50 PM
LOCAL SERVICE   SERVER1      NT AUTHORITY    9/25/2014 11:56:50 PM
SYSTEM          SERVER1      NT AUTHORITY    9/25/2014 11:56:50 PM



 Massive Flashy Header For SERVER2! 
 User profiles found: 4               

User            ComputerName Domain          LastUsed             
----            ------------ ------          --------             
Ryan            SERVER2      SERVER2         9/25/2014 11:56:50 PM
NETWORK SERVICE SERVER2      NT AUTHORITY    9/25/2014 11:56:50 PM
LOCAL SERVICE   SERVER2      NT AUTHORITY    9/25/2014 11:56:50 PM
SYSTEM          SERVER2      NT AUTHORITY    9/25/2014 11:56:50 PM

更合你的口味,是吗?

相关内容