?
Solved

CReate folder on remote computer

Posted on 2011-03-16
9
Medium Priority
?
1,427 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
[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
  • 5
  • 4
9 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 1000 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
Industry Leaders: 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!

 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month10 days, 4 hours left to enroll

762 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