0
0

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 1 year has passed since last update.

クレデンシャルのパスワードをPowerShellスクリプトに平文で書かない方法

Last updated at Posted at 2023-03-31

スクリプトの例

  • 下記スクリプトを例にして説明します。
  • Microsoftの各種サービスに接続するスクリプトです。
ConnectServices.ps1
param([Parameter(Mandatory = $true)][string]$PWordPlain)

cd (Convert-Path $PSScriptRoot)

$User = "UPN001@contoso.com"
$PWord = ConvertTo-SecureString -String $PWordPlain -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord

Connect-AzureAD -Credential $Credential
Connect-ExchangeOnline -Credential $Credential
Connect-SPOService -Url https://contoso-admin.sharepoint.com -Credential $Credential
Connect-MicrosoftTeams -Credential $Credential
Connect-MsolService -Credential $Credential

実行例

  • 引数を指定せずにps1を実行すると必須の引数 $PWordPlain を要求されるので、パスワードを入力します。
  • 入力時はパスワードが表示されますが、実行履歴には残りません。
PS C:\...> .\ConnectServices.ps1; cls
コマンド パイプライン位置 1 のコマンドレット ConnectServices.ps1
次のパラメーターに値を指定してください:
PWordPlain: 

...

PS C:/...> h
  Id CommandLine
  -- -----------
   1 .\ConnectServices.ps1; cls
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?