Skip to content

Instantly share code, notes, and snippets.

@hidari
Last active December 16, 2015 07:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hidari/5396195 to your computer and use it in GitHub Desktop.
Save hidari/5396195 to your computer and use it in GitHub Desktop.
JenkinsでPowerShellを使ってリモートの(Windows, AD)サーバへファイルをコピーするには.スクリプト内で資格情報オブジェクトを作ってそれを使ってサーバにアクセスする.
# ユーザ名
$user = "domain\username"
# パスワード
$spass = ConvertTo-SecureString "password" -AsPlainText -Force
# PSCredential(資格情報)オブジェクトを作成
$cred = New-Object System.Management.Automation.PSCredential($user, $spass)
# PSCredentialオブジェクトを使ってこのスクリプトからアクセス可能なPSDriveを作成
New-PSDrive -Name Hoge -PSProvider FileSystem -Root \\Foo\Bar -Credential $cred
# コピーを実行
$outputs = Copy-Item -Path "C:\test\src\Fuga.txt" -Destination "Hoge:\Foo\Bar\DestDit\Fuga.txt" -Force -PassThru
# コピーしたファイルと件数を表示
$outputs
$outputs.Count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment