【问题标题】:PowerShell: Remove Trailing "..."PowerShell:删除尾随“...”
【发布时间】:2011-05-31 19:59:45
【问题描述】:

我正在尝试创建一个将获取计划任务列表的 powershell 脚本。我已经获得了完整的任务列表,但是我需要删除尾随的“...”

你是怎么做到的?

$tasks | Select-String -pattern "Disabled" | ft @{Expression
={$_.Line};Label="Line";width=44}

输出:

Line
----
AD RMS Rights Policy Template Management ...
AD RMS Rights Policy Template Management ...
Proxy                                    ...
UserTask                                 ...
UserTask-Roam                            ...
Consolidator                             ...
KernelCeipTask                           ...
UsbCeip                                  ...
ScheduledDefrag                          ...
Scheduled                                ...
Microsoft-Windows-DiskDiagnosticDataColl ...
Microsoft-Windows-DiskDiagnosticResolver ...
Notifications                            ...
WinSAT                                   ...
ActivateWindowsSearch                    ...
ConfigureInternetTimeService             ...
DispatchRecoveryTasks                    ...
ehDRMInit                                ...
InstallPlayReady                         ...
mcupdate                                 ...
MediaCenterRecoveryTask                  ...
ObjectStoreRecoveryTask                  ...
OCURActivate                             ...
OCURDiscovery                            ...
PBDADiscovery                            ...
PBDADiscoveryW1                          ...
PBDADiscoveryW2                          ...
PeriodicScanRetry                        ...
PvrRecoveryTask                          ...
PvrScheduleTask                          ...
RecordingRestart                         ...
RegisterSearch                           ...
ReindexSearchRoot                        ...
SqlLiteRecoveryTask                      ...
UpdateRecordPath                         ...
CorruptionDetector                       ...
DecompressionFailureDetector             ...
HotStart                                 ...
LPRemove                                 ...
SystemSoundsService                      ...
GatherNetworkInfo                        ...
Background Synchronization               ...
Logon Synchronization                    ...
AnalyzeSystem                            ...
RacTask                                  ...
RegIdleBackup                            ...
WindowsParentalControls                  ...
WindowsParentalControlsMigration         ...
AutoWake                                 ...
GadgetManager                            ...
SessionAgent                             ...
SystemDataProviders                      ...
SR                                       ...
Interactive                              ...
IpAddressConflict1                       ...
IpAddressConflict2                       ...
MsCtfMonitor                             ...
SynchronizeTime                          ...
ResolutionHost                           ...
QueueReporting                           ...
BfeOnServiceStartTypeChange              ...
UpdateLibrary                            ...
ConfigNotification                       ...
Calibration Loader                       ...

【问题讨论】:

  • 这是答案:但不能提交; $任务 |选择字符串模式“已禁用”| ForEach-Object { $_ -replace "已禁用", ""} | ForEach-Object { $_ -replace "无法启动", ""} | ForEach-Object { $_.Trim() }

标签: string powershell


【解决方案1】:

不要使用 Format-Table (ft),因为它会尝试将数据放入控制台中可用的列数内,并且您已将“行”列的空间限制为 44 个字符。试试这个:

$tasks | Select-String -pattern "Disabled" | Foreach {$_.Line}

【讨论】:

    【解决方案2】:
    $tasks | Select-String -pattern "Disabled" | ForEach-Object
    { $_ -replace "Disabled", ""} | ForEach-Object { $_ -replace "Could not start",
    ""} | ForEach-Object { $_.Trim() }
    

    完全按照我的意愿行事。

    【讨论】:

      【解决方案3】:

      我不确定 $tasks 里面有什么,所以我根据 get-service 编写了这个,但它应该适用于你的情况:

      $tasks | Select-String -pattern "Disabled" | ft @{Expression=
      {$intLength = 44; if($_.Line.length -lt $intLength) {$intLength=$_.Line.length} $_.Line.substring(0,$intLength)};Label="Line"}
      

      【讨论】:

        猜你喜欢
        • 2011-05-30
        • 2012-01-26
        • 2015-04-27
        • 2020-07-23
        • 2011-01-03
        • 2021-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多