Solved

CReate folder on remote computer

Posted on 2011-03-16
9
1,366 Views
Last Modified: 2012-05-11
I need some help as to why the following script is not creating  c:\scripts folder on the list of remote computer or telling me that it exists when really it doesn't .I check on the first 2 remote computers, I can't see any folders there. The 3rd one seems okay because it couldn't ping it.

U104C2 C:\scripts Already exists
U114C1 C:\scripts Already exists
U999c1 Failed




Dim objFSO, objFile, wmiQuery, objWMIService, objPing, objStatus
Set objShell = CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
FolderName = "C:\scripts"

InputFile  = objShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop\CreateFolderInput.txt"
ReportFile = objShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop\CreateFolderOutput.txt"

Const intForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutput = objFSO.OpenTextFile(ReportFile, intForAppending, False)

arrComputers  = Split(objFSO.OpenTextFile(InputFile).ReadAll,vbNewLine)
 
For Each PC in arrComputers
 
  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$\"
                If objFSO.FolderExists(DestinationPath) = True then
              If not objFSO.FolderExists(Foldername) then
                  objFSO.CreateFolder(FolderName)
                 ResolveIP = PC & " " & Foldername & " Created"
              else
                ResolveIP = PC & " " & Foldername & " Already exists"
              end if
           Else
                 ResolveIP = PC & " " & DestinationPath & " was not found."
           End If
       End If
       ObjOutput.WriteLine(resolveip)
  next
Next

objOutput.Close

Wscript.echo "Job Done"





0
Comment
Question by:Bianchi928
  • 5
  • 4
9 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 250 total points
ID: 35153745
Hi, try this.  You need to convert the remote folder to UNC path, otherwise you're just creating it on your own PC.

Regards,

Rob.
Dim objFSO, objFile, wmiQuery, objWMIService, objPing, objStatus
Set objShell = CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
FolderName = "C:\scripts"

InputFile  = objShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop\CreateFolderInput.txt"
ReportFile = objShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop\CreateFolderOutput.txt"

Const intForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutput = objFSO.OpenTextFile(ReportFile, intForAppending, False)

arrComputers  = Split(objFSO.OpenTextFile(InputFile).ReadAll,vbNewLine)
 
For Each PC in arrComputers
  
	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 & "\" & Replace(FolderName, ":", "$")
			If objFSO.FolderExists(DestinationPath) = False Then
				objFSO.CreateFolder(DestinationPath)
				ResolveIP = PC & " " & Foldername & " Created"
			Else
				ResolveIP = PC & " " & Foldername & " Already exists"
			End If
		End If
		ObjOutput.WriteLine(resolveip)
	next
Next

objOutput.Close

Wscript.echo "Job Done"

Open in new window

0
 

Author Comment

by:Bianchi928
ID: 35155182
Before I allocate the points, can you please explain this UNC path to me .Thanks
Cheers
0
 

Author Comment

by:Bianchi928
ID: 35161784
Rob,

Also, I wanted to ask you what change needs to be done if in the future I need to rename the shortcut.

Thanks
Cheers
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 65

Expert Comment

by:RobSampson
ID: 35162246
Hi,

When you reference a folder on a remote computer, as long as you have admin rights, you can reference a drive by its hidden share, ie C$.

To reference a folder on that computer, use \\<computername>\C$\Temp, whereas, if you tried to reference C:\Temp (which is what you originally did with CreateFolder), it refers to your local computer, hence why it exists all the time.

So instead of doing this, which refers to your local computer:
              If not objFSO.FolderExists(Foldername) then
                  objFSO.CreateFolder(FolderName)


we do this;
                  DestinationPath = "\\" & PC & "\" & Replace(FolderName, ":", "$")
                  If objFSO.FolderExists(DestinationPath) = False Then
                        objFSO.CreateFolder(DestinationPath)

Where the colon in FolderName (the C:\Temp reference) is replaced with a dollar sign (to become C$\Temp) and has \\<computername>\ prefixed to it, so it looks for
\\<computername>\C$\Temp

I hope that makes sense.

>> I wanted to ask you what change needs to be done if in the future I need to rename the shortcut.

This script doesn't create any shortcuts, but in your previous questions, we used:
Set objShortcut = objShell.CreateShortcut("\\" & strComputer & "\C$\Documents and Settings\" & strUser & "\Desktop\My_Documents.lnk")

to create the shortcut called My_Documents.lnk

To rename the shortcut that has that name, to something else, using this script, add
strShortcut = "\\" & strComputer & "\C$\Documents and Settings\" & strUser & "\Desktop\My_Documents.lnk"
strNewName = "\\" & strComputer & "\C$\Documents and Settings\" & strUser & "\Desktop\My_New_Name.lnk"
If objFSO.FileExists(strShortcut) = True Then objFSO.MoveFile strShortcut, strNewName

Open in new window


Regards,

Rob.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35162250
Hopefully that makes sense...
0
 

Author Comment

by:Bianchi928
ID: 35162484
The UNC explanation makes perfect sense . Thanks.

But, i'm not too sure about the renaming of the shorcut...I don't know where & how to include these 3 lines in the script 35153745
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35162510
Hmmm, there's no user's specified in this script, so you won't reference any specific user profiles....I guess we could iterate through C:\Documents and Settings and change them all?

Maybe it would be easiest to use those lines in the other script instead, from your other question.

Regards,

Rob.
0
 

Author Comment

by:Bianchi928
ID: 35162516
I got ti to work fine now Rob.

Again..thanks a lot

Cheers
0
 

Author Closing Comment

by:Bianchi928
ID: 35162518
Top
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

831 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