Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBScript On Error

Posted on 2011-02-16
3
Medium Priority
?
342 Views
Last Modified: 2012-05-11
The following script works fine except that if the path that I want to copy to doesn't exist. I want to cater for this and write to the log file something like " PCName path not found" and carry on with the next entry in the input file

Thanks
Cheers


Dim objFSO, objFile, wmiQuery, objWMIService, objPing, objStatus
Set objShell = CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
ComputersList = objShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop\inputlist2.txt"
SourceFile    = objShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop\Interaction Training.url"
Const intForAppending = 8
ReportFile = objShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop\outputlist2.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutput = objFSO.OpenTextFile(ReportFile, intForAppending, False)
arrComputers  = Split(objFSO.OpenTextFile(ComputersList).ReadAll,vbNewLine)
 
For Each PC in arrComputers
  username = "psr" & mid(pc,2,3) & "a"
  Set objping = objWMIService.ExecQuery("Select * From Win32_PingStatus Where " & "Address = '" & PC & "'")
  for each objstatus in objping
       If IsNull(objStatus.StatusCode) Or objStatus.Statuscode <> 0 Then
           ResolveIP = PC & " " & "Failed"
       Else
           objFSO.CopyFile SourceFile, "\\" & PC & "\C$\" & "Documents and Settings\" & username & "\desktop\"
           ResolveIP = PC & " " & objStatus.ProtocolAddress & " Done"
       End If
       ObjOutput.WriteLine(resolveip)
  next
Next

objOutput.Close

Wscript.echo "Job Done"
0
Comment
Question by:Bianchi928
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 34912021
Hi, try this.

I added this:

                DestinationPath = "\\" & PC & "\C$\" & "Documents and Settings\" & username & "\desktop\"
                If objFSO.FolderExists(DestinationPath) = True Then


Regards,

Rob.
Dim objFSO, objFile, wmiQuery, objWMIService, objPing, objStatus
Set objShell = CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
ComputersList = objShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop\inputlist2.txt"
SourceFile    = objShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop\Interaction Training.url"
Const intForAppending = 8
ReportFile = objShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop\outputlist2.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutput = objFSO.OpenTextFile(ReportFile, intForAppending, False)
arrComputers  = Split(objFSO.OpenTextFile(ComputersList).ReadAll,vbNewLine)
 
For Each PC in arrComputers
  username = "psr" & mid(pc,2,3) & "a"
  Set objping = objWMIService.ExecQuery("Select * From Win32_PingStatus Where " & "Address = '" & PC & "'")
  for each objstatus in objping
       If IsNull(objStatus.StatusCode) Or objStatus.Statuscode <> 0 Then
           ResolveIP = PC & " " & "Failed"
       Else
       	   DestinationPath = "\\" & PC & "\C$\" & "Documents and Settings\" & username & "\desktop\"
       	   If objFSO.FolderExists(DestinationPath) = True Then
           	objFSO.CopyFile SourceFile, DestinationPath 
           	ResolveIP = PC & " " & objStatus.ProtocolAddress & " Done"
           Else
           	objOutput.WriteLine DestinationPath & " was not found."
           End If
       End If
       ObjOutput.WriteLine(resolveip)
  next
Next

objOutput.Close

Wscript.echo "Job Done"

Open in new window

0
 
LVL 16

Expert Comment

by:AlexPace
ID: 34912076
In this specific case Rob was able to work around the error by testing the path first.

When that isn't possible you have to do somethink like this:
on error resume next
<< code that might cause an error >>
If Err.Number <> 0 Then
  'handle error here
End if
0
 

Author Closing Comment

by:Bianchi928
ID: 34913032
All good mate

Cheers
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question