Add printer to system using Powershell within VBA? - vba

I can make it work with a batch file using:
powershell.exe "Add-printer -ConnectionName \\PS02.samba.net\HELPS006"
But if I try to run the bat file above with VBA, it does not add the printer:
Shell ("c:\Fraktsedlar\HELPS006.bat")
If I run powershell directly from VBA, none of these works either:
Shell ("powershell.exe -Command " & Chr(34) & "Add-printer -ConnectionName \\PS02.samba.net\HELPS006" & Chr(34))
Shell ("powershell.exe -Command {" & Chr(34) & "Add-printer -ConnectionName \\PS02.samba.net\HELPS006" & Chr(34) & "}")
Shell ("powershell.exe -Command " & Chr(34) & "{Add-printer -ConnectionName \\PS02.samba.net\HELPS006}" & Chr(34))
What is it I'm doing wrong?

If powershell can do it so VBA.
From Help
http://download.microsoft.com/download/winscript56/Install/5.6/W982KMeXP/EN-US/scrdoc56en.exe This contains some libraries for system admin. You can use all of Windows Scripting Host features except for the root wscript object (used by a script to communicate with the host). 1000 times as much admin/info can be used with WMI https://msdn.microsoft.com/en-us/library/windows/desktop/aa393262(v=vs.85).aspx.
WSH has a second add printer function. Look at that one.
Adds a Windows-based printer connection to your computer system.
object.AddWindowsPrinterConnection strPrinterPath
object
WshNetwork object.
strPrinterPath
String value indicating the path to the printer connection.
Example
The following code uses the AddWindowsPrinterConnection method to connect a network printer to a Windows NT/2000 computer system.
Set WshNetwork = CreateObject("WScript.Network")
PrinterPath = "\\printserv\DefaultPrinter"
WshNetwork.AddWindowsPrinterConnection PrinterPath

Try this:
Shell ("powershell.exe -Command { Add-Printer -ConnectionName \\PS02.samba.net\HELPS006 }")
Or this:
Shell ("powershell.exe -Command " & Chr(34) & "& { Add-Printer -ConnectionName \\PS02.samba.net\HELPS006 }" & Chr(34))
You can also try this which is what Add-Printer does:
$p = Get-WmiObject -List Win32_Printer -EnableAllPrivileges
$p.AddPrinterConnection ("\\PS02.samba.net\HELPS006")

Related

Executing a bat. file with arguments from vba code

I'm trying to execute a bat. file from vba but can't get it working (after viewing some of the threads from the subject).
here is the code now:
Dim filet As String
Dim numero As String
Const pekka = "TIEDOSTO"
Const sami = "NUMEROT"
filet = Range(pekka).Cells(1, 1).value
numero = Range(sami).Cells(1, 1).value
commandstring = "D:"
commandstring2 = "cd folder name"
commandstring3 = "Create-tri.bat" + " " + filet + " " + numero
Call Shell("cmd.exe /S" & commandstring & commandstring2 & commandstring3, vbNormalFocus)
So according to my logic, this code should first access D, then the wanted folder and then execute the bat. file with the given parameters (filet and numero). What am I doing wrong here?
Best regards and thanks in advance,
Johannes
Try to remove the Call, it is depreciated in VBA.
Then use Shell "cmd.exe /S" & commandstring & commandstring2 & commandstring3.
To see what you are actually trying to execute, use:
Debug.Print "cmd.exe /S" & commandstring & commandstring2 & commandstring3
What do you get? Most probably it is not an executable command, ending on .bat Some reference here - Execute .bat file from Excel VBA Macro

Vba to call a powershell script or .bat file

