pscp upload using vbs - vbscript

I am attempting to create a function in VBScript that uploads a text file to a remote server using the pscp command. When I run the function, I receive the error
80070002 - The system cannot find the file specified.
Below is the function, I was wondering if there was an issue with the pscp command or if there was an alternative way to upload a file in VBScript.
Function upload(source, dest)
WScript.Echo source
Set system = WScript.CreateObject("WScript.Shell")
system.Run "pscp -batch -pw [PASSWORD HERE] " & source & " [USERNAME HERE]:" & dest, 0, True
WScript.Echo "....Finished " & vbCrlf
End Function

Related

Self-elevating VBS from mapped network drive not working

Here is my sample test code:
On Error Resume Next
Dim localServiceKey : localServiceKey = CreateObject("WScript.Shell").RegRead("HKEY_USERS\s-1-5-19\")
'On error, elevate the script
If Err.Number <> 0 Then
CreateObject("Shell.Application").ShellExecute WScript.FullName, """" & WScript.ScriptFullName & """ /elevate", "", "runas", 1
Err.Clear
WScript.Quit()
End If
...INSERT CODE BELOW...
This self-elevating VBS script file works properly if I run it from a local drive or from a network drive accessed using UNC (ie: \\server\folder).
But if I run it from a mapped drive (ie: Z:\folder), I receive an error after the UAC prompt stating:
Can not find script file...
Is there a work around for allowing VBS script file to run from a mapped network drive?
NOTE: I'm testing this code on Windows 10 x64.
Thanks for any advice.

VB Window Services batch not executing

I am executing a batch file by using VB run method to connect ftp.
When I am executing VB code in debugging mode batch file is executing without any issue and ftp is successful. But, after building the exe file and tried to run it thorough windows service batch file is not executing.
command = Chr(34) & "FTPconnection.bat" & Chr(34) & " " & moReportFile
a.Run command, 0, True

How can I run the vbscript as the 'Administrator' but access on APPDATA folder of current logon user VBSCRIPT

I tried to access on the APPDATA folder, which does function with the following code perfectly (run as the current logon user):
Set objShellApp = CreateObject("Shell.Application")
Const ssfAPPDATA = &H1A
sAppDataPath = objShellApp.NameSpace(ssfAPPDATA).Self.Path
The result of sAppDataPath is: C:\Users\ Peter \AppData\Roaming
Now the problem is, I have to run the Script as the Administrator account. And if I run this script as the Administrator on Clients, than the result is allways following:
C:\Users\ Administrator \AppData\Roaming
How can I change that? I want, that he should take the Appdata folder path of the current logon user. In spite of running the script as the Administrator. And i have to run the script as Administrator because of some permission.
How can I realize that?
Cannot do this with a VBS alone.
You would need to create a batch file to pass this %APPDATA% environment variable into the script as Argument, then modify the VBS script to process this argument(s).
Sample below:
UPDATE:
If you do not mind people being able to open the batch file and know the password of the user account you intend to Run As, get psexec.exe from MS with the updated batch file contents (assuming psexec.exe stored in C:\SysInt\):
VBS File
Dim sTxt
sTxt = WScript.Arguments.Count & " arguments passed into vbs:"
sTxt = sTxt & JoinArgs
wscript.echo sTxt
Function JoinArgs()
Dim sTmp
sTmp = ""
For Each oArg In Wscript.Arguments
sTmp = sTmp & vbCrLf & oArg
Next
JoinArgs = sTmp
End Function
Batch File (UPDATED)
#echo off
C:\SysInt\psexec.exe -u %computername%\administrator -p AccountPassword -e wscript.exe "c:\debug\vbs\test.vbs" %appdata%

What is the equivalent of a Batch ECHO Command using VBScript?

We currently use Windows Batch (DOS) command files to control our process flow. To display messages to the Console, we would use the ECHO command. These messages would show up in our Scheduler software, which used to be Tivoli and now is CA WA Workstation\ ESP.
I would like to start using VBS files instead of CMD\BAT files and am trying to figure out how to do the equivalent of an ECHO to the console.
When I try to use either the WScript.Echo command or write to Standard Out, the messages are displayed in dialog boxes for both and they require the OK button to be pushed to continue. Not surprisingly, when I run unattended though a scheduler, the job hits one of these commands and just hangs since there is no one to OK the messagebox.
SET FS = CreateObject("Scripting.FileSystemObject")
SET StdOut = FS.GetStandardStream(1)
StdOut.Write("Test 1")
WScript.echo("Test 2")
I realize I could write the messages to a Log file using the Scripting object, but this could fail if an invalid path is provided or because of insufficient permissions. Besides, being able to see feedback write within the Scheduler is awfully convenient.
How do I write to the Console using VBScript? I’ve seen other posts here that suggest that the above methods which didn't work for the reason describe above were the way to do it.
wscript.echo is the correct command - but to output to console rather than dialogue you need to run the script with cscript instead of wscript.
You can resolve this by
running your script from command line like so:
cscript myscript.vbs
changing the default file association (or creating a new file extension and association for those scripts you want to run with cscript).
change the engine via the script host option (i.e. as per http://support.microsoft.com/kb/245254)
cscript //h:cscript //s
Or you can add a few lines to the start of your script to force it to switch "engine" from wscript to cscript - see http://www.robvanderwoude.com/vbstech_engine_force.php (copied below):
RunMeAsCScript
'do whatever you want; anything after the above line you can gaurentee you'll be in cscript
Sub RunMeAsCScript()
Dim strArgs, strCmd, strEngine, i, objDebug, wshShell
Set wshShell = CreateObject( "WScript.Shell" )
strEngine = UCase( Right( WScript.FullName, 12 ) )
If strEngine <> "\CSCRIPT.EXE" Then
' Recreate the list of command line arguments
strArgs = ""
If WScript.Arguments.Count > 0 Then
For i = 0 To WScript.Arguments.Count
strArgs = strArgs & " " & QuoteIt(WScript.Arguments(i))
Next
End If
' Create the complete command line to rerun this script in CSCRIPT
strCmd = "CSCRIPT.EXE //NoLogo """ & WScript.ScriptFullName & """" & strArgs
' Rerun the script in CSCRIPT
Set objDebug = wshShell.Exec( strCmd )
' Wait until the script exits
Do While objDebug.Status = 0
WScript.Sleep 100
Loop
' Exit with CSCRIPT's return code
WScript.Quit objDebug.ExitCode
End If
End Sub
'per Tomasz Gandor's comment, this will ensure parameters in quotes are covered:
function QuoteIt(strTemp)
if instr(strTemp," ") then
strTemp = """" & replace(strTemp,"""","""""") & """"
end if
QuoteIt = strTemp
end function

mstsc Remote Desktop questions

currently, to improve some inefficiencies on a daily process, I am trying to write a c# Winform app that will combine a mix of user-input with VBscripts that will expedite a previously all user-input process of looking at an excel file and moving files from VSS to certain folders of certain servers.
I was hoping to get some questions answered, pointed in the right way:
Using command line or other workaround instead of manually,
1) Is it possible to log into a 2003 remote desktop with a Smartcard/pin?
2) Is it possible to run a file/start a process on the remote desktop from a command on your machine?
Thanks for the help and time
I have only experience with the second question.
You can do this with remote scripting or with utilities like SysInternals PsExec http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx
here a vbscript that remotely starts a ipconfig command and redirects it to a textfile
Note that you can't start interactive processes like this, they would start but not show up
Dim sComputer 'computer name
Dim sCmdLine 'command line of the process
Dim sCurDir 'working directory of the process
Dim oProcess 'object representing the Win32_Process class
Dim oMethod 'object representing the Create method
sComputer = "." 'this is the local computer, use a pcname or ip-adress to do it remote
sCmdLine = "cmd /c ipconfig.exe > c:\ipconfig.txt"
Set oProcess = GetObject("winmgmts://" & sComputer & "/root/cimv2:Win32_Process")
Set oMethod = oProcess.Methods_("Create")
Set oInPar = oMethod.inParameters.SpawnInstance_()
oInPar.CommandLine = sCmdLine
oInPar.CurrentDirectory = sCurDir
Set oOutPar = oProcess.ExecMethod_("Create", oInPar)
If oOutPar.ReturnValue = 0 Then
WScript.Echo "Create process method completed successfully"
WScript.Echo "New Process ID is " & oOutPar.ProcessId
Else
WScript.Echo "Create process method failed"
End If

Resources