Trying to shutdown computer remotely in vb.net - vb.net

Helps master's, here is my coded when i try to shutdown remotely
Process.Start("shutdown", "-s -m \\COMPUTER NAME")
Then when i executed the program nothing happens..
I think my coded is not correct or i am missing something.

The shutdown command can fail for a number of reasons but you're not checking for success. Try this instead and then lookup the error.
Dim proc = Process.Start("shutdown", "/s /m \\COMPUTER_NAME")
proc.WaitForExit()
If proc.ExitCode <> 0 Then
MsgBox("Failed - Code = " & proc.ExitCode)
Else
MsgBox("Success")
End If
Note: you may need to run your application as an elevated process.
Before testing your application you should test that the command works from the command prompt.
Open cmd
Run shutdown /s /m \\COMPUTER_NAME
Check the output and make sure it worked. I suspect you'll get an access denied error. If so the right click on cmd and choose Launch as administrator. Then repeat this and make sure it works.
If this doesn't work then your program wont either. Google shutdown access denied and work through some of the trouble shooting tips.

Related

Apple Script / Vagrant

I'm new to apple script, but I'd like to open up Terminal, change the path to where the vagrant up command can be ran and then run a vagrant up. So far I have got to;
tell application "Terminal"
activate
do script "cd desktop/development/vagrant/"
end tell
If I try the following;
tell application "Terminal"
activate
do script "cd desktop/development/vagrant/"
do script "vagrant up"
end tell
This opens up a second terminal and tries to execute the vagrant up from the home location instead of running the command in the terminal window that navigates to desktop/development/vagrant/
My question is; is there a way to either run a follow up command in the original (first) Terminal window or a way to run the cd desktop/development/vagrant vagrant up as one singular command?
Thanks in advance
Found the answer;
tell application "Terminal"
set currentTab to do script ("cd desktop/development/vagrant/")
do script ("vagrant up") in currentTab
end tell

cmd.exe will not recognize command

I have a vb.net application that runs cmd.exe as the following:
Dim Port1 = CreateObject("WScript.Shell")
Port1.Run("cmd.exe")
Port1.Sendkeys("telnet ip address")
Port1.Sendkeys("{Enter}")
System.Threading.Thread.Sleep(6000)
When the cmd runs the sendkeys a error shows
"'telnet' is not recognized as an internal or external command,
operable program or batch file."
I closed the cmd window, opened another cmd, and typed in the command with not problems.
I need cmd to run in the vb.net and not as a separate file.
Running the vb.net in Microsoft visual studio Ultimate 2013.
Update: Can anyone tell me why it is doing the same thing when i move the app to another computer and try to run it?
You have to enable telnet on the computer by going to start > control panel > programs and features > turn windows feature on or off > check telnet client or typing optionalfeatures in run and checking telnet client. The command should execute successfully after that.
Either Telnet is not installed on your computer (it's not installed by default on any modern Windows versions as far as I know), or it its installation directory not in the PATH environment variable.
Either add it's install directory to PATH, or specify the full path to telnet.exe in the arguments to cmd.exe.
CMD runs commands based off of System32 and .bat(batch) or .exe. You would have to create a .exe or a .bat and place it into System32 in order for the command to work.

vb.net console application crashes

i have created a very simple console application in vb.net that emails me some info from the db daily. everythign works in my dev environment, however when i move it out to production and try to run the executable, i get the following error message:
"A problem caused the program to stop working correctly. please close the program"
i have put in some exception checks in the code:
Dim output As String
Try
output = FormatIndividualRecords(False)
'SaveToFile(output, "NIndividualRecords.doc")
''email files
'Emailusers()
Catch e As Exception
WriteLine(e.Message)
End Try
and i expected the error to show up on the application console, but it does not, is there another way for me to see what error is being thrown? there is no development environment set up in production (no visual studio, etc), is there maaybe a log that that i can look at? how do i catch the error that's causing the app to fail?
Thank you in advance!
EDIT: i chagned the WriteLine(e.Message) to console.write(e.message) now there appealrs to be a message on the console when i run the application, but it blinks too fast and closes, i don't have time to see what it is... how can i get the console application to remain open?
Try directing output of the app to a file, rather than to the console...
MyProgram.exe > log.txt

How to run a PHP script via SSH and keep it running after I quit

I'm trying to restart a custom IRC bot. I tried various commands :
load.php
daemon load.php
daemon load.php &&
But that makes the script execute inside the console (I see all the output) and when I quit the bot quits as well.
The bot author only taught me the IRC commands so I'm a bit lost.
You can install a package called screen. Then, run screen -dm php load.php and resume with screen -dR
This will allow you to run the script in the background, and still be able to use your current SSH terminal. You can also logout and the process will still be running.
Chances are good the shell is sending the HUP signal to all its running children when you log out to indicate that "the line has been hung up" (a plain old telephone system modem reference to a line being "hung up" when disconnected. You know, because you "hang" the handset on the hook...)
The HUP signal will ask all programs to die conveniently.
Try this:
nohup load.php &
The nohup asks for the next program executed to ignore the HUP signal. See signal(7) and the nohup(1) manpages for details. The & asks the shell to execute the program in the background.
Clay's answer of using screen(1) is pretty awesome, definitely look into screen(1) or tmux(1), but I don't think that they are necessary for this problem.
This line might help you
php load.php &

applescript utility problems/bash/ process detection

I made a small automator app to help me launch and end apache ( another for mysql), so that I don't have to go to the terminal and do it ( yes I'm that lazy).
now here is the applescript I use for it:
on run {input, parameters}
set apache2state to (do shell script "/bin/ps -arx |/usr/bin/grep apache2 |wc -l")
repeat until apache2state does not start with " "
set apache2state to text 2 thru -1 of apache2state
end repeat
if apache2state is equal to "3" then
do shell script "/opt/local/bin/port load apache2" with administrator privileges
else
do shell script "/opt/local/bin/port unload apache2" with administrator privileges
end if
end run
Now this works, except for the fact that I'm actually comparing to integer values that are relative to the number of processes that apache is running.. so it's not really reliable.
Is there a better way to test if apache (and mysql, I have another script just the same) is running. Problem is that when they are shut off the shell command will return an integer value just the same ( and not just a 1 for the grep process..)
thanks
EDITED to specify thatthe solution up here worked badly because the grep would return any instance of the apache threads and any other process that said apache ( like tail -r /var/log/apache2/error.log for instance).
The running state of a service can be tested with the following command:
/sbin/service --test-if-configured-on "org.apache.httpd"
This however might not work for services installed with MacPorts.
Also see the answer to the this question.
Since the command service is deprecated under Snow Leopard, you can alternatively use launchctl to obtain the running state of a service. The command can be invoked from AppleScript in the following way:
try
do shell script "/bin/launchctl list | grep -q org.apache.httpd" with administrator privileges
set apache2Running to true
on error
set apache2Running to false
end try