回答編集履歴

3

修正

2024/07/08 10:19

投稿

mike2mike4
mike2mike4

スコア934

test CHANGED
@@ -1,32 +1,33 @@
1
- ChatGPTに依頼たら以下が提示れました
1
+ 再挑戦。以下をPowerShellの管理者権限で実行再起動してみてくだ
2
2
  ```PowerShell
3
- # 管理者権限で実行されていることを確認
3
+ # 管理者権限確認と昇格
4
- <# :
5
- @echo off
6
- openfiles >nul 2>&1
7
- if errorlevel 1 (
8
- powershell start-process "'%~f0'" -verb runas
4
+ if (!([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
9
- goto :EOF
10
- )
5
+ {
11
- powershell -noprofile -command "invoke-expression (get-content '%~f0' -raw)"
6
+ $arguments = "& '" + $myInvocation.MyCommand.Definition + "'"
12
- goto :EOF
7
+ Start-Process powershell -Verb runas -ArgumentList $arguments
13
- #>
8
+ Exit
14
-
15
- # PowerShellスクリプト開始
16
- $adapter = Get-NetAdapter | Where-Object {$_.InterfaceAlias -eq "Wi-Fi"}
17
-
18
- # IPアドレスとデフォルトゲートウェイの設定をクリア
19
- $conf = $adapter | Get-NetIPConfiguration
20
- If ($conf.IPv4Address.IPAddress) {
21
- $adapter | Remove-NetIPAddress -AddressFamily IPv4 -Confirm:$false
22
- }
23
- If ($conf.Ipv4DefaultGateway) {
24
- $adapter | Remove-NetRoute -AddressFamily IPv4 -Confirm:$false
25
9
  }
26
10
 
11
+ # ネットワークアダプタの取得
12
+ $wifiAdapter = Get-NetAdapter | Where-Object {$_.InterfaceDescription -match "Wi-Fi"}
13
+ $ethernetAdapter = Get-NetAdapter | Where-Object {$_.InterfaceDescription -match "Ethernet"}
14
+
15
+ # Wi-Fiアダプタの確認
16
+ if ($wifiAdapter) {
17
+ Write-Output "Wi-Fi adapter found: $($wifiAdapter.Name)"
27
- # DHCPと自動DNS取得を有効化
18
+ # DHCPと自動DNS取得を有効化
28
- $adapter | Set-NetIPInterface -DHCP Enabled
19
+ $wifiAdapter | Set-NetIPInterface -DHCP Enabled
29
- $adapter | Set-DnsClientServerAddress -InterfaceAlias "Wi-Fi" -ResetServerAddresses
20
+ $wifiAdapter | Set-DnsClientServerAddress -ResetServerAddresses
21
+ Write-Output "DHCP and DNS set to automatic for Wi-Fi"
22
+ } elseif ($ethernetAdapter) {
23
+ Write-Output "Wi-Fi adapter not found. Ethernet adapter found: $($ethernetAdapter.Name)"
24
+ # DHCPと自動DNS取得を有効化
25
+ $ethernetAdapter | Set-NetIPInterface -DHCP Enabled
26
+ $ethernetAdapter | Set-DnsClientServerAddress -ResetServerAddresses
27
+ Write-Output "DHCP and DNS set to automatic for Ethernet"
28
+ } else {
29
+ Write-Error "No Wi-Fi or Ethernet adapter found"
30
+ }
30
31
 
31
32
  ```
32
- この後再起動てみてください。
33
+ 一応、wifiとイーサネット両方に対応ましたが、どちらでしょうか?

2

修正

2024/07/08 01:22

投稿

mike2mike4
mike2mike4

スコア934

test CHANGED
@@ -1,5 +1,6 @@
1
1
  ChatGPTに依頼したら以下が提示されました。
2
2
  ```PowerShell
3
+ # 管理者権限で実行されていることを確認
3
4
  <# :
4
5
  @echo off
5
6
  openfiles >nul 2>&1
@@ -11,39 +12,21 @@
11
12
  goto :EOF
12
13
  #>
13
14
 
14
- # 以下 PowerShell スクリプト
15
+ # PowerShellスクリプト開始
15
16
  $adapter = Get-NetAdapter | Where-Object {$_.InterfaceAlias -eq "Wi-Fi"}
16
17
 
17
- # アダプターのコンフィグ取得
18
+ # IPドレスとデフォルトゲトウェイ設定クリア
18
19
  $conf = $adapter | Get-NetIPConfiguration
19
-
20
- # IPアドレスが設定されている場合
21
20
  If ($conf.IPv4Address.IPAddress) {
22
- # その設定を削除する
23
21
  $adapter | Remove-NetIPAddress -AddressFamily IPv4 -Confirm:$false
24
22
  }
25
-
26
- # デフォルトゲートウェイが設定されている場合
27
23
  If ($conf.Ipv4DefaultGateway) {
28
- # その設定を削除する
29
24
  $adapter | Remove-NetRoute -AddressFamily IPv4 -Confirm:$false
30
25
  }
31
26
 
32
- # 以下が追加した
33
-
34
- $IPType = "IPv4"
35
- $adapter = Get-NetAdapter | Where-Object {$_.Status -eq "Up"}
36
- $interface = $adapter | Get-NetIPInterface -AddressFamily $IPType
37
-
38
- If ($interface.Dhcp -eq $false) {
39
- # Remove existing gateway
40
- If (($interface | Get-NetIPConfiguration).Ipv4DefaultGateway) {
41
- $interface | Remove-NetRoute -Confirm:$false
42
- }
43
- # Enable DHCP
27
+ # DHCPと自動DNS取得を有効化
44
- $interface | Set-NetIPInterface -DHCP Enabled
28
+ $adapter | Set-NetIPInterface -DHCP Enabled
45
- # Configure the DNS Servers automatically
46
- $interface | Set-DnsClientServerAddress -ResetServerAddresses
29
+ $adapter | Set-DnsClientServerAddress -InterfaceAlias "Wi-Fi" -ResetServerAddresses
47
- }
48
30
 
49
31
  ```
32
+ この後再起動してみてください。

1

訂正

2024/07/05 11:01

投稿

mike2mike4
mike2mike4

スコア934

test CHANGED
@@ -1,4 +1,4 @@
1
- ChatGPTに自動生成されたら以下が提示されました。
1
+ ChatGPTに依頼したら以下が提示されました。
2
2
  ```PowerShell
3
3
  <# :
4
4
  @echo off