有什么解决方案可以使用Powershell在Oracle中向导创建表空间和用户?

时间:2019-04-08 06:37:10

标签: oracle powershell cmdlets

我想使用powershell在oracle中自动配置表空间和用户。

我想创建一个powershell文件并只运行它,然后在sqlplus询问我的凭据,表空间和用户并自动授予oracle中的权限后。 我尝试了下面的代码,但是我的问题是当连接到sqlplus时,powershell中的其他代码未运行,因为区域将更改为sqlplus。

set oracle_sid = pargar;

sqlplus


CREATE TABLESPACE Pargar DATAFILE 
  'C:\app\Administrator\oradata\pargar\Pargar.DBF' SIZE 100M AUTOEXTEND ON NEXT 10M MAXSIZE 10240M
LOGGING
PERMANENT
EXTENT MANAGEMENT LOCAL AUTOALLOCATE
BLOCKSIZE 16K
SEGMENT SPACE MANAGEMENT AUTO
FLASHBACK ON;

CREATE TABLESPACE Pargar_Lob DATAFILE 
  'C:\app\Administrator\oradata\pargar\Pargar_Lob.DBF' SIZE 100M AUTOEXTEND ON NEXT 10M MAXSIZE 10240M
LOGGING
PERMANENT
EXTENT MANAGEMENT LOCAL AUTOALLOCATE
BLOCKSIZE 16K
SEGMENT SPACE MANAGEMENT AUTO
FLASHBACK ON;


CREATE USER PARGAR
  IDENTIFIED BY pargar
  DEFAULT TABLESPACE PARGAR
  TEMPORARY TABLESPACE TEMP
  PROFILE DEFAULT
  ACCOUNT UNLOCK;

  GRANT CONNECT TO PARGAR;
  ALTER USER PARGAR DEFAULT ROLE NONE;

  GRANT CREATE ANY VIEW TO PARGAR;
  GRANT CREATE ANY INDEX TO PARGAR;
  GRANT CREATE ANY TABLE TO PARGAR;
  GRANT CREATE SESSION TO PARGAR;

  ALTER USER PARGAR QUOTA UNLIMITED ON PARGAR;
  ALTER USER PARGAR QUOTA UNLIMITED ON PARGAR_LOB ;

exit ;

1 个答案:

答案 0 :(得分:0)

C:\upwork\stackoverflow\create_user>powershell .\create_user.ps1  scott2 tiger2
NAME_USER: scott2
PASSWORD_USER: tiger2
USER default_tablespace: tablespace_scott2
User created successfully!

create_user.ps1

param(
[string]$name_user = "user1"
, 
[string]$password_user = "password1"
)
Write-Host "NAME_USER: $name_user"
Write-Host "PASSWORD_USER: $password_user"
# login DBA user
$username_dba = "system"
# PASSWORD DBA user !!!!
$password_dba = "manager"
#
#$ORACLE_ERROR = '0'
#
#select value from v$parameter
#where name='os_authent_prefix';
#os_authent_prefix=OPS$ 
#$os_authent_prefix="OPS$"
$os_authent_prefix=""
#

$tnsalias_db = "test-ecdu"

# Connect without alias 
#$tnsalias_db = "(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = xxx.xx.yyy.zz)(PORT = 1521)))(CONNECT_DATA = (SID = test)))"
#
#Parameter Usage
#username The name of the schema, and therefore the user, to be created. The username can be up to 30 characters
#long and cannot be a reserved word unless it is quoted
#(which is not recommended).
#IDENTIFIED  BY password 
#Specifies how the user will be authenticated: by the database with a password
# DEFAULT TABLESPACE tablespace #The tablespace where permanent objects are created, unless a tablespace is explicitly specified during creation.
#TEMPORARY TABLESPACE tablespace #The tablespace where temporary segments are created
#during sort operations, index creation, and so forth.
#QUOTA { size | UNLIMITED } ON tablespace
#The amount of space allowed for objects created on
#the specified tablespace. Size is in kilobytes (K) or megabytes (M).
#PROFILE profile The profile assigned to this user. If a profile is not specified, the DEFAULT profile is used.
#PASSWORD EXPIRE At first logon, the user must change their password.
#ACCOUNT {LOCK | UNLOCK} Specifies whether the account is locked or unlocked.
#By default, the account is unlocked.# 


