使用vbs在用户配置文件(在桌面上)创建Excel文件

时间:2011-07-07 02:43:03

标签: windows vbscript wsh inventory

(编辑) - 我能够弄明白,万一有人需要它......

只需要添加几行:

dim WSHShell, DesktopPath, objFSO

set objFSO=CreateObject("Scripting.FileSystemObject")
Set WSHshell = CreateObject("WScript.Shell")
DesktopPath = WSHShell.SpecialFolders("Desktop")

strWorkBook = (DesktopPath) & "\" & WshNetwork.ComputerName & "_" & WshNetwork.UserName & "-hardware.xls"

我确信这个问题对你们其中一个脚本人来说没问题。我是系统管理员,所以我的脚本技能不是很快。

我有一个vbs脚本,我一直在使用一些定义的标准来浏览用户PC的库存。

执行时,脚本基本上创建一个Excel文件,然后收集一些信息并写入该文件。我的问题是我必须为xls输出指定一个路径,目前它将文件直接保存到C:\驱动器。对于内部用户,我刚刚使用UNC路径保存文件,但是对于可能无法访问网络的外部用户,我需要这样做。

我想将路径自动设置为用户配置文件,如在bat脚本中,即:%UserProfile%\ Desktop \ somefolder

我只是不那么熟悉.vbs是否有一种简单的方法可以做到这一点?这是脚本,你可能会注意到那里有一些额外的东西,忽略它,它来自这个脚本的先前版本。

TLDR - 如何将路径自动设置为用户桌面(Win XP和7)?

像往常一样,非常感谢阅读和回答!

Set WshNetwork = WScript.CreateObject("WScript.Network")

strComputer = "."

strWorkBook = "C:\" & WshNetwork.ComputerName & "_" & WshNetwork.UserName & "-hardware.xls"

' Create Excel Spreadsheet
strFileName = "C:\" & WshNetwork.ComputerName & "_" & WshNetwork.UserName & "-hardware.xls"
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add()
objWorkbook.SaveAs(strFileName)
objExcel.Quit
Set app = CreateObject("Excel.Application")
app.Workbooks.Open strWorkBook
Set wb = app.ActiveWorkbook
app.Visible = False
wb.Activate
Set ws = wb.Worksheets(1)
ws.Cells(1,1).Value = "System Name"
ws.Columns(1).ColumnWidth = 20

ws.Cells(1,2).Value = "Username"
ws.Columns(2).ColumnWidth = 30

ws.Cells(1,3).Value = "Main/Test System"
ws.Columns(3).ColumnWidth = 30

ws.Cells(1,4).Value = "Special Applications"
ws.Columns(4).ColumnWidth = 30

ws.Cells(1,5).Value = "Location of the system"
ws.Columns(5).ColumnWidth = 30

ws.Cells(1,6).Value = "Used by which department"
ws.Columns(6).ColumnWidth = 30

ws.Cells(1,7).Value = "if not DHCP / ip address"
ws.Columns(7).ColumnWidth = 30

ws.Cells(1,8).Value = "local admin rights"
ws.Columns(8).ColumnWidth = 30

ws.Cells(1,9).Value = "Operating System"
ws.Columns(9).ColumnWidth = 40

ws.Cells(1,10).Value = "Disk space available on hard drive"
ws.Columns(10).ColumnWidth = 20

ws.Cells(1,11).Value = "CPU"
ws.Columns(11).ColumnWidth = 30

ws.Cells(1,12).Value = "Total Memory"
ws.Columns(12).ColumnWidth = 20

ws.Cells(1,13).Value = "Manufacturer"
ws.Columns(13).ColumnWidth = 20

ws.Cells(1,14).Value = "Model"
ws.Columns(14).ColumnWidth = 20

ws.Cells(1,15).Value = "Serial Number"
ws.Columns(15).ColumnWidth = 30

ws.Rows(1).Font.Bold = True

' Get the serial number first to see if it already exists in the spreadsheet
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_BIOS",,48)
For Each objItem In colItems
strSerialNumber = "" & objItem.SerialNumber
Next
Set objWMIService = Nothing
Set colItems = Nothing

intRowToUse = -1
For intRowCount = 2 To ws.UsedRange.Rows.Count
      If Trim(strSerialNumber) = Trim(ws.Cells(intRowCount,5).Value) Then
            intRowToUse = intRowCount
      End If
Next
If intRowToUse = -1 Then
      intRowToUse = ws.UsedRange.Rows.Count + 1
End If

' Get Computer System Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
For Each objItem In colItems
    ws.Cells(intRowToUse,1).Value = "" & objItem.Caption 
    ws.Cells(intRowToUse,2).Value = "" & objItem.UserName
    ws.Cells(intRowToUse,13).Value = "" & objItem.Manufacturer
    ws.Cells(intRowToUse,14).Value = "" & objItem.Model
Next
Set objWMIService = Nothing
Set colItems = Nothing
'
'Output the Serial Number
ws.Cells(intRowToUse,15).Value = strSerialNumber

'
' Get CPU Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Processor",,48)
For Each objItem In colItems
    ws.Cells(intRowToUse,11).Value = "" & objItem.Name 
Next
Set objWMIService = Nothing
Set colItems = Nothing
'
' Get C: free space
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk where DeviceID='c:'",,48)
For Each objItem In colItems
        ws.Cells(intRowToUse,10).Value = "" & FormatNumber(objItem.FreeSpace/1024/1024/1024,0) & "GB"
Next
Set objWMIService = Nothing
Set colItems = Nothing
'
' Get OS Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
For Each objItem In colItems
    ws.Cells(intRowToUse,9).Value = "" & objItem.Caption
    ws.Cells(intRowToUse,12).Value = "" & FormatNumber(objItem.TotalVisibleMemorySize/1024/1024,0) & "GB"  
Next
Set objWMIService = Nothing
Set colItems = Nothing
'
' Get & Writeout current Date
'ws.Cells(intRowToUse,12).value = "" & Month(Now) & "-" & Day(Now) & "-" & Year(Now)

'Autofit all columns
app.ActiveSheet.Columns.EntireColumn.AutoFit
' Save Audit File
app.DisplayAlerts = False
'wb.Saved = True
wb.Close True
app.DisplayAlerts = False
app.quit

MsgBox "Done"
'=====================

1 个答案:

答案 0 :(得分:0)

只需添加几行

dim WSHShell, DesktopPath, objFSO

set objFSO=CreateObject("Scripting.FileSystemObject")
Set WSHshell = CreateObject("WScript.Shell")
DesktopPath = WSHShell.SpecialFolders("Desktop")

strWorkBook = (DesktopPath) & "\" & WshNetwork.ComputerName & "_" & WshNetwork.UserName & "-hardware.xls"