32

出力が省略記号 (...) で表示されないように、次のスクリプトの出力について助けが必要です。挿入しようとしまし| Format-Table -Wrap -AutoSizeたが、うまくいかないようです。

 clear-host Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue    
 $services = new-object system.collections.sortedlist
 $servers = (get-spfarm).servers  
 foreach ($server in $servers) {
     foreach($service in $server.serviceinstances)
     {
         if ($service.status = "Online")
         {
             $s = $service.typename
             if ($services.contains($s))
             {
                 $serverlist = $services[$s]
                 $servername = $server.name 
                 $services[$s]  = "$serverlist - $servername"
             }
             else
             {
                 $services[$s] = $server.name
             }
         }
     } } 
  $services

出力:

Name                            Value                                                                           
----                           -----                                                                           
Access Database Service        SE5APP - SE5FE - SE7FE - FAQ3                                          
Application Discovery **and L...** SE5APP - SE5FE - SE7FE - FAQ3                                          
Application Registry Service   SE5APP - SE5FE - SE7FE - FAQ3                                          
4

3 に答える 3

35

Format-Listここでは( fl) またはFormat-Table -auto( ) のいずれかft -autoが役立ちます。

$services | fl

また

$services | ft -auto
于 2012-12-06T02:13:58.933 に答える
0

この状況で私が行うことは、フォーマットの説明を作成し、それを Format-Table コマンドの引数として使用することです。最長のデータを含むデータ フィールドを調べる関数 (Get-MaxLength) を開発し (形式の説明の最後にこれがあると便利です)、返される値で形式の説明の幅を設定します。以下のコードで計算を確認できます。Intel(4) Management Engine Interface の Number 値に注意してください。また、Format-Table コマンドで -Wrap を使用していることにも注意してください。この概念は、すべてのフィールド幅または最後のフィールド幅のみを計算するように変更できます。これはちょっとした計算です。

Function Get-MaxLength {

<#
.SYNOPSIS
   Finds the length of the longest item in collection.

.DESCRIPTION
   Use this Function to get the length of the longest item in a
   collection for use in format strings or other places where
   needed.

.PARAMETER TestObj
    The qualified object to be tested. See example!

.Parameter MinLen
    The minimum length of the item (if using for formatting) which
    should be the Label (title) length. Note if the object item
    being tested does not have a Length property you MUST specify
    the label length!

.OUTPUTS
    Returns a numerical value

.EXAMPLE
   $NameLen = Get-MaxLength -TestObj $DotNet.PSChildName
   $VerLen  = Get-MaxLength -TestObj $DotNet.Version
   $RNLen   = Get-MaxLength -TestObj $DotNet.Release -MinLen 11

     #--- .Net Information ---

 $fmtDotNet =
  @{Expression={$_.PSChildName};Label=".Net Type";Width=$NameLen},
  @{Expression={$_.Version};Label="Version No:";Width=$VerLen},
  @{Expression={$_.Release};Label="Release No:";Width=$RNLen}

  $Dotnet | Format-Table $fmtDotNet
#>

  Param(
    [Parameter(Mandatory=$True)]
     [object] $TestObj,
    [Parameter(Mandatory=$False)]
     [int] $MinLen = 0,
    [Parameter(Mandatory=$False)]
     [int] $MaxLen = 0
  )

   $ErrorActionPreference = "SilentlyContinue"

   foreach ($x in $TestObj) {
     If ($x.Trim().length -gt $MinLen) {
       $MinLen = $x.Trim().length
     }
   }

   If ($MaxLen -ne 0) {
     If ($MinLen -gt $MaxLen) {
       $MinLen = $MaxLen
     }
   }

   $ErrorActionPreference = "Continue"

   Return ,$MinLen

} #End Function -----------  Get-MaxLength  -------------------

  $OstrWidth = 80
  
  $DriverInfo =
  Get-CimInstance -ClassName 'Win32_PNPSignedDriver'         |
  Where-Object -Property DriverProviderName  -ne "Microsoft" |
  Where-Object -Property DeviceName -ne -Value $Null         |
  Sort-Object  -Property DeviceName -Unique

$DriverCnt = $DriverInfo.Count

  $DVLen =
    Get-MaxLength -TestObj $DriverInfo.DriverVersion -MinLen 14
  $DDLen = $OstrWidth - $DVLen

  $fmtDRVR = @{Label="`nDriver Description";Width=$DDLen;
                                 Expression={$_.DeviceName}},
             @{Label="Version Number";    Width=$DVLen;
                                 Expression={$_.DriverVersion}}

  $DrvTitle = "$($DriverCnt) Non-Windows Unique Drivers and " +
              "Version Numbers:" | Out-String

  $DriverInfo =
    $DriverInfo | Format-Table -Property $fmtDRVR -Wrap |
                  Out-String   -Width $OStrWidth

サンプル出力:

Driver Description                                                 Number
-------------------                                                -------------
Alcor Micro USB 2.0 Card Reader                                    2.0.150.10135
ASMedia USB3.1 eXtensible Host Controller                          1.16.42.1
...
Intel(R) HD Graphics 630                                           21.20.16.4550
Intel(R) Management Engine Interface                               1914.12.0.125
                                                                   6
Intel(R) Ready Mode Technology Device                              1.2.0.0
...
Realtek Audio                                                      6.0.1.8248
Samsung NVMe Controller                                            3.0.0.1802
于 2020-09-24T22:27:54.963 に答える