I have a line to call a powershell script and it works but the powershell windows gets closed without it working.
the code is, any ideas?
Additionally I would like it to be able to run .bat files (I tried Shell ( & pathcrnt & "\GUI-getuserpropertiesV2.10.ps1" & pathcrnt) bit I get the same error.
Sub CallBatch()
Dim pathcrnt As String
pathcrnt = ActiveWorkbook.Path
Shell ("PowerShell " & pathcrnt & "\GUI-getuserpropertiesV2.10.ps1" & pathcrnt)
End Sub
Please see the link below and make sure you have permissions for the machine.
http://www.tek-tips.com/viewthread.cfm?qid=1585510
I don't know about your setup or security, so if you encounter an error, search for 'PS permissions', or something along those lines. The VBA script will definitely work!
If you insert a space after .ps1 it should work:
Sub CallBatch()
Dim pathcrnt As String
pathcrnt = ActiveWorkbook.Path
Shell "PowerShell " & pathcrnt & "\GUI-getuserpropertiesV2.10.ps1 " & pathcrnt
End Sub
Imagine the resulting string; for pathcrnt C:\Windows it would be this: "PowerShell C:\Windows\GUI-getuserpropertiesV2.10.ps1C:\Windows"
You also might want to enclose the path elements with double quotes to prevent spaces messing with your parameters, like this:
Shell "PowerShell """ & pathcrnt & "\GUI-getuserpropertiesV2.10.ps1"" """ & pathcrnt & """"
Since double quotes also are string delimiters in VBA you have to escape them with another double quote.

File doesn't zip using 7zip command in vb.net

I am trying to zip a file in vb.net. I am using 7zip to do this. I am using the Process.Start method.
Here is the zip line of my code:
Process.Start("C:\Program Files\7-Zip\7z.exe", "a -tzip" + (ChosenFile & "\" & "SavedFiles") + NewFileName1)
No error that I know of happens, however when I look through the path, I cannot find the zipped files.
ChosenFile & "\" & "SavedFiles" is the destination folder.
NewFileName1 is the file to be zipped
you must do a space after "-tzip" and u forgot use " to folders with spaces like that:
Process.Start("C:\Program Files\7-Zip\7z.exe", "a -tzip " & (ControlChars.Quote & ChosenFile & "\" & "SavedFiles" & ControlChars.Quote) & " " & ControlChars.Quote & NewFileName1 & ControlChars.Quote &)
Probably its obvious, but you have the rights to access those files with the user who runs the script?
Else, can you run the script manually step by step to debug where it fails?

downloading file using SFTP with VBA

My objective is to download, not upload a file from an SFTP server, and I am trying to adapt the code from another question on this site to do so (I pasted the code below for your convenience).
I downloaded PSFTP from Putty. PSFTP closes when I try to connect using the following command line:
open username:password#server.com.port:1111
I have three questions:
Is something wrong with my command line? If not then what could be the problem?
As far as I know SFTP would normally utilize get/put commands, but i don't see a put command in the code below, so I don't understand where I should enter the get command to download the file instead of uploading it (which is what the code below is supposed to be doing).
Is it correct that pRemotePath is the location of the file on the SFTP server, and pFile is the location I want the file downloaded to?
A simple explanation would be very much appreciated.
Public Sub SftpGet()
Const cstrSftp As String = """C:\Users\Ron\UtilityTools\psftp.exe"""
Dim strCommand As String
Dim pUser As String
Dim pPass As String
Dim pHost As String
Dim pFile As String
Dim pRemotePath As String
pUser = "uid"
pPass = "PW"
pHost = "dns"
pFile = "C:\Users\Ron\activity.txt"
pRemotePath = "Z:/activity.log"
strCommand = cstrSftp & " -sftp -l " & pUser & " -pw " & pPass & _
" " & pFile & " " & pHost & ":" & pRemotePath
Debug.Print strCommand
Shell strCommand, 1 ' vbNormalFocus '
End Sub
I think you should start with a Windows command prompt session. Work out the details of your command line there, as I suggested in an answer to a similar question: SFTP upload with VBA. Once you have a command line which works there, it will be very easy to execute that same command from VBA.
I've never used Putty's psftp.exe tool, only pscp.exe, so I can't offer help about how to construct your psftp.exe command line. One thing I noticed in Putty's documentation is that PSFTP (pscp.exe) can only work with a SSH-2 server --- if your target server supports only SSH-1, PSFTP will not work.
I think it would be worthwhile for you to review the Putty documentation at that link.

Shell Command Gives Error "error after : and."

I used :
Shell("cmd /k type " & System.IO.Path.GetTempPath & "file.exe > " & Application.ExecutablePath & ":file.exe")
Temp Folder : C:\Documents and Settings\Admin\Local Settings\Temp\
Command Prompt Window gives error : File not found, error after : and. System can't find destination path.
I think the problem is in this name : Documents and Settings
What should I do to bring it to life?
P.S : File exists, it works when I use : start command.
You need extra wrapping quotes.
Shell("cmd /k type """ & System.IO.Path.GetTempPath & "file.exe"" > " & Application.ExecutablePath & ":file.exe")
Spaces are delimiters in command line parameters. If you have a space in a single parameter, you need to wrap the entire parameter in quotes.
you probably need to put your path between "". something like
Shell("cmd /k type \"" & System.IO.Path.GetTempPath & "file.exe\" > " & Application.ExecutablePath & ":file.exe")