回答編集履歴
3
PS v2 に対応した方法へ変更(テキスト取得方法、ホスト名取得)
test
CHANGED
@@ -88,7 +88,31 @@
|
|
88
88
|
|
89
89
|
|
90
90
|
|
91
|
+
#### 追記
|
92
|
+
|
93
|
+
上記の配列に対してその要素のメソッドを呼び出す方法はPowerShell version 2ではできない方法でしたので、一括して読み込む方法を使うとよいです。
|
94
|
+
|
95
|
+
```PowerShell
|
96
|
+
|
97
|
+
# これを
|
98
|
+
|
99
|
+
$conf = Get-Content sample.conf -Encoding utf8
|
100
|
+
|
101
|
+
↓
|
102
|
+
|
103
|
+
# こっちに変更
|
104
|
+
|
105
|
+
$conf = [IO.File]::ReadAllText(sample.conf, [Text.Encoding]::UTF8)
|
106
|
+
|
107
|
+
```
|
108
|
+
|
109
|
+
こうすることで文字列の配列ではなく一つの文字列としてファイルを読み込みます。
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
|
114
|
+
|
91
|
-
最後に全体的に手直しして少し見通しをよくしたコードを置いて置きます。まったくテストしてないのでバグがあったらすみません。ご参考までに。
|
115
|
+
最後に全体的に手直しして少し見通しをよくしたコードを置いて置きます。まったくテストしてないのでバグがあったらすみません。ご参考までに。(こちらも修正しました)
|
92
116
|
|
93
117
|
|
94
118
|
|
@@ -126,7 +150,7 @@
|
|
126
150
|
|
127
151
|
"ServerActive=127.0.0.1" = "ServerActive=$ip"
|
128
152
|
|
129
|
-
"Hostname=Windows host" = "Hostname=" +
|
153
|
+
"Hostname=Windows host" = "Hostname=" + (hostname)
|
130
154
|
|
131
155
|
"# HostMetadata=" = "# HostMetadata=rnrnHostMetadata=$metaData"
|
132
156
|
|
@@ -176,7 +200,7 @@
|
|
176
200
|
|
177
201
|
#confファイル書き換え
|
178
202
|
|
179
|
-
$conf = $confFile
|
203
|
+
$conf = [IO.File]::ReadAllText($confFile, [Text.Encoding]::UTF8)
|
180
204
|
|
181
205
|
$h.GetEnumerator() | foreach { $conf = $conf.Replace($_.Key, $_.Value) }
|
182
206
|
|
2
zipファイルのコピーを省略した
test
CHANGED
@@ -58,6 +58,70 @@
|
|
58
58
|
|
59
59
|
"LogFile=c:\zabbix_agentd.log" = "LogFile=$logPath"
|
60
60
|
|
61
|
+
"Server=127.0.0.1" = "Server=$ip"
|
62
|
+
|
63
|
+
"ServerActive=127.0.0.1" = "ServerActive=$ip"
|
64
|
+
|
65
|
+
"Hostname=Windows host" = "Hostname=" + [Net.Dns]::GetHostName()
|
66
|
+
|
67
|
+
"# HostMetadata=" = "# HostMetadata=rnrnHostMetadata=$metaData"
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
$conf = Get-Content sample.conf -Encoding utf8
|
74
|
+
|
75
|
+
$h.GetEnumerator() | foreach { $conf = $conf.Replace($_.Key, $_.Value) }
|
76
|
+
|
77
|
+
$conf | Out-File sample.conf -Encoding utf8
|
78
|
+
|
79
|
+
```
|
80
|
+
|
81
|
+
この辺りはいろいろなやり方があると思うので自分が良いと思う方法でやってください。単純に
|
82
|
+
|
83
|
+
`"abcde".Replace("a","x").Replace("b","y")・・・`とつなげてもいいです。
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
あと置換の度にファイルに保存して、また読み込んで、を繰り返すのは無駄なので変数内で繰り返し置換したあとに最後に保存するとよいと思います。
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
最後に全体的に手直しして少し見通しをよくしたコードを置いて置きます。まったくテストしてないのでバグがあったらすみません。ご参考までに。
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
```PowerShell
|
96
|
+
|
97
|
+
$ip = "..."
|
98
|
+
|
99
|
+
$logPath = "c:\zabbix\zabbix_agentd.log"
|
100
|
+
|
101
|
+
$metaData = "windows2008"
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
##### ここから本文 ######
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
$zipPath = Join-Path ([Environment]::GetFolderPath("Desktop")) "zabbix_agents.zip"
|
110
|
+
|
111
|
+
$destDir = "C:\zabbix"
|
112
|
+
|
113
|
+
$exeName = "zabbix_agentd.exe"
|
114
|
+
|
115
|
+
$confName = "zabbix_agentd.conf"
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
#confファイル置換用ハッシュテーブル
|
120
|
+
|
121
|
+
$h = @{
|
122
|
+
|
123
|
+
"LogFile=c:\zabbix_agentd.log" = "LogFile=$logPath"
|
124
|
+
|
61
125
|
"Server=127.0.0.1" = "Server=$ip"
|
62
126
|
|
63
127
|
"ServerActive=127.0.0.1" = "ServerActive=$ip"
|
@@ -70,130 +134,56 @@
|
|
70
134
|
|
71
135
|
|
72
136
|
|
137
|
+
#スクリプト出力ログ
|
138
|
+
|
139
|
+
$scriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
|
140
|
+
|
141
|
+
Start-Transcript -Path "$scriptRoot/zabbixAgentインストール.log"
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
#### 処理開始 ####
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
#フォルダ作成
|
150
|
+
|
151
|
+
New-Item $destDir -ItemType Directory > $null
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
#解凍
|
156
|
+
|
157
|
+
$shell = New-Object -ComObject shell.application
|
158
|
+
|
159
|
+
$srcNS = $shell.NameSpace($zipPath)
|
160
|
+
|
161
|
+
$destNS = $shell.NameSpace($destDir)
|
162
|
+
|
163
|
+
$destNS.CopyHere($srcNS.Items())
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
#exeファイル配置
|
168
|
+
|
169
|
+
$exeFile = Move-Item "$destDir/bin/$exeName" $destDir -PassThru
|
170
|
+
|
171
|
+
#confフォルダ配置
|
172
|
+
|
173
|
+
$confFile = Move-Item "$destDir/conf/$confName" $destDir -PassThru
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
#confファイル書き換え
|
178
|
+
|
73
|
-
$conf = Get-Content
|
179
|
+
$conf = $confFile | Get-Content -Encoding utf8
|
74
180
|
|
75
181
|
$h.GetEnumerator() | foreach { $conf = $conf.Replace($_.Key, $_.Value) }
|
76
182
|
|
77
|
-
$conf | Out-File sample.conf -Encoding utf8
|
78
|
-
|
79
|
-
```
|
80
|
-
|
81
|
-
この辺りはいろいろなやり方があると思うので自分が良いと思う方法でやってください。単純に
|
82
|
-
|
83
|
-
`"abcde".Replace("a","x").Replace("b","y")・・・`とつなげてもいいです。
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
あと置換の度にファイルに保存して、また読み込んで、を繰り返すのは無駄なので変数内で繰り返し置換したあとに最後に保存するとよいと思います。
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
最後に全体的に手直しして少し見通しをよくしたコードを置いて置きます。まったくテストしてないのでバグがあったらすみません。ご参考までに。
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
```PowerShell
|
96
|
-
|
97
|
-
$ip = "..."
|
98
|
-
|
99
|
-
$logPath = "c:\zabbix\zabbix_agentd.log"
|
100
|
-
|
101
|
-
$metaData = "windows2008"
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
##### ここから本文 ######
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
$zipPath = [Environment]::GetFolderPath("Desktop") | Join-Path -ChildPath zabbix_agents.zip
|
110
|
-
|
111
|
-
$destDir = "C:\zabbix"
|
112
|
-
|
113
|
-
$exeName = "zabbix_agentd.exe"
|
114
|
-
|
115
|
-
$confName = "zabbix_agentd.conf"
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
#confファイル置換用ハッシュテーブル
|
120
|
-
|
121
|
-
$h = @{
|
122
|
-
|
123
|
-
"LogFile=c:\zabbix_agentd.log" = "LogFile=$logPath"
|
124
|
-
|
125
|
-
"Server=127.0.0.1" = "Server=$ip"
|
126
|
-
|
127
|
-
"ServerActive=127.0.0.1" = "ServerActive=$ip"
|
128
|
-
|
129
|
-
"Hostname=Windows host" = "Hostname=" + [Net.Dns]::GetHostName()
|
130
|
-
|
131
|
-
"# HostMetadata=" = "# HostMetadata=rnrnHostMetadata=$metaData"
|
132
|
-
|
133
|
-
}
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
#スクリプト出力ログ
|
138
|
-
|
139
|
-
$scriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
|
140
|
-
|
141
|
-
Write-Host $scriptRoot
|
142
|
-
|
143
|
-
Start-Transcript -Path "$scriptRoot/zabbixAgentインストール.log"
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
#### 処理開始 ####
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
#フォルダ作成
|
152
|
-
|
153
|
-
New-Item $destDir -ItemType Directory
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
#zipファイル移動
|
158
|
-
|
159
|
-
$zipFile = Move-Item $zipPath $destDir -PassThru
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
#解凍
|
164
|
-
|
165
|
-
$shell = New-Object -ComObject shell.application
|
166
|
-
|
167
|
-
$srcNS = $shell.NameSpace($zipFile.FullName)
|
168
|
-
|
169
|
-
$destNS = $shell.NameSpace($zipFile.DirectoryName)
|
170
|
-
|
171
|
-
$destNS.CopyHere($srcNS.Items())
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
#exeファイル配置
|
176
|
-
|
177
|
-
$exeFile = Move-Item "$destDir/bin/$exeName" $destDir -PassThru
|
178
|
-
|
179
|
-
#confフォルダ配置
|
180
|
-
|
181
|
-
$confFile = Move-Item "$destDir/conf/$confName" $destDir -PassThru
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
#confファイル書き換え
|
186
|
-
|
187
|
-
$conf = $confFile | Get-Content -Encoding utf8
|
188
|
-
|
189
|
-
$h.GetEnumerator() | foreach { $conf = $conf.Replace($_.Key, $_.Value) }
|
190
|
-
|
191
183
|
$conf | Out-File $confFile -Encoding utf8
|
192
184
|
|
193
185
|
|
194
186
|
|
195
|
-
|
196
|
-
|
197
187
|
#ZabbixAgentサービス開始
|
198
188
|
|
199
189
|
& $exeFile -i -c $confFile
|
@@ -216,7 +206,7 @@
|
|
216
206
|
|
217
207
|
#zipファイル,binフォルダ,confフォルダ削除
|
218
208
|
|
219
|
-
$zip
|
209
|
+
$zipPath,"$destDir/bin","$destDir/conf" | Remove-Item -Recurse
|
220
210
|
|
221
211
|
|
222
212
|
|
1
微修正
test
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
`-replace`演算子は正規表現によって置換します。
|
2
2
|
|
3
|
-
正規表現においてカンマ`.`やバックスラッシュ`\`などは特別な意味をもつので文字通りにマッチしなかったのが原因
|
3
|
+
正規表現においてカンマ`.`やバックスラッシュ`\`などは特別な意味をもつので文字通りにマッチしなかったのが原因かと思われます。
|
4
4
|
|
5
5
|
|
6
6
|
|
@@ -14,7 +14,7 @@
|
|
14
14
|
|
15
15
|
```
|
16
16
|
|
17
|
-
特別な意味をもつ記号
|
17
|
+
特別な意味をもつ記号をエスケープしてやればマッチします。
|
18
18
|
|
19
19
|
```PowerShell
|
20
20
|
|
@@ -38,7 +38,7 @@
|
|
38
38
|
|
39
39
|
|
40
40
|
|
41
|
-
ただしこのような正規表現が必要ない
|
41
|
+
ただしこのような正規表現が必要ない場合は`-replace`演算子を使うと無駄にコストがかかるので
|
42
42
|
|
43
43
|
`"文字列".Replace("置換前", "置換後")`メソッドを使って単純な置換を行うのが普通です。
|
44
44
|
|
@@ -74,122 +74,122 @@
|
|
74
74
|
|
75
75
|
$h.GetEnumerator() | foreach { $conf = $conf.Replace($_.Key, $_.Value) }
|
76
76
|
|
77
|
+
$conf | Out-File sample.conf -Encoding utf8
|
78
|
+
|
79
|
+
```
|
80
|
+
|
81
|
+
この辺りはいろいろなやり方があると思うので自分が良いと思う方法でやってください。単純に
|
82
|
+
|
83
|
+
`"abcde".Replace("a","x").Replace("b","y")・・・`とつなげてもいいです。
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
あと置換の度にファイルに保存して、また読み込んで、を繰り返すのは無駄なので変数内で繰り返し置換したあとに最後に保存するとよいと思います。
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
最後に全体的に手直しして少し見通しをよくしたコードを置いて置きます。まったくテストしてないのでバグがあったらすみません。ご参考までに。
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
```PowerShell
|
96
|
+
|
97
|
+
$ip = "..."
|
98
|
+
|
99
|
+
$logPath = "c:\zabbix\zabbix_agentd.log"
|
100
|
+
|
101
|
+
$metaData = "windows2008"
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
##### ここから本文 ######
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
$zipPath = [Environment]::GetFolderPath("Desktop") | Join-Path -ChildPath zabbix_agents.zip
|
110
|
+
|
111
|
+
$destDir = "C:\zabbix"
|
112
|
+
|
113
|
+
$exeName = "zabbix_agentd.exe"
|
114
|
+
|
115
|
+
$confName = "zabbix_agentd.conf"
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
#confファイル置換用ハッシュテーブル
|
120
|
+
|
121
|
+
$h = @{
|
122
|
+
|
123
|
+
"LogFile=c:\zabbix_agentd.log" = "LogFile=$logPath"
|
124
|
+
|
125
|
+
"Server=127.0.0.1" = "Server=$ip"
|
126
|
+
|
127
|
+
"ServerActive=127.0.0.1" = "ServerActive=$ip"
|
128
|
+
|
129
|
+
"Hostname=Windows host" = "Hostname=" + [Net.Dns]::GetHostName()
|
130
|
+
|
131
|
+
"# HostMetadata=" = "# HostMetadata=rnrnHostMetadata=$metaData"
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
#スクリプト出力ログ
|
138
|
+
|
139
|
+
$scriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
|
140
|
+
|
141
|
+
Write-Host $scriptRoot
|
142
|
+
|
143
|
+
Start-Transcript -Path "$scriptRoot/zabbixAgentインストール.log"
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
#### 処理開始 ####
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
#フォルダ作成
|
152
|
+
|
153
|
+
New-Item $destDir -ItemType Directory
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
#zipファイル移動
|
158
|
+
|
159
|
+
$zipFile = Move-Item $zipPath $destDir -PassThru
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
#解凍
|
164
|
+
|
165
|
+
$shell = New-Object -ComObject shell.application
|
166
|
+
|
167
|
+
$srcNS = $shell.NameSpace($zipFile.FullName)
|
168
|
+
|
169
|
+
$destNS = $shell.NameSpace($zipFile.DirectoryName)
|
170
|
+
|
171
|
+
$destNS.CopyHere($srcNS.Items())
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
#exeファイル配置
|
176
|
+
|
177
|
+
$exeFile = Move-Item "$destDir/bin/$exeName" $destDir -PassThru
|
178
|
+
|
179
|
+
#confフォルダ配置
|
180
|
+
|
181
|
+
$confFile = Move-Item "$destDir/conf/$confName" $destDir -PassThru
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
#confファイル書き換え
|
186
|
+
|
187
|
+
$conf = $confFile | Get-Content -Encoding utf8
|
188
|
+
|
189
|
+
$h.GetEnumerator() | foreach { $conf = $conf.Replace($_.Key, $_.Value) }
|
190
|
+
|
77
191
|
$conf | Out-File $confFile -Encoding utf8
|
78
192
|
|
79
|
-
```
|
80
|
-
|
81
|
-
この辺りはいろいろなやり方があると思うので自分が良いと思う方法でやってください。単純に
|
82
|
-
|
83
|
-
`"abcde".Replace("a","x").Replace("b","y")・・・`とつなげてもいいです。
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
あと置換の度にファイルに保存して、また読み込んで、を繰り返すのは無駄なので変数内で繰り返し置換したあとに最後に保存するとよいと思います。
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
最後に全体的に手直しして少し見通しをよくしたコードを置いて置きます。まったくテストしてないのでバグがあったらすみません。ご参考までに。
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
```PowerShell
|
96
|
-
|
97
|
-
$ip = "..."
|
98
|
-
|
99
|
-
$logPath = "c:\zabbix\zabbix_agentd.log"
|
100
|
-
|
101
|
-
$metaData = "windows2008"
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
##### ここから本文 ######
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
$zipPath = [Environment]::GetFolderPath("Desktop") | Join-Path -ChildPath zabbix_agents.zip
|
110
|
-
|
111
|
-
$destDir = "C:\zabbix"
|
112
|
-
|
113
|
-
$exeName = "zabbix_agentd.exe"
|
114
|
-
|
115
|
-
$confName = "zabbix_agentd.conf"
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
#confファイル置換用ハッシュテーブル
|
120
|
-
|
121
|
-
$h = @{
|
122
|
-
|
123
|
-
"LogFile=c:\zabbix_agentd.log" = "LogFile=$logPath"
|
124
|
-
|
125
|
-
"Server=127.0.0.1" = "Server=$ip"
|
126
|
-
|
127
|
-
"ServerActive=127.0.0.1" = "ServerActive=$ip"
|
128
|
-
|
129
|
-
"Hostname=Windows host" = "Hostname=" + [Net.Dns]::GetHostName()
|
130
|
-
|
131
|
-
"# HostMetadata=" = "# HostMetadata=rnrnHostMetadata=$metaData"
|
132
|
-
|
133
|
-
}
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
#スクリプト出力ログ
|
138
|
-
|
139
|
-
$scriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
|
140
|
-
|
141
|
-
Write-Host $scriptRoot
|
142
|
-
|
143
|
-
Start-Transcript -Path "$scriptRoot/zabbixAgentインストール.log"
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
#### 処理開始 ####
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
#フォルダ作成
|
152
|
-
|
153
|
-
New-Item $destDir -ItemType Directory
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
#zipファイル移動
|
158
|
-
|
159
|
-
$zipFile = Move-Item $zipPath $destDir -PassThru
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
#解凍
|
164
|
-
|
165
|
-
$shell = New-Object -ComObject shell.application
|
166
|
-
|
167
|
-
$srcNS = $shell.NameSpace($zipFile.FullName)
|
168
|
-
|
169
|
-
$destNS = $shell.NameSpace($zipFile.DirectoryName)
|
170
|
-
|
171
|
-
$destNS.CopyHere($srcNS.Items())
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
#exeファイル配置
|
176
|
-
|
177
|
-
$exeFile = Move-Item "$destDir/bin/$exeName" $destDir -PassThru
|
178
|
-
|
179
|
-
#confフォルダ配置
|
180
|
-
|
181
|
-
$confFile = Move-Item "$destDir/conf/$confName" $destDir -PassThru
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
#confファイル書き換え
|
186
|
-
|
187
|
-
$conf = $confFile | Get-Content -Encoding utf8
|
188
|
-
|
189
|
-
$h.GetEnumerator() | foreach { $conf = $conf.Replace($_.Key, $_.Value) }
|
190
|
-
|
191
|
-
$conf | Out-File $confFile -Encoding utf8
|
192
|
-
|
193
193
|
|
194
194
|
|
195
195
|
|