-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet-NetworkConfig.ps1
More file actions
176 lines (152 loc) · 7.05 KB
/
Copy pathSet-NetworkConfig.ps1
File metadata and controls
176 lines (152 loc) · 7.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<#
.SYNOPSIS
ネットワークインターフェースのメトリクスとプロファイルを変更するGUIツールじゃ。
.DESCRIPTION
現在のネットワークアダプタ一覧を取得し、GUI(表形式)で表示する。
ユーザーはメトリクス値とネットワークカテゴリ(Public/Private)を編集し、
「適用」ボタンを押すことで一括変更できるのじゃ。
.PARAMETER h
ヘルプを表示するのじゃ。
.PARAMETER help
ヘルプを表示するのじゃ。
.EXAMPLE
.\Set-NetworkConfig.ps1
GUIを起動する。
.EXAMPLE
.\Set-NetworkConfig.ps1 -h
ヘルプを表示する。
#>
[CmdletBinding()]
param(
[switch]$h,
[switch]$help
)
#region Help Section
if ($h -or $help) {
Get-Help $PSCommandPath -Full
exit
}
#endregion
function Invoke-SelfElevation {
param(
[string[]]$ArgumentList
)
$sudoCommand = Get-Command sudo -ErrorAction SilentlyContinue
if ($sudoCommand) {
$allArgs = @('powershell.exe') + $ArgumentList
& $sudoCommand @allArgs
exit
}
$gsudoCommand = Get-Command gsudo -ErrorAction SilentlyContinue
if ($gsudoCommand) {
$allArgs = @('powershell.exe') + $ArgumentList
& $gsudoCommand @allArgs
exit
}
Start-Process powershell.exe -ArgumentList $ArgumentList -Verb RunAs
exit
}
# 管理者権限チェック
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Warning "これを使うには管理者権限が必要じゃ!出直してまいれ。"
$relaunchArguments = @('-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', $PSCommandPath)
foreach ($entry in $PSBoundParameters.GetEnumerator()) {
$relaunchArguments += "-$($entry.Key)"
if ($entry.Value -is [System.Management.Automation.SwitchParameter]) {
continue
}
$relaunchArguments += [string]$entry.Value
}
Invoke-SelfElevation -ArgumentList $relaunchArguments
}
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# フォーム作成
$form = New-Object System.Windows.Forms.Form
$form.Text = "ネットワーク設定変更ツール - 改修版"
$form.Size = New-Object System.Drawing.Size(600, 400)
$form.StartPosition = "CenterScreen"
# グリッドビュー作成
$grid = New-Object System.Windows.Forms.DataGridView
$grid.Size = New-Object System.Drawing.Size(560, 300)
$grid.Location = New-Object System.Drawing.Point(10, 10)
$grid.Anchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left -bor [System.Windows.Forms.AnchorStyles]::Right -bor [System.Windows.Forms.AnchorStyles]::Bottom
$grid.AllowUserToAddRows = $false
$grid.RowHeadersVisible = $false
$grid.AutoSizeColumnsMode = [System.Windows.Forms.DataGridViewAutoSizeColumnsMode]::Fill
# カラム定義
$colName = New-Object System.Windows.Forms.DataGridViewTextBoxColumn
$colName.Name = "Name"
$colName.HeaderText = "アダプタ名"
$colName.ReadOnly = $true
$grid.Columns.Add($colName) | Out-Null
$colIdx = New-Object System.Windows.Forms.DataGridViewTextBoxColumn
$colIdx.Name = "Index"
$colIdx.HeaderText = "Index"
$colIdx.ReadOnly = $true
$colIdx.Visible = $false
$grid.Columns.Add($colIdx) | Out-Null
$colMetric = New-Object System.Windows.Forms.DataGridViewTextBoxColumn
$colMetric.Name = "Metric"
$colMetric.HeaderText = "メトリクス (自動=0)"
$grid.Columns.Add($colMetric) | Out-Null
$colProfile = New-Object System.Windows.Forms.DataGridViewComboBoxColumn
$colProfile.Name = "Profile"
$colProfile.HeaderText = "プロファイル"
$colProfile.Items.Add("Public") | Out-Null
$colProfile.Items.Add("Private") | Out-Null
$colProfile.Items.Add("DomainAuthenticated") | Out-Null
$grid.Columns.Add($colProfile) | Out-Null
# データ取得と投入(ここを修正したぞ)
$adapters = Get-NetAdapter | Where-Object { $_.Status -eq 'Up' }
foreach ($adapter in $adapters) {
# エラーが出ても止まらぬよう SilentlyContinue を追加じゃ
$ipIf = Get-NetIPInterface -InterfaceIndex $adapter.InterfaceIndex -AddressFamily IPv4 -ErrorAction SilentlyContinue
$connProfile = Get-NetConnectionProfile -InterfaceIndex $adapter.InterfaceIndex -ErrorAction SilentlyContinue
$row = $grid.Rows.Add()
$grid.Rows[$row].Cells["Name"].Value = $adapter.Name
$grid.Rows[$row].Cells["Index"].Value = $adapter.InterfaceIndex
# ipIfが取れなかった場合は空欄にする
$grid.Rows[$row].Cells["Metric"].Value = if ($ipIf) { $ipIf.InterfaceMetric } else { "" }
$grid.Rows[$row].Cells["Profile"].Value = if ($connProfile) { $connProfile.NetworkCategory.ToString() } else { "" }
}
# 適用ボタン
$btnApply = New-Object System.Windows.Forms.Button
$btnApply.Text = "変更を適用する"
$btnApply.Size = New-Object System.Drawing.Size(120, 30)
$btnApply.Location = New-Object System.Drawing.Point(450, 320)
$btnApply.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right
$btnApply.Add_Click({
foreach ($row in $grid.Rows) {
$idx = $row.Cells["Index"].Value
$metric = $row.Cells["Metric"].Value
$cat = $row.Cells["Profile"].Value
try {
if ($idx) {
# メトリクスの適用
if ($metric -match "^\d+$") {
# IPv4インターフェースがあるか確認してからセットする
$targetIf = Get-NetIPInterface -InterfaceIndex $idx -AddressFamily IPv4 -ErrorAction SilentlyContinue
if ($targetIf) {
Set-NetIPInterface -InterfaceIndex $idx -InterfaceMetric $metric -ErrorAction Stop
}
}
# プロファイルの適用
if ($cat -and $cat -ne "") {
$currentProfile = Get-NetConnectionProfile -InterfaceIndex $idx -ErrorAction SilentlyContinue
if ($currentProfile -and $currentProfile.NetworkCategory -ne $cat) {
Set-NetConnectionProfile -InterfaceIndex $idx -NetworkCategory $cat -ErrorAction Stop
}
}
}
}
catch {
[System.Windows.Forms.MessageBox]::Show("エラーじゃ! アダプタ: $($row.Cells["Name"].Value)`n$($_.Exception.Message)", "警告", 0, 48)
}
}
[System.Windows.Forms.MessageBox]::Show("処理が完了したぞ。", "完了", 0, 64)
# フォームを再描画して値を更新したいならここで再取得処理を入れるが、今回は閉じるまでそのままとする
})
$form.Controls.Add($grid)
$form.Controls.Add($btnApply)
$form.ShowDialog() | Out-Null