如何在输出文件名中包含主机名和日期?

时间:2015-06-03 06:25:32

标签: batch-file vbscript wmi-query

我有一个批处理文件,可以安装软件和更新。它看起来像这样:

call wmic /output:"ServerName-InstalledSoftware-MMDDYYYY.csv" qfe get /format:csv

每次从服务器运行时,我都必须使用当前服务器和日期修改批处理文件。

有没有办法可以将其转换为自动获取%computername%变量和MMDDYYYY日期值的VBScript文件?

2 个答案:

答案 0 :(得分:1)

从记忆中,回复MMDDYYY部分(在单独的行中)

echo %DATE:~4,2%
echo %DATE:~7,2%
echo %DATE:~-4%

这将为您提供服务器名称:

echo %COMPUTERNAME%

所以你的命令应该是

call wmic /output:"%COMPUTERNAME%-InstalledSoftware-%DATE:~4,2%%DATE:~7,2%%DATE:~-4%.csv" qfe get /format:csv

你可以通过用echo代替call wmic / output来试试这个:

echo "%COMPUTERNAME%-InstalledSoftware-%DATE:~4,2%%DATE:~7,2%%DATE:~-4%.csv"

答案 1 :(得分:0)

如果你想在VBScript中这样做,你可以在VBScript中完成:

Set fso = CreateObject("Scripting.FileSystemObject")
Set net = CreateObject("WScript.Network")
Set wmi = GetObject("winmgmts://./root/cimv2")

today = Right("00" & Month(Date), 2) & Right("00" & Day(Date), 2) & Year(Date)

filename = net.ComputerName & "-InstalledSoftware-" & today & ".csv"
dir      = "C:\output\folder"

Set f = fso.OpenTextFile(fso.BuildPath(dir, filename), 2, True)

f.WriteLine "Node,Caption,CSName,Description,FixComments,HotFixID,InstallDate," & _
            "InstalledBy,InstalledOn,Name,ServicePackInEffect,Status"

For Each qf In wmi.ExecQuery("SELECT * FROM Win32_QuickFixEngineering")
  f.WriteLine net.ComputerName & _
    "," & qf.Caption & _
    "," & qf.CSName & _
    "," & qf.Description & _
    "," & qf.FixComments & _
    "," & qf.HotFixID & _
    "," & qf.InstallDate & _
    "," & qf.InstalledBy & _
    "," & qf.InstalledOn & _
    "," & qf.Name & _
    "," & qf.ServicePackInEffect & _
    "," & qf.Status
Next

f.Close

我建议您将日期格式更改为ISO日期格式,这样您的文件将自动按正确的日期顺序排列:

today = Year(Date) & Right("00" & Month(Date), 2) & Right("00" & Day(Date), 2)