質問編集履歴
4
誤字修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -38,7 +38,7 @@
|
|
38
38
|
exit
|
39
39
|
```
|
40
40
|
|
41
|
-
**satocha様
|
41
|
+
**satocha様からご指摘の箇所に関して:**
|
42
42
|
|
43
43
|
web_configfilesをechoした結果は以下のようにヒットした複数ファイル数分出力されます。今回はweb.configというファイルが9つ検索にヒットします。
|
44
44
|
|
@@ -83,7 +83,7 @@
|
|
83
83
|
+ FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.CopyItemCommand
|
84
84
|
|
85
85
|
|
86
|
-
**satocha様
|
86
|
+
**satocha様からご指摘の箇所に関して その2:**
|
87
87
|
|
88
88
|
satocha様にご指摘頂きました通り、dateの変数のフォーマットが問題でした。
|
89
89
|
以下のように変更したところスクリプトが意図した通りの結果を出しました!
|
3
修正版のフルコードを更新
title
CHANGED
File without changes
|
body
CHANGED
@@ -80,4 +80,89 @@
|
|
80
80
|
+ Copy-Item -Path $sourcePath -Destination $destPath
|
81
81
|
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
82
82
|
+ CategoryInfo : NotSpecified: (:) [Copy-Item], NotSupportedException
|
83
|
-
+ FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.CopyItemCommand
|
83
|
+
+ FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.CopyItemCommand
|
84
|
+
|
85
|
+
|
86
|
+
**satocha様にご指摘の箇所に関して その2:**
|
87
|
+
|
88
|
+
satocha様にご指摘頂きました通り、dateの変数のフォーマットが問題でした。
|
89
|
+
以下のように変更したところスクリプトが意図した通りの結果を出しました!
|
90
|
+
|
91
|
+
$date = Get-Date -Format "-yyyyMMdd-HH-mm-ss"
|
92
|
+
|
93
|
+
ただしコロンを含んでいてもTrueが返りました。
|
94
|
+
PS C:\Users\Desktop\PS_Folder> test-path -isvalid "C:\Users\Desktop\PS_Folder\CC\A\web.config-20180902-11:18:47 "
|
95
|
+
**True**
|
96
|
+
|
97
|
+
|
98
|
+
全体的にまだまだ改善の余地の多い汚いコードかと思いますが、修正版フルコードは以下の通りとなります。
|
99
|
+
|
100
|
+
```Powershell
|
101
|
+
$date = Get-Date -Format "-yyyyMMdd-HH-mm-ss"
|
102
|
+
$homeDir = 'C:\Users\Desktop\PS_Folder'
|
103
|
+
$conStrFrom = 'Server1'
|
104
|
+
$conStrTo = 'Server2'
|
105
|
+
$targetFile = 'web.config'
|
106
|
+
|
107
|
+
Set-Location $homeDir
|
108
|
+
|
109
|
+
function Get-ScriptPath {
|
110
|
+
Split-Path $MyInvocation.ScriptName
|
111
|
+
}
|
112
|
+
|
113
|
+
$CntReplaced = 0
|
114
|
+
if ((Get-ScriptPath) -eq $homeDir) {
|
115
|
+
echo "The Path is correct: $homeDir"
|
116
|
+
echo "`r`n"
|
117
|
+
}
|
118
|
+
else {
|
119
|
+
echo "--Script is not in the correct path!!--"
|
120
|
+
exit
|
121
|
+
}
|
122
|
+
$web_configfiles = Get-ChildItem $homeDir -recurse -include $targetFile
|
123
|
+
$filesCnt = ($web_configfiles).Count
|
124
|
+
|
125
|
+
echo ("There are $filesCnt `"$targetFile`" Files`r`n")
|
126
|
+
|
127
|
+
$backUp = Read-Host "Do You Want to Take Backups of $targetFile ? Please enter Y or N"
|
128
|
+
if ($backUp -eq "Y"){
|
129
|
+
echo "--Start Backuping $targetFile--`r`n"
|
130
|
+
$web_configfiles | foreach-object{
|
131
|
+
$filePath = $_.fullname
|
132
|
+
$parent = split-path $filePath
|
133
|
+
$fileBase = [io.path]::getFileNameWithoutExtension( $filePath )
|
134
|
+
$ext = [io.path]::getExtension( $filePath )
|
135
|
+
$sourcePath = Join-Path $parent $fileBase$ext
|
136
|
+
$destPath = Join-Path $parent $fileBase$ext$date
|
137
|
+
Copy-Item -Path $sourcePath -Destination $destPath
|
138
|
+
echo "Backup was successful to create in : $parent"
|
139
|
+
}
|
140
|
+
}
|
141
|
+
elseif ($backUp -eq "N") {
|
142
|
+
echo "--User didn't choose to take backups--`r`n"
|
143
|
+
}
|
144
|
+
else {
|
145
|
+
echo "--Please enter Y or N--"
|
146
|
+
exit
|
147
|
+
}
|
148
|
+
|
149
|
+
if ($web_configfiles) {
|
150
|
+
foreach ($file in $web_configfiles) {
|
151
|
+
$cnt = (Get-Content $file |Select-String $conStrFrom).Count
|
152
|
+
$fname = Split-Path $file -Leaf
|
153
|
+
echo ("$fname contains Target text in `"$cnt`" location")
|
154
|
+
|
155
|
+
if ($cnt -gt 0) {
|
156
|
+
(Get-Content $file) | Foreach-Object { $_ -creplace $conStrFrom, $conStrTo } | Set-Content $file
|
157
|
+
$CntReplaced = $CntReplaced + 1
|
158
|
+
}
|
159
|
+
else {
|
160
|
+
echo ("--The File doesn't contain Target Text--")
|
161
|
+
}
|
162
|
+
|
163
|
+
}
|
164
|
+
}
|
165
|
+
|
166
|
+
echo `r`n
|
167
|
+
echo "`"$CntReplaced`" $fname files of text `"$conStrFrom`" were replaced to `"$conStrTo`""
|
168
|
+
```
|
2
修正版コードと新たなエラーを更新
title
CHANGED
File without changes
|
body
CHANGED
@@ -46,4 +46,38 @@
|
|
46
46
|
|
47
47
|
Mode LastWriteTime Length Name
|
48
48
|
---- ------------- ------ ----
|
49
|
-
-a---- 9/1/2018 8:54 PM 5308 web.config
|
49
|
+
-a---- 9/1/2018 8:54 PM 5308 web.config
|
50
|
+
|
51
|
+
-----
|
52
|
+
|
53
|
+
```Powershell
|
54
|
+
$web_configfiles | foreach-object{
|
55
|
+
$filePath = $_.fullname #ファイルのフルパスを文字列として取得
|
56
|
+
#echo $filePath
|
57
|
+
$parent = split-path $filePath #親ディレクトリ
|
58
|
+
#echo $parent
|
59
|
+
$fileBase = [io.path]::getFileNameWithoutExtension( $filePath ) #拡張子を除いたファイル名
|
60
|
+
#echo $fileBase
|
61
|
+
$ext = [io.path]::getExtension( $filePath ) #拡張子(.を含む)
|
62
|
+
#echo $fileBase$ext
|
63
|
+
#あとは新パスを適当に組み立ててコピー
|
64
|
+
$sourcePath = Join-Path $parent $fileBase$ext
|
65
|
+
$destPath = Join-Path $parent $fileBase$ext$date
|
66
|
+
echo $sourcePath
|
67
|
+
echo $destPath
|
68
|
+
Copy-Item -Path $sourcePath -Destination $destPath
|
69
|
+
}
|
70
|
+
```
|
71
|
+
Copy元とコピー先のそれぞれ対象ディレクトリのパスとファイル名は意図したものとなっているがエラーとなってしまう。
|
72
|
+
|
73
|
+
**echo $sourcePath**
|
74
|
+
C:\Users\Desktop\PS_Folder\CC\A\web.config
|
75
|
+
**echo $destPath**
|
76
|
+
C:\Users\Desktop\PS_Folder\CC\A\web.config-20180902-11:18:47
|
77
|
+
|
78
|
+
Copy-Item : The given path's format is not supported.
|
79
|
+
At C:\Users\Desktop\PS_Folder\change_connstr.ps1:51 char:13
|
80
|
+
+ Copy-Item -Path $sourcePath -Destination $destPath
|
81
|
+
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
82
|
+
+ CategoryInfo : NotSpecified: (:) [Copy-Item], NotSupportedException
|
83
|
+
+ FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.CopyItemCommand
|
1
satocha様のご指摘に関する修正について
title
CHANGED
File without changes
|
body
CHANGED
@@ -7,10 +7,10 @@
|
|
7
7
|
+ ... Copy-Item -Path $fPath'\'$targetFile -Destination $fPath' ...
|
8
8
|
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
9
9
|
+ CategoryInfo : NotSpecified: (:) [Copy-Item], NotSupportedException
|
10
|
-
+ FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.CopyItemCommand
|
10
|
+
+ FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.CopyItemCommand
|
11
11
|
|
12
|
-
**バックアップをとる部分のスクリプトは以下になります。**
|
12
|
+
**バックアップをとる部分の修正前スクリプトは以下になります。**
|
13
|
-
```
|
13
|
+
```Powershell
|
14
14
|
#複数のサブディレクトリに同じ名前のファイルがあるので再帰的に取得
|
15
15
|
$web_configfiles = Get-ChildItem $homeDir -recurse -include $targetFile
|
16
16
|
|
@@ -36,4 +36,14 @@
|
|
36
36
|
else {
|
37
37
|
echo "--Please enter Y or N--"
|
38
38
|
exit
|
39
|
-
```
|
39
|
+
```
|
40
|
+
|
41
|
+
**satocha様にご指摘の箇所に関して:**
|
42
|
+
|
43
|
+
web_configfilesをechoした結果は以下のようにヒットした複数ファイル数分出力されます。今回はweb.configというファイルが9つ検索にヒットします。
|
44
|
+
|
45
|
+
Directory: C:\Users\Desktop\PS_Folder\A
|
46
|
+
|
47
|
+
Mode LastWriteTime Length Name
|
48
|
+
---- ------------- ------ ----
|
49
|
+
-a---- 9/1/2018 8:54 PM 5308 web.config
|