How to get %username% in VBScript? - windows

I am trying to hide network path of shared folders from domain users. (Windows Server 2012) I have found this script while searching for network drive labeling:
Option Explicit
Dim objNetwork, strDrive, objShell, objUNC
Dim strRemotePath, strDriveLetter, strNewName
strDriveLetter = "H:"
strRemotePath = "\\servername\sharedfoldername$\"
strNewName = "Save Your Files Here"
'Section to map the network drive
Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
'Section which actually (re)names the Mapped Drive
Set objShell = CreateObject("Shell.Application")
objShell.NameSpace(strDriveLetter).Self.Name = strNewName
WScript.Echo "Check : "& strDriveLetter & " for " & strNewName
WScript.Quit
My network path will be like below:
strRemotePath = "\\servername\sharedfoldername1$\%username%"
strRemotePath = "\\servername\sharedfoldername2$\%username%"
strRemotePath = "\\servername\sharedfoldername5$\%username%"
strRemotePath = "\\servername\sharedfoldernameNNN$\%username%"
When I insert %username%, the script does not run.
Kindly guide me how to modify this script that will run as per my requirements.

You can expand environment variables in your path string:
strRemotePath = "\\servername\sharedfoldername1$\%username%"
Set sh = CreateObject("WScript.Shell")
WScript.Echo sh.ExpandEnvironmentStrings(strRemotePath)
or you can build the path from the share and the UserName property of the WshNetwork that you already have:
share = "\\servername\sharedfoldername1$"
Set fso = CreateObject("Scripting.FileSystemObject")
WScript.Echo fso.BuildPath(share, objNetwork.UserName)

Related

Trying to code for the first time, but failed with file location

The problem is that I tried to make a file on somebody else's desktop but I don't know what to write in the users.
I tried "C:\Users\" & strUser & "\desktop\Test"
Set fso = CreateObject("Scripting.filesystemobject")
fso.Createfolder "C:\Users\" & strUser & "\desktop\Test"
error: Can't find the file location.
Use the special folder Shell function to return the path to the desktop.
set objShell = Wscript.CreateObject("Wscript.Shell")
strDesktopPath = objShell.SpecialFolders("Desktop")
Set fso = CreateObject("Scripting.filesystemobject")
fso.Createfolder strDesktopPath & "\Test"
I think your code will work if you simply add a \ at the end of your path:
Set fso = CreateObject("Scripting.filesystemobject")
fso.Createfolder "C:\Users\" & strUser & "\desktop\Test\"

Find Shortcut Path Vbs

I am looking for a VBS browse function which gets the shortcut path. Unfortunately all browse functions I tried got only the folder path, but not shortcut path. I can see the shortcut in the dialog, but cannot get the path of it.
Is there someone who has this kind of function?
strComputer = "."
Const ALL_OPTIONS = &H4000
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder (0, "Select a folder:",ALL_OPTIONS,(16))
If objFolder Is Nothing Then
Wscript.Quit
Else
Set objFolderItem = objFolder.Self
End If
objPath = objFolderItem.Path
This function gives a real folder path, but when I select a shortcut with it, it returns null.
How can I get the shortcut path with browse for folder function?
Use the .ShortPath property of the file or folder object:
Option Explicit
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim f
Set f = goFS.GetFolder(".") : WScript.Echo f.Path, f.ShortPath
Set f = goFS.GetFile(WScript.ScriptFullName) : WScript.Echo f.Path, f.ShortPath
output:
cscript //nologo "45388073-pi pa po.vbs"
C:\Users\eh\work C:\Users\eh\work
C:\Users\eh\work\45388073-pi pa po.vbs C:\Users\eh\work\453880~1.VBS
Update wrt comment:
given:
objPath = objFolderItem.Path ' it's a string not an object!
sShortPath, and goFS, do:
sShortPath = goFS.GetFolder(objPath).ShortPath

How to connect to Remote desktop to copy files

