PowerShell:删除尾随“...”

时间:2011-05-31 19:59:45

标签: string powershell

我正在尝试创建一个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                       ...

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

$tasks | Select-String -pattern "Disabled" | ForEach-Object
{ $_ -replace "Disabled", ""} | ForEach-Object { $_ -replace "Could not start",
""} | ForEach-Object { $_.Trim() }

完全符合我的要求。

答案 2 :(得分:0)

我不确定$ 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"}