##TEMPORARY TABLESPACE tablespace #The tablespace where temporary segments are created
$temp_tablespace = "TEMP"
#Direcrory for datafile users tablespace 
#$user_datafile_directory = "'C:\app\Administrator\oradata\pargar\"
$user_datafile_directory = "'/oracle/oradata/oradata/test/"
#for windows only
#$user_datafile_directory = "'C:\oracle\oradata\oradata\test\"
#Full path for  datafile users tablespace 
$user_datafile_name  = $user_datafile_directory+"student_"+$name_user+".dbf'"
$user_lob_datafile_name  = $user_datafile_directory+"student_"+$name_user+"_lob.dbf'"
# Initial size of datafile
$size_datafile = "100M"
# Max size of datafile
$maxsize_datafile = "10240M"
#Mask for name of users DEFAULT TABLESPACE
$user_tablespace = "tablespace_"
$user_lob_tablespace = "tablespace_lob_"
#PROFILE profile The profile assigned to this user. If a profile is not specified, the DEFAULT profile is used.
$profile_user = "DEFAULT"
# Role connect and Resource need user for make connect and create tables,
#ROLE       System privilege
#_______________________________
#CONNECT    CREATE VIEW
#CONNECT    CREATE TABLE
#CONNECT    ALTER SESSION
#CONNECT    CREATE CLUSTER
#CONNECT    CREATE SESSION
#CONNECT    CREATE SYNONYM
#CONNECT    CREATE SEQUENCE
#CONNECT    CREATE DATABASE LINK
#
#RESOURCE   CREATE TYPE
#RESOURCE   CREATE TABLE
#RESOURCE   CREATE CLUSTER
#RESOURCE   CREATE TRIGGER
#RESOURCE   CREATE OPERATOR
#RESOURCE   CREATE SEQUENCE
#RESOURCE   CREATE INDEXTYPE
#RESOURCE   CREATE PROCEDURE 


$role = " connect, resource "
# full name of users tablespace    exaple  tablespace_stud_test1
$default_tablespace_user = $user_tablespace+$name_user
$default_lob_tablespace_user = $user_lob_tablespace+$name_user


Write-Host "USER default_tablespace: $default_tablespace_user"

#SQL for CREATE TABLESPACE
#Example 
#CREATE TABLESPACE  tablespace_stud_test1  DATAFILE '/oracle/oradata/oradata/test/student_stud_test1.dbf'
#SIZE 10M  REUSE AUTOEXTEND ON NEXT  1024K MAXSIZE 250M  EXTENT MANAGEMENT LOCAL SEGMENT SPACE
#MANAGEMENT  AUTO
#;
#
$sqlQuery_create_tablespace = @"
set NewPage none
set heading off
set feedback off
set timing off
WHENEVER SQLERROR EXIT SQL.SQLCODE
CREATE TABLESPACE  $default_tablespace_user  DATAFILE $user_datafile_name
SIZE $size_datafile  REUSE AUTOEXTEND ON NEXT  1024K MAXSIZE $maxsize_datafile  EXTENT MANAGEMENT LOCAL SEGMENT SPACE 
MANAGEMENT  AUTO 
;

"@

$sqlQuery_create_tablespace_lob = @"
set NewPage none
set heading off
set feedback off
set timing off
WHENEVER SQLERROR EXIT SQL.SQLCODE
CREATE TABLESPACE  $default_lob_tablespace_user  DATAFILE $user_lob_datafile_name
SIZE $size_datafile  REUSE AUTOEXTEND ON NEXT  1024K MAXSIZE $maxsize_datafile  EXTENT MANAGEMENT LOCAL SEGMENT SPACE 
MANAGEMENT  AUTO 
;

"@


# Degug display SQL Tablespace: $sqlQuery_create_tablespace"
#Write-Host "SQL Tablespace: $sqlQuery_create_tablespace"
#
#full_user_name 
$full_name_user=$os_authent_prefix+$name_user
$full_name_user_owner=$full_name_user+"."
$full_name_user_owner_bonus=$full_name_user_owner+"bonus"
$full_name_user_owner_dept=$full_name_user_owner+"dept"
$full_name_user_owner_emp=$full_name_user_owner+"emp"
$full_name_user_owner_emp_pk_emp=$full_name_user_owner+"pk_emp"
$full_name_user_owner_fk_deptno=$full_name_user_owner+"fk_deptno"
$full_name_user_owner_salgrade=$full_name_user_owner+"salgrade"

