Solved

CReate folder on remote computer

Posted on 2011-03-16
9
1,330 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now