実現したいこと:
ある名前のファイルを検索し、そのファイルの中の特定のキーワードを変更するためのPowershellスクリプトの中で、変更前ファイルのバックアップ機能として、元のファイルがある同じディレクトリに別名として(同じファイル名に日時情報を加える)コピーを作成機能が実現できずにおり、Webで検索したり本をみたりしていますが、Powershellのスクリプトを書いたのが今回初めてのため解決策が見つけられずにいます。アドバイス頂ければ幸いです。どうぞ、よろしくお願い致します。
エラー:
Copy-Item : The given path's format is not supported.
At C:\Users\Desktop\PS_Folder\change_connstr.ps1:38 char:17
- ... Copy-Item -Path $fPath''$targetFile -Destination $fPath' ...
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : NotSpecified: (:) [Copy-Item], NotSupportedException
- FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.CopyItemCommand
バックアップをとる部分の修正前スクリプトは以下になります。
Powershell
1#複数のサブディレクトリに同じ名前のファイルがあるので再帰的に取得 2$web_configfiles = Get-ChildItem $homeDir -recurse -include $targetFile 3 4#バックアップ取得するかどうかは実行者が決定する仕様 5 $backUp = Read-Host "Do You Want to Take Backups of $targetFile ? Please enter Y or N" 6 if ($backUp -eq "Y"){ 7 echo "--Start Backuping $targetFile--`r`n" 8 #以下でファイルがみつかったディレクトリパスのみを取得 9 $dir_paths = ($web_configfiles).DirectoryName 10 #ループにて対象ファイルを一つずつ取得 11 foreach ($bkFile in $web_configfiles) { 12 #ループにて対象ディレクトリパスを一つずつ取得 13 foreach ($fPath in $dir_paths) { 14 #コピー対象のパスとファイルをそれぞれうまく渡せず上記エラーになる 15 Copy-Item -Path $fPath'\'$bkFile -Destination $fPath'\'$bkFile'_'$date$fPath'\web.config_'$date 16 echo "Backup was success to make in : $fPath" 17 } 18 } 19 } 20 elseif ($backUp -eq "N") { 21 echo "--User didn't choose to take backups--`r`n" 22 } 23 else { 24 echo "--Please enter Y or N--" 25 exit
satocha様からご指摘の箇所に関して:
web_configfilesをechoした結果は以下のようにヒットした複数ファイル数分出力されます。今回はweb.configというファイルが9つ検索にヒットします。
Directory: C:\Users\Desktop\PS_Folder\A
Mode LastWriteTime Length Name
-a---- 9/1/2018 8:54 PM 5308 web.config
Powershell
1 $web_configfiles | foreach-object{ 2 $filePath = $_.fullname #ファイルのフルパスを文字列として取得 3 #echo $filePath 4 $parent = split-path $filePath #親ディレクトリ 5 #echo $parent 6 $fileBase = [io.path]::getFileNameWithoutExtension( $filePath ) #拡張子を除いたファイル名 7 #echo $fileBase 8 $ext = [io.path]::getExtension( $filePath ) #拡張子(.を含む) 9 #echo $fileBase$ext 10 #あとは新パスを適当に組み立ててコピー 11 $sourcePath = Join-Path $parent $fileBase$ext 12 $destPath = Join-Path $parent $fileBase$ext$date 13 echo $sourcePath 14 echo $destPath 15 Copy-Item -Path $sourcePath -Destination $destPath 16 }
Copy元とコピー先のそれぞれ対象ディレクトリのパスとファイル名は意図したものとなっているがエラーとなってしまう。
echo $sourcePath
C:\Users\Desktop\PS_Folder\CC\A\web.config
echo $destPath
C:\Users\Desktop\PS_Folder\CC\A\web.config-20180902-11:18:47
Copy-Item : The given path's format is not supported.
At C:\Users\Desktop\PS_Folder\change_connstr.ps1:51 char:13
-
Copy-Item -Path $sourcePath -Destination $destPath
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : NotSpecified: (:) [Copy-Item], NotSupportedException
- FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.CopyItemCommand
satocha様からご指摘の箇所に関して その2:
satocha様にご指摘頂きました通り、dateの変数のフォーマットが問題でした。
以下のように変更したところスクリプトが意図した通りの結果を出しました!
$date = Get-Date -Format "-yyyyMMdd-HH-mm-ss"
ただしコロンを含んでいてもTrueが返りました。
PS C:\Users\Desktop\PS_Folder> test-path -isvalid "C:\Users\Desktop\PS_Folder\CC\A\web.config-20180902-11:18:47 "
True
全体的にまだまだ改善の余地の多い汚いコードかと思いますが、修正版フルコードは以下の通りとなります。
Powershell
1$date = Get-Date -Format "-yyyyMMdd-HH-mm-ss" 2$homeDir = 'C:\Users\Desktop\PS_Folder' 3$conStrFrom = 'Server1' 4$conStrTo = 'Server2' 5$targetFile = 'web.config' 6 7Set-Location $homeDir 8 9function Get-ScriptPath { 10 Split-Path $MyInvocation.ScriptName 11} 12 13$CntReplaced = 0 14if ((Get-ScriptPath) -eq $homeDir) { 15 echo "The Path is correct: $homeDir" 16 echo "`r`n" 17 } 18else { 19 echo "--Script is not in the correct path!!--" 20 exit 21} 22 $web_configfiles = Get-ChildItem $homeDir -recurse -include $targetFile 23 $filesCnt = ($web_configfiles).Count 24 25 echo ("There are $filesCnt `"$targetFile`" Files`r`n") 26 27 $backUp = Read-Host "Do You Want to Take Backups of $targetFile ? Please enter Y or N" 28 if ($backUp -eq "Y"){ 29 echo "--Start Backuping $targetFile--`r`n" 30 $web_configfiles | foreach-object{ 31 $filePath = $_.fullname 32 $parent = split-path $filePath 33 $fileBase = [io.path]::getFileNameWithoutExtension( $filePath ) 34 $ext = [io.path]::getExtension( $filePath ) 35 $sourcePath = Join-Path $parent $fileBase$ext 36 $destPath = Join-Path $parent $fileBase$ext$date 37 Copy-Item -Path $sourcePath -Destination $destPath 38 echo "Backup was successful to create in : $parent" 39 } 40 } 41 elseif ($backUp -eq "N") { 42 echo "--User didn't choose to take backups--`r`n" 43 } 44 else { 45 echo "--Please enter Y or N--" 46 exit 47 } 48 49 if ($web_configfiles) { 50 foreach ($file in $web_configfiles) { 51 $cnt = (Get-Content $file |Select-String $conStrFrom).Count 52 $fname = Split-Path $file -Leaf 53 echo ("$fname contains Target text in `"$cnt`" location") 54 55 if ($cnt -gt 0) { 56 (Get-Content $file) | Foreach-Object { $_ -creplace $conStrFrom, $conStrTo } | Set-Content $file 57 $CntReplaced = $CntReplaced + 1 58 } 59 else { 60 echo ("--The File doesn't contain Target Text--") 61 } 62 63 } 64 } 65 66echo `r`n 67echo "`"$CntReplaced`" $fname files of text `"$conStrFrom`" were replaced to `"$conStrTo`""

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/09/02 06:45
2018/09/02 07:16