$sqlQuery_create_user = 
@"
CREATE USER $full_name_user IDENTIFIED BY $password_user
PROFILE $profile_user
DEFAULT TABLESPACE $default_tablespace_user
TEMPORARY TABLESPACE $temp_tablespace                 
QUOTA UNLIMITED ON  $default_tablespace_user
QUOTA UNLIMITED ON  $default_lob_tablespace_user
ACCOUNT UNLOCK 
;
--grant $role  to $name_user 
GRANT CREATE VIEW TO $full_name_user
;
GRANT CREATE TABLE TO $full_name_user
;
GRANT ALTER SESSION TO $full_name_user 
;
GRANT CREATE CLUSTER TO $full_name_user 
;
GRANT CREATE SESSION TO $full_name_user 
;
GRANT CREATE SYNONYM TO $full_name_user 
;
GRANT CREATE SEQUENCE TO $full_name_user 
;
GRANT CREATE DATABASE LINK TO $full_name_user 
;
GRANT CREATE INDEXTYPE TO $full_name_user 
;
GRANT CREATE OPERATOR TO $full_name_user 
;
GRANT CREATE PROCEDURE TO $full_name_user 
;
GRANT CREATE TRIGGER TO $full_name_user 
;
GRANT CREATE TYPE TO $full_name_user 
;
"@

$sqlQuery_create_table=@"

CREATE TABLE $full_name_user_owner_bonus
    (ename                          VARCHAR2(10 BYTE),
    job                            VARCHAR2(9 BYTE),
    sal                            NUMBER,
    comm                           NUMBER)
  PCTFREE     10
  INITRANS    1
  MAXTRANS    255
  TABLESPACE  $default_tablespace_user
  NOCACHE
  MONITORING
  NOPARALLEL
  LOGGING
;

CREATE TABLE $full_name_user_owner_dept
    (deptno                         NUMBER(2,0) ,
    dname                          VARCHAR2(14 BYTE),
    loc                            VARCHAR2(13 BYTE))
  PCTFREE     10
  INITRANS    1
  MAXTRANS    255
  TABLESPACE  $default_tablespace_user
  STORAGE   (
    INITIAL     65536
    NEXT        1048576
    MINEXTENTS  1
    MAXEXTENTS  2147483645
  )
  NOCACHE
  MONITORING
  NOPARALLEL
  LOGGING
;

ALTER TABLE $full_name_user_owner_dept
ADD CONSTRAINT pk_dept PRIMARY KEY (deptno)
USING INDEX
  PCTFREE     10
  INITRANS    2
  MAXTRANS    255
  TABLESPACE  $default_tablespace_user
  STORAGE   (
    INITIAL     65536
    NEXT        1048576
    MINEXTENTS  1
    MAXEXTENTS  2147483645
  )
;

CREATE TABLE $full_name_user_owner_emp
    (empno                          NUMBER(4,0) ,
    ename                          VARCHAR2(10 BYTE),
    job                            VARCHAR2(9 BYTE),
    mgr                            NUMBER(4,0),
    hiredate                       DATE,
    sal                            NUMBER(7,2),
    comm                           NUMBER(7,2),
    deptno                         NUMBER(2,0))
  PCTFREE     10
  INITRANS    1
  MAXTRANS    255
  TABLESPACE  $default_tablespace_user
  STORAGE   (
    INITIAL     65536
    NEXT        1048576
    MINEXTENTS  1
    MAXEXTENTS  2147483645
  )
  NOCACHE
  MONITORING
  NOPARALLEL
  LOGGING
;

ALTER TABLE $full_name_user_owner_emp
ADD CONSTRAINT pk_emp PRIMARY KEY (empno)
USING INDEX
  PCTFREE     10
  INITRANS    2
  MAXTRANS    255
  TABLESPACE  $default_tablespace_user
  STORAGE   (
    INITIAL     65536
    NEXT        1048576
    MINEXTENTS  1
    MAXEXTENTS  2147483645
  )
;

