質問編集履歴

4

誤字修正

2018/09/02 07:38

投稿

minhouse10
minhouse10

スコア41

test CHANGED
File without changes
test CHANGED
@@ -78,7 +78,7 @@
78
78
 
79
79
 
80
80
 
81
- **satocha様ご指摘の箇所に関して:**
81
+ **satocha様からご指摘の箇所に関して:**
82
82
 
83
83
 
84
84
 
@@ -168,7 +168,7 @@
168
168
 
169
169
 
170
170
 
171
- **satocha様ご指摘の箇所に関して その2:**
171
+ **satocha様からご指摘の箇所に関して その2:**
172
172
 
173
173
 
174
174
 

3

修正版のフルコードを更新

2018/09/02 07:38

投稿

minhouse10
minhouse10

スコア41

test CHANGED
File without changes
test CHANGED
@@ -163,3 +163,173 @@
163
163
  + CategoryInfo : NotSpecified: (:) [Copy-Item], NotSupportedException
164
164
 
165
165
  + FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.CopyItemCommand
166
+
167
+
168
+
169
+
170
+
171
+ **satocha様にご指摘の箇所に関して その2:**
172
+
173
+
174
+
175
+ satocha様にご指摘頂きました通り、dateの変数のフォーマットが問題でした。
176
+
177
+ 以下のように変更したところスクリプトが意図した通りの結果を出しました!
178
+
179
+
180
+
181
+ $date = Get-Date -Format "-yyyyMMdd-HH-mm-ss"
182
+
183
+
184
+
185
+ ただしコロンを含んでいてもTrueが返りました。
186
+
187
+ PS C:\Users\Desktop\PS_Folder> test-path -isvalid "C:\Users\Desktop\PS_Folder\CC\A\web.config-20180902-11:18:47 "
188
+
189
+ **True**
190
+
191
+
192
+
193
+
194
+
195
+ 全体的にまだまだ改善の余地の多い汚いコードかと思いますが、修正版フルコードは以下の通りとなります。
196
+
197
+
198
+
199
+ ```Powershell
200
+
201
+ $date = Get-Date -Format "-yyyyMMdd-HH-mm-ss"
202
+
203
+ $homeDir = 'C:\Users\Desktop\PS_Folder'
204
+
205
+ $conStrFrom = 'Server1'
206
+
207
+ $conStrTo = 'Server2'
208
+
209
+ $targetFile = 'web.config'
210
+
211
+
212
+
213
+ Set-Location $homeDir
214
+
215
+
216
+
217
+ function Get-ScriptPath {
218
+
219
+ Split-Path $MyInvocation.ScriptName
220
+
221
+ }
222
+
223
+
224
+
225
+ $CntReplaced = 0
226
+
227
+ if ((Get-ScriptPath) -eq $homeDir) {
228
+
229
+ echo "The Path is correct: $homeDir"
230
+
231
+ echo "`r`n"
232
+
233
+ }
234
+
235
+ else {
236
+
237
+ echo "--Script is not in the correct path!!--"
238
+
239
+ exit
240
+
241
+ }
242
+
243
+ $web_configfiles = Get-ChildItem $homeDir -recurse -include $targetFile
244
+
245
+ $filesCnt = ($web_configfiles).Count
246
+
247
+
248
+
249
+ echo ("There are $filesCnt `"$targetFile`" Files`r`n")
250
+
251
+
252
+
253
+ $backUp = Read-Host "Do You Want to Take Backups of $targetFile ? Please enter Y or N"
254
+
255
+ if ($backUp -eq "Y"){
256
+
257
+ echo "--Start Backuping $targetFile--`r`n"
258
+
259
+ $web_configfiles | foreach-object{
260
+
261
+ $filePath = $_.fullname
262
+
263
+ $parent = split-path $filePath
264
+
265
+ $fileBase = [io.path]::getFileNameWithoutExtension( $filePath )
266
+
267
+ $ext = [io.path]::getExtension( $filePath )
268
+
269
+ $sourcePath = Join-Path $parent $fileBase$ext
270
+
271
+ $destPath = Join-Path $parent $fileBase$ext$date
272
+
273
+ Copy-Item -Path $sourcePath -Destination $destPath
274
+
275
+ echo "Backup was successful to create in : $parent"
276
+
277
+ }
278
+
279
+ }
280
+
281
+ elseif ($backUp -eq "N") {
282
+
283
+ echo "--User didn't choose to take backups--`r`n"
284
+
285
+ }
286
+
287
+ else {
288
+
289
+ echo "--Please enter Y or N--"
290
+
291
+ exit
292
+
293
+ }
294
+
295
+
296
+
297
+ if ($web_configfiles) {
298
+
299
+ foreach ($file in $web_configfiles) {
300
+
301
+ $cnt = (Get-Content $file |Select-String $conStrFrom).Count
302
+
303
+ $fname = Split-Path $file -Leaf
304
+
305
+ echo ("$fname contains Target text in `"$cnt`" location")
306
+
307
+
308
+
309
+ if ($cnt -gt 0) {
310
+
311
+ (Get-Content $file) | Foreach-Object { $_ -creplace $conStrFrom, $conStrTo } | Set-Content $file
312
+
313
+ $CntReplaced = $CntReplaced + 1
314
+
315
+ }
316
+
317
+ else {
318
+
319
+ echo ("--The File doesn't contain Target Text--")
320
+
321
+ }
322
+
323
+
324
+
325
+ }
326
+
327
+ }
328
+
329
+
330
+
331
+ echo `r`n
332
+
333
+ echo "`"$CntReplaced`" $fname files of text `"$conStrFrom`" were replaced to `"$conStrTo`""
334
+
335
+ ```

2

修正版コードと新たなエラーを更新

2018/09/02 07:17

投稿

minhouse10
minhouse10

スコア41

test CHANGED
File without changes
test CHANGED
@@ -95,3 +95,71 @@
95
95
  ---- ------------- ------ ----
96
96
 
97
97
  -a---- 9/1/2018 8:54 PM 5308 web.config
98
+
99
+
100
+
101
+ -----
102
+
103
+
104
+
105
+ ```Powershell
106
+
107
+ $web_configfiles | foreach-object{
108
+
109
+ $filePath = $_.fullname #ファイルのフルパスを文字列として取得
110
+
111
+ #echo $filePath
112
+
113
+ $parent = split-path $filePath #親ディレクトリ
114
+
115
+ #echo $parent
116
+
117
+ $fileBase = [io.path]::getFileNameWithoutExtension( $filePath ) #拡張子を除いたファイル名
118
+
119
+ #echo $fileBase
120
+
121
+ $ext = [io.path]::getExtension( $filePath ) #拡張子(.を含む)
122
+
123
+ #echo $fileBase$ext
124
+
125
+ #あとは新パスを適当に組み立ててコピー
126
+
127
+ $sourcePath = Join-Path $parent $fileBase$ext
128
+
129
+ $destPath = Join-Path $parent $fileBase$ext$date
130
+
131
+ echo $sourcePath
132
+
133
+ echo $destPath
134
+
135
+ Copy-Item -Path $sourcePath -Destination $destPath
136
+
137
+ }
138
+
139
+ ```
140
+
141
+ Copy元とコピー先のそれぞれ対象ディレクトリのパスとファイル名は意図したものとなっているがエラーとなってしまう。
142
+
143
+
144
+
145
+ **echo $sourcePath**
146
+
147
+ C:\Users\Desktop\PS_Folder\CC\A\web.config
148
+
149
+ **echo $destPath**
150
+
151
+ C:\Users\Desktop\PS_Folder\CC\A\web.config-20180902-11:18:47
152
+
153
+
154
+
155
+ Copy-Item : The given path's format is not supported.
156
+
157
+ At C:\Users\Desktop\PS_Folder\change_connstr.ps1:51 char:13
158
+
159
+ + Copy-Item -Path $sourcePath -Destination $destPath
160
+
161
+ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
162
+
163
+ + CategoryInfo : NotSpecified: (:) [Copy-Item], NotSupportedException
164
+
165
+ + FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.CopyItemCommand

1

satocha様のご指摘に関する修正について

2018/09/02 03:22

投稿

minhouse10
minhouse10

スコア41

test CHANGED
File without changes
test CHANGED
@@ -16,13 +16,13 @@
16
16
 
17
17
  + CategoryInfo : NotSpecified: (:) [Copy-Item], NotSupportedException
18
18
 
19
- + FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.CopyItemCommand
19
+ + FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.CopyItemCommand
20
20
 
21
21
 
22
22
 
23
- **バックアップをとる部分のスクリプトは以下になります。**
23
+ **バックアップをとる部分の修正前スクリプトは以下になります。**
24
24
 
25
- ```ここに言語を入力
25
+ ```Powershell
26
26
 
27
27
  #複数のサブディレクトリに同じ名前のファイルがあるので再帰的に取得
28
28
 
@@ -75,3 +75,23 @@
75
75
  exit
76
76
 
77
77
  ```
78
+
79
+
80
+
81
+ **satocha様にご指摘の箇所に関して:**
82
+
83
+
84
+
85
+ web_configfilesをechoした結果は以下のようにヒットした複数ファイル数分出力されます。今回はweb.configというファイルが9つ検索にヒットします。
86
+
87
+
88
+
89
+ Directory: C:\Users\Desktop\PS_Folder\A
90
+
91
+
92
+
93
+ Mode LastWriteTime Length Name
94
+
95
+ ---- ------------- ------ ----
96
+
97
+ -a---- 9/1/2018 8:54 PM 5308 web.config