开发者

Creating an excel file in a users profile (on Desktop) with vbs

开发者 https://www.devze.com 2023-03-18 01:47 出处:网络
(edit) - I was able to figure it out, in case anyone ever needs it... Just needed to add a few lines:

(edit) - I was able to figure it out, in case anyone ever needs it...

Just needed to add a few lines:

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"

I'm sure this question will be no problem for one of you scripting guys. I'm sys admin so my scripting skills are not quite up to speed.

I have a vbs script which I've been hacking away on to take inventory of users PC's using some defi开发者_如何学Pythonned criteria..

When executed, the script basically creates an Excel file, then gathers some info and writes to that file. My problem is that I have to specify a path for the xls output, currently it's saving the file directly to the C:\ drive. For internal users, I've just been saving the file with a UNC path, but I need this for my external users who may not have access to the network.

I would like to have the path automagically set to the users profile like in a bat script ie: %UserProfile%\Desktop\somefolder

I'm just not that familier with .vbs is there an easy way to do this? Here is the script, you will probably notice some extra stuff in there, ignore that, it's from the previous version of this script.

TLDR - How to make the path automatically set to users Desktop (Win XP and 7)?

As usual many, many thanks for reading and or answering!

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"
'=====================


Just needed to add a few lines

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"
0

精彩评论

暂无评论...
验证码 换一张
取 消