0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SQLserverのユーザ追加メモ

Last updated at Posted at 2022-05-13

はじめに

SQLServerのユーザは下記の2つ必要っぽい

  • インスタンスログイン用(Management Studioログイン)
  • DBログイン

ユーザを作る

PowerShellでSelectするだけのユーザを作る時のメモ

-- インスタンスログイン用ユーザの作成
CREATE LOGIN ユーザ名_i
    WITH PASSWORD = 'パスワード'; 

-- データベース ユーザーの作成 (ユーザ名_d = ユーザ名_i、異なる名称にしたらどうなるかは把握していない)
use DB名;
CREATE USER ユーザ名_d FOR LOGIN ユーザ名_i;  

-- 特定のテーブルのSelectのみを許可
GRANT SELECT ON OBJECT::テーブル名 TO ユーザ名_d;

パッチ用のパスワード暗号化ファイルを作成する

バッチにパスワードを平文で書きたくないのでやる

# パスワードの暗号化
$Cred = ConvertTo-SecureString -string "パスワード" -AsPlainText -Force

# 以下を実行して入力情報を暗号化ファイルに出力する
"$Cred | ConvertFrom-SecureString | Out-File パスワードファイルのフルパス 

バッチ内でパスワードの復号は下記のようにやる

# 暗号化したテキストをSecureStringオブジェクトに変換
$importSecureString = Get-Content パスワードファイルのフルパス | ConvertTo-SecureString

# SecureStringからパスワードを復号
$bstr               = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($importSecureString)
$StringPassword     = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr)
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?