[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

CReate folder on remote computer

Posted on 2011-03-16
9
Medium Priority
?
1,449 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

650 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