Salida de VBScript: formato a una columna (los datos pueden variar en longitud)

Salida de VBScript: formato a una columna (los datos pueden variar en longitud)

Me gustaría tener un archivo por lotes que solicite una lista de servicios (DisplayName, State, StartMode), pero que se muestren en un formato agradable al mirarlo en un archivo txt.

Esto es lo que tengo que volcarlo en un archivo .csv, pero quiero tenerlo en un archivo txt que tendrá otra información y no será factible usar Excel (normalmente uso el bloc de notas para abrir este archivo y quiero conservarlo). Por aquí).

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service WHERE DisplayName LIKE 'CommVault%'")
wscript.echo chr(34) & "Service Name" & chr(34) & "," & chr(34) & "Status" & chr(34) & "," & chr(34) & "Start Mode" & chr(34)
For Each objService in colListOfServices
    wscript.echo chr(34) & objService.DisplayName & chr(34) & "," & chr(34) & objService.State & chr(34) & "," & chr(34) & objService.StartMode & chr(34)
Next

Respuesta1

Esto le dará una tabla de texto con un formato agradable:

wmic /output:Services.txt service where "DisplayName like 'CommVault%'" get DisplayName,State,StartMode

También puede utilizar los distintos archivos XSL en Windows\System32\wbemy Windows\System32\wbem\en-US(o cualquier código de idioma que utilice su copia de Windows) para formatear elwmicsalida de varias maneras.

Por ejemplo, esto le dará una tabla HTML con un formato agradable:

wmic /output:Services.htm service where "DisplayName like 'CommVault%'" get DisplayName,State,StartMode /format:"%windir%\System32\wbem\en-US\htable.xsl"

Si por alguna razón debe utilizar VBScript, simplemente puede modificar elwscript.echodeclaraciones para generar en cualquier formato que desee, luego redirigir a un archivo de texto o, mejor aún, hacer que el script escriba directamente en un archivo de texto.

Respuesta2

Llame a esto MyServiceViewer.hta.

<html>
<head>
<style>
BODY        {font-size :100%;font-family: Arial, Helvetica, sans-serif;color: black;
        background:URL(images/watermark.gif);background-color: white;
        margin-top:0; margin-left:0pt; margin-right:0pt ; text-align:Justify}
P       {margin-left:40pt;margin-right:10pt}
TABLE       {font-size: 90%; text-align:left; margin-left:40pt;margin-right:10pt;background-color:lavender;width:90%}
THEAD       {color: white;font-weight:bold;background-color:darkblue; margin-left:40pt;margin-right:10pt}
TD      {Vertical-Align:Top;padding:3px}
</style>
</head>
<body>
<OBJECT CLASSID="clsid:333C7BC4-460F-11D0-BC04-0080C7055A83"
    ID=dsoMacro5 WIDTH=0 HEIGHT=0>
    <PARAM NAME="DataURL" VALUE="Services.txt">
    <PARAM NAME="UseHeader" Value="True">
    <PARAM NAME="FieldDelim" VALUE=",">
    <PARAM NAME="Sort"  Value="Service Name">
</OBJECT>
<h3>My Services Database</h3>
<h4>Select a button to filter list</h4>
<p>To search for a word in the Title field use <i>* word *</i>. To search for the first word in a field use <i>Word *</i> or the last word use <i>* word</i>. To search for a string within a word or word use <i>*partialword*</i>. Searches are case sensitive.</i></p>
<p><INPUT Name=tb1 TYPE=Text Value=""> <INPUT ID=cmdNavFirst TYPE=BUTTON VALUE="     Search     " onclick="dsoMacro5.object.filter='Service Name=' + tb1.value;dsoMacro5.reset()"></p>
<p><INPUT ID=cmdNavFirst TYPE=BUTTON VALUE="   Sort Status   " onclick="dsoMacro5.object.sort='Status';dsoMacro5.reset()"></p>
<hr class="body">
<TABLE ID=tblMacro2 DATASRC=#dsoMacro5 OnRowEnter=Alert(tblMacro2.row)>
<THEAD>
<TR>
<TD WIDTH="60%"><b>Service Name</b></TD>
<TD WIDTH="20%"><b>Status</b></TD>
<TD WIDTH="20%"><b>Start Mode</b></TD>
</TR>
</THEAD>
<TBODY>
<TR>
<TD WIDTH="20%"><SPAN DATAFLD="Service Name"></SPAN></TD>
<TD WIDTH="60%"><SPAN DATAFLD="Status"></SPAN></TD>
<TD WIDTH="20%"><SPAN DATAFLD="Start Mode"></SPAN></TD>
</TR>
</TBODY>
</TABLE>
</body>
</html>

Y llame a este archivo services.txt

Service Name,Status,Start Mode
Workstation,Ok,Manual
Workstation,Ok,Manual
Workstation,Ok,Manual
Workstation,Ok,Manual
Server,Ok,Manual
Server,Ok,Manual
Server,Ok,Manual
Server,Ok,Manual
BFE,Ok,Auto
BFE,Ok,Auto
BFE,Ok,Auto

Para su pregunta específica, no funcionará en fuentes de ancho variable, vbs + bloc de notas. Si cambia a una fuente de ancho fijo en el bloc de notas...

Para rellenar con 20 caracteres de ancho fijo

A="Running"

MsgBox A & Space(20 - Len(A)) & "|end of marker so you can see spaces in msgbox"

A menudo, simplemente colocar pestañas entre columnas también funcionará al 95%, 100% si los datos tienen la misma longitud o casi la misma. Nuevamente, configurar pestañas más grandes que las estándar en un visor (pero no en el bloc de notas) puede hacer que esto funcione. Normalmente uso Word, pero si estás en Windows de 32 bits, Editar puede tener anchos de pestaña personalizados.

información relacionada