I am not able to copy files onto the remote machine. For manually copying files we log on to the remote desktop, go to that individual folder and then paste the files.
We connect to remote desktop by giving the information
Computer Name : targetserver.x.com
Username : xyz
password : xyz
I am able to copy files to my local machine
source folder = "\Sourceserver\Archive\folder1"
target folder = "C:\Users\TEMPPAHIR\LearnVB\folder1"
but not able to connect to the server for copying files onto it, like not able to copy from
source folder = "\Sourceserver\Archive\folder1"
target folder = "\Targetserver\F\folder1"
I have tried the below code
Option Explicit
'Get environment variables
Set objShell = CreateObject("WScript.Shell")
Set objSystemEnv = objShell.Environment("SYSTEM")
Set objNetwork = CreateObject("WScript.Network")
strEnvVarSinc = objSystemEnv("SINC_DATA")
strEnvVarPcData = objSystemEnv("PC_DATA")
strEnvVarArchive = objSystemEnv("ARCHIVE")
strComputerName = objNetwork.ComputerName
Set objSystemEnv = Nothing
Set objShell = Nothing
Set objNetwork = Nothing
'SINC_DATA_AREA
strSincPath = strEnvVarSinc & "\" & strArea
'PC_DATA_AREA
strPcDataPath = strEnvVarPcData & "\" & strArea
I am able to map the drive now finally on my local machine but getting an error while copying files on to it - error on (18,1) Microsoft VBScript runtime error: permission denied. Is it because of some firewall or some other issue ? I am manually able to connect the same RD by giving in my username and password and then copy files onto the RD.
Please help !
Option Explicit
Dim objNet, sFile, objFSO, Arg, var, strInfo, strComputer, strDomain, objSWbemLocator, objSWbemServices
Dim colSwbemObjectSet, strUser, strPassword, objNetwork, objProcess
Dim SourceFolder, TargetFolder, objWMIService, strMachine, strAltUsername, strAltPwd
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNet = CreateObject("WScript.NetWork")
set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive "T:" , "\\srv10219\F"
SourceFolder = "\\srv10218\Archive\BudgetPiecesStore\20151116_094107"
Wscript.echo "Source folder Name is : " & SourceFolder
TargetFolder = "\\srv10219\F\ICCdata\BudgetPiecesStore\"
Wscript.echo "Target folder Name is : " & TargetFolder
For Each sFile In objFSO.GetFolder(SourceFolder).Files
objFSO.GetFile(sFile).Copy TargetFolder & "\" & objFSO.GetFileName(sFile),True
WScript.Echo "Copying file : " & Chr(34) & objFSO.GetFileName(sFile) & Chr(34) & " to " & TargetFolder
Set objNet = Nothing
Next

VBScript Using Computername to Name a File

New to VBScript and having a problem grasping this concept.
This is the code:
Set WshNetwork = WScript.CreateObject("WScript.Network")
strCompName = WshNetwork.Computername
Wscript.Echo WshNetwork.Username >j:\strCompName.txt
WScript.Quit()
Basically I want to the username dumped to a text file and the text file should be named with the name of the computer. I've tried putting the strCompName in quotes, single quotes, parenthesis with no success.
Here is the code that you can use. You need to use FileSystemObject. The FileSystemObject is used to gain access to a computer's file system. It can create new files and access existing ones.
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strCompName = WshNetwork.Computername
'writing to file
outFile="c:\TEMP\" & strCompName & ".txt"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write WshNetwork.Username & vbCrLf
objFile.Close
Set objFile = Nothing
Set objFSO = Nothing
Set WshNetwork = Nothing
WScript.Quit()
Save this in .vbs file and run and you will get a text file with computer name in TEMP folder (Change the path if you like).
This code should work. This code opens the file and appends it if the file exists or creates a file and writed to it if it does not exist.
'constants
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
'Load domain, username, & computer variables
Set oShell = CreateObject( "WScript.Shell" )
sDomain = oShell.ExpandEnvironmentStrings( "%USERDOMAIN%" )
sUserName = oShell.ExpandEnvironmentStrings( "%USERNAME%" )
sComputer = oShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
'Setup filesystemobject
Set oFSO=CreateObject("Scripting.FileSystemObject")
'Check to see if file exists. If exists open it forAppending
'else create file and write to it.
outFile="c:\export\" & sComputer & ".txt"
If oFSO.FileExists(outFile) Then
Set objFile = oFSO.OpenTextFile(outFile, ForAppending, True, TristateTrue)
Else
Set objFile = oFSO.CreateTextFile(outFile,True)
End If
'write to file
objFile.WriteLine sDomain & "\" & sUsername & " - " & Now
'clean up objects
objFile.Close
Set objFile = Nothing
Set oFSO = Nothing
Set oShell = Nothing

VBscript , create directory in FTP

I would like to create a directory in FTP, the name of the directory must be as my computer name,
here is my code,
Dim FoldertoCreate, filesys, newfolder, Ob
Set Ob = Wscript.CreateObject("Wscript.Network" )
FoldertoCreate = "ftp://user:password#ftpserver/url-path/" & ob.ComputerName
Set filesys = CreateObject("Scripting.FileSystemObject")
If Not filesys.FolderExists(FoldertoCreate) Then
Set newfolder = filesys.CreateFolder(FoldertoCreate)
End If
This code doesn't work however when i replace ftp://user:password#ftpserver/url-path with any local directory like D:/ , it works :S
how to make it work for my ftp too
The FileSystemObject does not support FTP. The Shell Automation Object does, but it doesn't seem to like the NewFolder method. That leaves us with automating the FTP.exe command using an unattended FTP session. It might look something like this.
strUser = "myusername"
strPass = "mypassword"
strHost = "ftp.myhost.com"
Const ForWriting = 2
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile = objFso.OpenTextFile("session.txt", ForWriting, vbTrue)
With objFile
.WriteLine "OPEN " & strHost
.WriteLine "USER " & strUser
.WriteLine strPass
.WriteLine "mkdir sometestdirectory"
.Close
End With
strFTP = "%systemroot%\System32\ftp.exe -s:session.txt"
Set WshShell = CreateObject("WScript.Shell")
strFTP = WshShell.ExpandEnvironmentStrings(strFTP)
WshShell.Run strFTP,, vbTrue
objFso.DeleteFile "session.txt", vbTrue

Resources