SQLPLUS BAT File - batch-file

I have a BAT file that runs a script on oracle :
sqlplus myuser/mypassword#mydatabase #C:\runthisfile.sql
I want to distribute this to other users (that don't necessarily know how to modify a BAT file).
I want the dos prompt to ask the user to enter their user and password (obviously I don't want to give them my connection details). Have tried all types of combination but all that happens is that I end up with SQL>......
Am stumped!

You can use the SET command with the /P argument in order to prompt the user for text during a batch file run, for example:
SET /P variable=Please enter text
This will then fill variable with whatever they type before hitting return.
#ECHO OFF
SET /P uname=Username:
SET /P pass=password:
This is a simple program which will prompt the first for a username, then a password.
You should then be able to pass this as an argument to sqlplus:
sqlplus %uname%/%pass%#mydatabase #C:\runthisfile.sql

Regarding with SQLPlus stop, doing nothing:
Sometimes SQLPlus finish with ... meaning that is waiting for something more.
Try to add "/" (without quotes) in the end of your SQL file to execute it.
I hope it will help...

It is the very simple code for opening SQLPLUS without entering usename and password manually.
sqlplus -L UserName/Password
For example : sqlplus -L Rak4ak#sun64/rk4
For Understanding :
sqlplus [ [] [{logon | /nolog}] [] ]
is: [-C ] [-L] [-M ""] [-NOLOGINTIME] [-R ]
[-S]
-C <version> Sets the compatibility of affected commands to the
version specified by <version>. The version has
the form "x.y[.z]". For example, -C 10.2.0
-L Attempts to log on just once, instead of
reprompting on error.
-M "<options>" Sets automatic HTML markup of output. The options
have the form:
HTML [ON|OFF] [HEAD text] [BODY text] [TABLE text]
[ENTMAP {ON|OFF}] [SPOOL {ON|OFF}] [PRE[FORMAT] {ON|OFF}]
-NOLOGINTIME Don't display Last Successful Login Time.
-R <level> Sets restricted mode to disable SQL*Plus commands
that interact with the file system. The level can
be 1, 2 or 3. The most restrictive is -R 3 which
disables all user commands interacting with the
file system.
-S Sets silent mode which suppresses the display of
the SQL*Plus banner, prompts, and echoing of
commands.
is: {[/][#] | / }
[AS {SYSDBA | SYSOPER | SYSASM | SYSBACKUP | SYSDG | SYSKM}] [EDITION=value]
Specifies the database account username, password and connect
identifier for the database connection. Without a connect
identifier, SQL*Plus connects to the default database.
The AS SYSDBA, AS SYSOPER, AS SYSASM, AS SYSBACKUP, AS SYSDG,
and AS SYSKM options are database administration privileges.

Related

How to do batch redirection for commands with options

I'm trying to run a series of commands that requires the user's password, so I'm asking for the password at the beginning of the script and redirect to the command using | (pipe redirection).
In the documentation I found the syntax:
set password=secretpassword
echo %password% | command that prompt for password
I tried with the scp command to copy one file from the server with the following code:
echo %password% | scp myuser#serverip:/home/myuser/myfile ./myfile
but didn't work.
But this code works, for some reason:
echo %password | %scp myuser#serverip:/home/myuser/myfile ./myfile
I didn't understand why, maybe the interpretation of pipe inside %% is different.
The main problem is when I pass options to the command, like to get a folder recursive, it doesn't work. Example:
echo %password | %scp -r myuser#serverip:/home/myuser/myfolder ./myfolder
I know that there are other better methods like using key pair authentication instead of a password, but I want to understand why it's not working.

Oracle sqlplus > substitution a prompt variable

I have a *.sql script file and there are some PROMPT commands in this file which force user to type in something.
I would like to execute this script file with sqlplus and supress the prompt question somehow.
Is there any way to supress the question and substitute its value with a pre-defined variable?
This is my test code:
set define on
set define $
SET VERIFY OFF
SET HEADING OFF
DEFINE semaowner = "hello" (CHAR);
accept semaowner prompt "schema owner: "
select '$semaowner' semaowner from dual;
quit;
And the way how I execute it:
sqlplus sys/ora123#host:port/schema as sysdba #prompt-demo.sql
But it does not work because the prompt appears either the DEFINE command is applied or not.
You could supply the response to the prompt from the command line, wrapped in a shell script/batch file if necessary:
echo schema_name | sqlplus sys/ora123#host:port/schema as sysdba #prompt-demo.sql
That works on Windows and Linux.
So you can do that in dev with (presumably) a fixed known value; and in prod just run it as you were before and have to manually enter the value.
You will still see the prompt text in dev, but it won't stop and wait for input.
If you have multiple prompts you could use multiple echos:
(echo schema_name && echo something_else) | sqlplus ...
which also works in both; in Linux you could also use a single print statement with embedded newlines:
print "schema_name\nsomething_else\n" | sqlplus ...
or a heredoc:
sqlplus sys/ora123#host:port/schema as sysdba #prompt-demo.sql <<!EOF
schema_name
something_else
!EOF
but that doesn't help you on your Windows dev box. (There may be a heredoc equivalent on Windows but I think it's basically rearranging the echos...)

How to pass a variable value through a batch file to an sql script from the command line

I've been trying to pass a variable value that i can enter in the command line to open a batch file and then pass that value to an sql script. In this instance I'm looking to change a database user's password to a new password by setting it in the command line.
command line - update_passwords.bat server_name 1234
batch file -
#echo off
set sql_server_name=%1
set newPass=%2
osql -S %1 -U dbuser -P user_pass -v newPass=%2 -i c:\sql_script -o c:\sql_log
sql file -
ALTER LOGIN user1 WITH PASSWORD = %2;
I get an incorrect syntax near '%' error when I run the the batch file
Any help is greatly appreciated
There are two main issues. One is that %2 won't be available in c:\sql_script, so the SQL statement is trying to set the password to "%2", literally. You could resolve that by passing the SQL statement on the command line instead of in an input file. The second issue is that the SQL statement needs quotes around the %2. Try the following.
osql -S %1 -U dbuser -P user_pass -q "ALTER LOGIN user1 WITH PASSWORD = '%2';" -o c:\sql_log
Now, if the new password has apostrophes in it, you would need to escape them by converting them to double apostropes.
Given that you're not using the sql_server_name and newPass variables after setting them, you can remove them completely. This will remove the security issue mentioned by shawnt00.

Launch PL/SQL script from batch file (with arguments)

I'm strugling at passing arguments to a PL/SQL script from a Windows .bat file.
Here is the content of the batch file :
#echo off
set mName = "test"
exit | sqlplus <connection_string> #test.sql %mName%
pause
And here is the content of test.sql :
set verify off
set serveroutput on size unlimited
BEGIN
DBMS_OUTPUT.PUT_LINE('&&1');
END;
/
I would expect to see "test" appearing in the shell but instead, I get the following message :
Enter value for 1:
SP2-0546: User requested Interrupt or EOF detected.
Does anyone have a solution to this problem ?
Remove the spaces around your = sign. Here's my working batch file:
#echo off
set mName="test"
exit | sqlplus <connection_string> #test.sql %mName%
pause
Also note that you can use the -L option on sqlplus for batch jobs:
-L Attempts to log on just once, instead of reprompting on error.

Net User in PowerShell

I am in the middle of moving to the cloud, migrating from SBS 2003 Active Directory, to 2008 R2.
I configured a new user, and noticed that the user was unable to reset their password.
My Server Admin showed me how to use net user.
I noticed that I can obtain information from some accounts and not others. With over 100 accounts to process, I thought I'd try PowerShell.
In this post (Use powershell to look up 'net user' on other domains?) Lorenzo recommends using Get-ADUser (and this applies to polling from another domain). When I run Get-ADUser from my PowerShell prompt, I receive a message stating that the commandlet is not recognized.
I am reading the user IDs from a text file, and sending the output to a log file so that I can send to the server admin for further analysis.
Here is my code so far (please note that I am completely new to PowerShell):
# Get our list of user names from the local staff.txt file
$users = get-content 'C:\Scripts\staff.txt'
# Create log file of output:
$LogTime = Get-Date -Format 'MM-dd-yyyy_hh-mm-ss'
$CompPath = "C:\Scripts\"
$CompLog = $CompPath + "NetUserInfo" + $LogTime + ".txt"
New-Item -path $CompLog -type File
foreach ($user in $users) {
#Testing user:
"Testing user: $user" | out-file $CompLog -Append
# Obtain user information using net user:
net user $user /domain >> $CompLog
# Pause to let system gather information:
Start-Sleep -Second 15
}
As the script runs currently, my log file will have two or three user names followed by the response "The request will be processed at a domain controller for domain (domain)"
If net user, from CMD, would return "System error 5 has occurred, Access is denied." This is not logged in the output file. IF net user, from CMD, would return user information, this is logged to the output file. I am currently receiving output for only a couple users, but when I run the command from CMD, I am able to retrieve information for at least ten.
My first thought was that I needed to wait for the net user command to complete (hence the Start-Sleep command) but that has not had any effect on the output.
Any assistance would be greatly appreciated.
Sorry, I don't have enough reputation to add a comment.
When you run programs in Powershell (such as net user... or ping) it should be running exactly the same as it would in the normal command prompt (cmd).
If I understand correctly you're getting different results when you (effectively) run the same thing in Powershell or Command Prompt. Is that right?
Are you using the ISE to build your script? If so you can set Breakpoints that will pause the script and allow you to see what the variables are. It could be that the $user variable isn't holding what you think it should be, or that the value doesn't match the name for a domain user account.
EDIT:
What happens when you run the net user ... command interactively (e.g. not as a script, but manually) in Powershell? Do you get an error? If so, what does the error say?
Additional related stuff:
You shouldn't need the Start-Sleep as the commands are run in order, and the next line shouldn't execute until the previous on has completed.
Also, which version of Powershell are you using?
(You can check by running $host.version.Major)
The Get-ADUser cmdlet requires version 3 (I believe) and also needs to import the Active Directory module.
The reason the error output is not being appended to the log file is because you are only redirecting the STDOUT (standard output stream). To also redirect the STDERR (standard error stream) change
net user $user /domain >> $CompLog
to
net user $user /domain 2>&1 $CompLog
This explains it a bit more:
http://www.techotopia.com/index.php/Windows_PowerShell_1.0_Pipes_and_Redirection#Windows_PowerShell_Redirection_Operators

Resources