CREATE TABLE $full_name_user_owner_salgrade
    (grade                          NUMBER,
    losal                          NUMBER,
    hisal                          NUMBER)
  PCTFREE     10
  INITRANS    1
  MAXTRANS    255
  TABLESPACE  $default_tablespace_user
  STORAGE   (
    INITIAL     65536
    NEXT        1048576
    MINEXTENTS  1
    MAXEXTENTS  2147483645
  )
  NOCACHE
  MONITORING
  NOPARALLEL
  LOGGING
;

ALTER TABLE $full_name_user_owner_emp
ADD CONSTRAINT fk_deptno FOREIGN KEY (deptno)
REFERENCES $full_name_user_owner_dept (deptno)
;

INSERT INTO $full_name_user_owner_dept (DEPTNO,DNAME,LOC) 
VALUES(10,'ACCOUNTING','NEW YORK');
INSERT INTO $full_name_user_owner_dept (DEPTNO,DNAME,LOC) 
VALUES(20,'RESEARCH','DALLAS');
INSERT INTO $full_name_user_owner_dept (DEPTNO,DNAME,LOC) 
VALUES(30,'SALES','CHICAGO');
INSERT INTO $full_name_user_owner_dept (DEPTNO,DNAME,LOC) 
VALUES(40,'OPERATIONS','BOSTON');
commit;

