• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1479
  • Last Modified:

CReate folder on remote computer

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
Bianchi928
Asked:
Bianchi928
  • 5
  • 4
1 Solution
 
RobSampsonCommented:
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
 
Bianchi928Author Commented:
Before I allocate the points, can you please explain this UNC path to me .Thanks
Cheers
0
 
Bianchi928Author Commented:
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.

 
RobSampsonCommented:
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
 
RobSampsonCommented:
Hopefully that makes sense...
0
 
Bianchi928Author Commented:
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
 
RobSampsonCommented:
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
 
Bianchi928Author Commented:
I got ti to work fine now Rob.

Again..thanks a lot

Cheers
0
 
Bianchi928Author Commented:
Top
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now