実現したいこと:
ある名前のファイルを検索し、そのファイルの中の特定のキーワードを変更するための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
#複数のサブディレクトリに同じ名前のファイルがあるので再帰的に取得 $web_configfiles = Get-ChildItem $homeDir -recurse -include $targetFile #バックアップ取得するかどうかは実行者が決定する仕様 $backUp = Read-Host "Do You Want to Take Backups of $targetFile ? Please enter Y or N" if ($backUp -eq "Y"){ echo "--Start Backuping $targetFile--`r`n" #以下でファイルがみつかったディレクトリパスのみを取得 $dir_paths = ($web_configfiles).DirectoryName #ループにて対象ファイルを一つずつ取得 foreach ($bkFile in $web_configfiles) { #ループにて対象ディレクトリパスを一つずつ取得 foreach ($fPath in $dir_paths) { #コピー対象のパスとファイルをそれぞれうまく渡せず上記エラーになる Copy-Item -Path $fPath'\'$bkFile -Destination $fPath'\'$bkFile'_'$date$fPath'\web.config_'$date echo "Backup was success to make in : $fPath" } } } elseif ($backUp -eq "N") { echo "--User didn't choose to take backups--`r`n" } else { echo "--Please enter Y or N--" 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
$web_configfiles | foreach-object{ $filePath = $_.fullname #ファイルのフルパスを文字列として取得 #echo $filePath $parent = split-path $filePath #親ディレクトリ #echo $parent $fileBase = [io.path]::getFileNameWithoutExtension( $filePath ) #拡張子を除いたファイル名 #echo $fileBase $ext = [io.path]::getExtension( $filePath ) #拡張子(.を含む) #echo $fileBase$ext #あとは新パスを適当に組み立ててコピー $sourcePath = Join-Path $parent $fileBase$ext $destPath = Join-Path $parent $fileBase$ext$date echo $sourcePath echo $destPath Copy-Item -Path $sourcePath -Destination $destPath }
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
$date = Get-Date -Format "-yyyyMMdd-HH-mm-ss" $homeDir = 'C:\Users\Desktop\PS_Folder' $conStrFrom = 'Server1' $conStrTo = 'Server2' $targetFile = 'web.config' Set-Location $homeDir function Get-ScriptPath { Split-Path $MyInvocation.ScriptName } $CntReplaced = 0 if ((Get-ScriptPath) -eq $homeDir) { echo "The Path is correct: $homeDir" echo "`r`n" } else { echo "--Script is not in the correct path!!--" exit } $web_configfiles = Get-ChildItem $homeDir -recurse -include $targetFile $filesCnt = ($web_configfiles).Count echo ("There are $filesCnt `"$targetFile`" Files`r`n") $backUp = Read-Host "Do You Want to Take Backups of $targetFile ? Please enter Y or N" if ($backUp -eq "Y"){ echo "--Start Backuping $targetFile--`r`n" $web_configfiles | foreach-object{ $filePath = $_.fullname $parent = split-path $filePath $fileBase = [io.path]::getFileNameWithoutExtension( $filePath ) $ext = [io.path]::getExtension( $filePath ) $sourcePath = Join-Path $parent $fileBase$ext $destPath = Join-Path $parent $fileBase$ext$date Copy-Item -Path $sourcePath -Destination $destPath echo "Backup was successful to create in : $parent" } } elseif ($backUp -eq "N") { echo "--User didn't choose to take backups--`r`n" } else { echo "--Please enter Y or N--" exit } if ($web_configfiles) { foreach ($file in $web_configfiles) { $cnt = (Get-Content $file |Select-String $conStrFrom).Count $fname = Split-Path $file -Leaf echo ("$fname contains Target text in `"$cnt`" location") if ($cnt -gt 0) { (Get-Content $file) | Foreach-Object { $_ -creplace $conStrFrom, $conStrTo } | Set-Content $file $CntReplaced = $CntReplaced + 1 } else { echo ("--The File doesn't contain Target Text--") } } } echo `r`n echo "`"$CntReplaced`" $fname files of text `"$conStrFrom`" were replaced to `"$conStrTo`""
まだ回答がついていません
会員登録して回答してみよう