INSERT INTO $full_name_user_owner_emp 
VALUES(7369,'SMITH','CLERK',7902,TO_DATE('1980-12-17 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),800,NULL,20);
INSERT INTO $full_name_user_owner_emp 
VALUES(7499,'ALLEN','SALESMAN',7698,TO_DATE('1981-02-20 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),1600,300,30);
INSERT INTO $full_name_user_owner_emp 
VALUES(7521,'WARD','SALESMAN',7698,TO_DATE('1981-02-22 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),1250,500,30);
INSERT INTO $full_name_user_owner_emp 
VALUES(7566,'JONES','MANAGER',7839,TO_DATE('1981-04-02 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),2975,NULL,20);
INSERT INTO $full_name_user_owner_emp 
VALUES(7654,'MARTIN','SALESMAN',7698,TO_DATE('1981-09-28 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),1250,1400,30);
INSERT INTO $full_name_user_owner_emp 
VALUES(7698,'BLAKE','MANAGER',7839,TO_DATE('1981-05-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),2850,NULL,30);
INSERT INTO $full_name_user_owner_emp 
VALUES(7782,'CLARK','MANAGER',7839,TO_DATE('1981-06-09 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),2450,NULL,10);
INSERT INTO $full_name_user_owner_emp 
VALUES(7788,'SCOTT','ANALYST',7566,TO_DATE('1987-04-19 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),3000,NULL,20);
INSERT INTO $full_name_user_owner_emp 
VALUES(7839,'KING','PRESIDENT',NULL,TO_DATE('1981-11-17 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),5000,NULL,10);
INSERT INTO $full_name_user_owner_emp 
VALUES(7844,'TURNER','SALESMAN',7698,TO_DATE('1981-09-08 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),1500,0,30);
INSERT INTO $full_name_user_owner_emp 
VALUES(7876,'ADAMS','CLERK',7788,TO_DATE('1987-05-23 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),1100,NULL,20);
INSERT INTO $full_name_user_owner_emp 
VALUES(7900,'JAMES','CLERK',7698,TO_DATE('1981-12-03 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),950,NULL,30);
INSERT INTO $full_name_user_owner_emp 
VALUES(7902,'FORD','ANALYST',7566,TO_DATE('1981-12-03 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),3000,NULL,20);
INSERT INTO $full_name_user_owner_emp 
VALUES(7934,'MILLER','CLERK',7782,TO_DATE('1982-01-23 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),1300,NULL,10);
commit;

INSERT INTO $full_name_user_owner_salgrade (GRADE,LOSAL,HISAL) 
VALUES(1,700,1200);
INSERT INTO $full_name_user_owner_salgrade (GRADE,LOSAL,HISAL) 
VALUES(2,1201,1400);
INSERT INTO $full_name_user_owner_salgrade (GRADE,LOSAL,HISAL) 
VALUES(3,1401,2000);
INSERT INTO $full_name_user_owner_salgrade (GRADE,LOSAL,HISAL) 
VALUES(4,2001,3000);
INSERT INTO $full_name_user_owner_salgrade (GRADE,LOSAL,HISAL) 
VALUES(5,3001,9999);
commit;

exit;
"@

# make all sqlQuery=$sqlQuery_create_tablespace + $sqlQuery_create_user
$sqlQuery = $sqlQuery_create_tablespace +$sqlQuery_create_tablespace_lob + $sqlQuery_create_user + $sqlQuery_create_table
#Write-Host $sqlQuery   
# Debug display ALL SQL=$sqlQuery_create_tablespace + $sqlQuery_create_user
$sqlOutput = $sqlQuery | sqlplus -silent $username_dba/$password_dba@$tnsalias_db
#
# Debug display result SQL

#Write-Host  $sqlOutput
#

    IF ($LASTEXITCODE -ne 0) {
        $EXITCODE = 0
       Write-Host Oracle Error Code  $LASTEXITCODE
           Write-Host  $sqlOutput
        Return $EXITCODE

        }
       ELSE {
          Write-Host User created successfully!
          $EXITCODE = 1
                  }


#Write-Host $LASTEXITCODE


C:\upwork\stackoverflow\create_user>powershell .\drop_user.ps1 scott2
NAME_USER: scott2
USER default_tablespace: tablespace_scott2
USER LOB default_tablespace: tablespace_lob_scott2
User dropped successfully!

drop_user.ps1

param(
[string]$name_user = ""
)

Write-Host "NAME_USER: $name_user"

##!!!!! ORA-01940: cannot drop a user that is currently connected
## USER must disconnect before drop user!!!

#Dropping Users
#Dropping users is very straightforward and is accomplished with the drop user command. The
#only parameters are the username to be dropped and the cascade option; any objects owned by
#the user must be explicitly dropped or moved to another schema if the cascade option is not used.
#In the following example, the user QUEENB is dropped, and if there are any objects owned by
#QUEENB, they are automatically dropped as well:
#SQL> drop user queenb cascade;
#User dropped.

$username_dba = "system"
$password_dba = "manager"
$tnsalias_db = "test-ecdu"
#$os_authent_prefix="OPS$"
$os_authent_prefix=""
$full_name_user=$os_authent_prefix+$name_user
$outputfile = "C:\Temp\create_users.txt"      # this is only required for option 1
#
$user_tablespace = "tablespace_"
$user_lob_tablespace = "tablespace_lob_"
$default_tablespace_user = $user_tablespace+$name_user
$default_lob_tablespace_user = $user_lob_tablespace+$name_user
#
Write-Host "USER default_tablespace: $default_tablespace_user"
Write-Host "USER LOB default_tablespace: $default_lob_tablespace_user"


#Make SQL DROP TABLESPACE
# The database also automatically drops from the operating system any Oracle-managed datafiles and tempfiles in the tablespace. 
#Other datafiles and tempfiles are not removed from the operating system unless you specify INCLUDING CONTENTS AND DATAFILES.
$sqlQuery_drop_tablespace = @"
set NewPage none
set heading off
set feedback off
WHENEVER SQLERROR EXIT SQL.SQLCODE
DROP TABLESPACE  $default_tablespace_user INCLUDING CONTENTS AND DATAFILES
;
DROP TABLESPACE  $default_lob_tablespace_user INCLUDING CONTENTS AND DATAFILES
;
"@

#Degug display "SQL Tablespace: $sqlQuery_drop_tablespace"
#Specify CASCADE to drop all objects in the user's schema before dropping the user. 
#You must specify this clause to drop a user whose schema contains any objects.
#Write-Host "SQL Tablespace: $sqlQuery_drop_tablespace"
#
$sqlQuery_drop_user = 
@"

DROP USER $full_name_user CASCADE 
;
exit
"@


$sqlQuery = $sqlQuery_drop_tablespace + $sqlQuery_drop_user

# Degug display  $sqlQuery = $sqlQuery_drop_tablespace + $sqlQuery_drop_user
#Write-Host $sqlQuery   

$sqlOutput = $sqlQuery | sqlplus -silent $username_dba/$password_dba@$tnsalias_db


    IF ($LASTEXITCODE -ne 0) {
        $EXITCODE = 0
       Write-Host Oracle Error Code  $LASTEXITCODE
           Write-Host  $sqlOutput
        Return $EXITCODE

        }
       ELSE {
          Write-Host User dropped successfully!
          $EXITCODE = 1
                  }






#           Write-Host  $sqlOutput
#