我是相当新的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
我会假设,当你说你想“更容易阅读”的输出,你的意思是希望更容易阅读控制台上的输出,而不是在该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
更喜欢你的喜好,是吗?