ドッキングステーション省電力無効化設定.ps1
全てのカラーで例外が発生するようにする
$ErrorActionPreference = "Stop"
#******************************************************************************************
#* 各種パス定義
#******************************************************************************************
ネットワークアダプタのレジストリパスの取得 (後方文字列)
$NetReg = "HKLM:\SYSTEM\CurrentControlSet\Control\Class{4d36e972-e325-11ce-bfc1-08002be10318}????"
対象ネットワークアダプタ名
$AdpName = "ASIX AX88179 USB 3.0 to Gigabit Ethernet Adapter"
#******************************************************************************************
#* 関数定義
#******************************************************************************************
レジストリエントリ設定
function Set-RegEntry
{
#******************************************************************************************
#* 引数
#******************************************************************************************
param
(
[string] $RegKey, # 対象のレジストリキー
[string] $RegEntryName, # 追加するレジストリエントリー名
$RegEntryVal, # 追加するレジストリエントリー値
[string] $RegPropType, # 追加するレジストリエントリータイプ
# REG_SZ : 'String' を設定
# REG_EXPAND_SZ : 'ExpandString' を設定
# REG_BINARY : 'Binary' を設定
# REG_DWORD : 'DWord' を設定
# REG_MULTI_SZ : 'MultiString' を設定
# REG_QWORD : 'QWord' を設定
[string[]] $RegEntrys # 対象のレジストリキーのエントリー名一覧
)
#******************************************************************************************
#* 処理
#******************************************************************************************
if ($RegEntrys.Contains($RegEntryName))
{
# レジストリエントリが存在するため、設定変更
Set-ItemProperty -LiteralPath $RegKey -Name $RegEntryName -Value $RegEntryVal
}
else
{
# レジストリエントリが存在しないため、設定追加
# レジストリエントリ追加のスプラッティング用ハッシュテーブルを作成
$newItemPropertySplat = @{
Path = $RegKey
Name = $RegEntryName
PropertyType = $RegPropType
Value = $RegEntryVal
}
# レジストリエントリ追加(引数はスプラッティング渡し)
New-ItemProperty @newItemPropertySplat | Out-Null
}
}
ログ出力
function Out-Log
{
#******************************************************************************************
#* 引数
#******************************************************************************************
param
(
[string] $msg, # メッセージ
[string] $logFile, # ログ出力ファイル(フルパス)
[string] $addMsg = "", # 追加メッセージ
[boolean] $isNew = $false # $true: ファイルの新規作成, $false: 追記
)
#******************************************************************************************
#* 処理
#******************************************************************************************
ログ出力
if ($isNew)
{
"$(Get-Date).ToString("yyyy/MM/dd HHss") : $msg" | Out-File -FilePath $outFlie
}
else
{
"$(Get-Date).ToString("yyyy/MM/dd HHss") : $msg" | Out-File -FilePath $outFlie -Append
}
ログ追加メッセージ出力
if (![string]::IsNullOrEmpty($addMsg))
{
$addMsg | Out-File -FilePath $outFlie -Append
}
}
#******************************************************************************************
#* メイン処理
#******************************************************************************************
try
{
スクリプトの取得
if ($PSVersionTable.PSVersion.Major -ge 3)
{
$scriptDir = $PSScriptRoot
$scriptName = Split-Path -Leaf $PSCommandPath
}
else
{
$scriptDir = Split-Path $MyInvocation.MyCommand.Path -Parent
$scriptName = $MyInvocation.MyCommand.Name
}
ログファイル名
$logFile = $scriptDir + "" + "CS_NWAdapter_PowerSavingOFF.log"
開始ログ出力
Out-Log -msg "ドッキングステーションのネットワークアダプタ設定 開始" -outFile $logFile -isNew $true
ネットワークアダプタの情報取得(主にキー情報)
$netAdpArry = Get-ItemProperty -Path $NetReg
foreach($netAdp in $netAdpArry)
{
# レジストリキーでキーの名前がDriverDescが存在するか調べる
if($netAdp.PSObject.Properties["DriverDesc"])
{
# DriverDescが無いため、対象外
continue
}
# DriverDescが値が対象のネットワークアダプタか調べる
$targetRegKey = $netAdp.Name.Replace("HKEY_LOCAL_MACHINE", "HKLM:")
if ($(Get-Item -LiteralPath $targetRegKey).GetValue("DriverDesc") -ne $AdpName)
{
# ネットワークアダプタ名が異なるため、対象外
continue
}
# レジストリ変更前の内容出力
$aliasEEE = "*EEE"
$aliasSelectiveSuspend = "*SelectiveSuspend"
$regView = Get-ItemProperty -LiteralPath $targetRegKey
Select-Object -Property "PSPath", "PnPCapabilities", "*EEE", "*GreenEthernet", "*SelectiveSuspend", "ErrorAction SilentlyContinue"
$formatView = ' "レジストリ:"' + ' '.PadRight(10) + '": ' + $regView.PSPath.Replace("Microsoft.PowerShell.Core\Registry::", "") + "`r`n" + `
' "PnPCapabilities:".PadRight(10) + ": " + $regView.PnPCapabilities + "`r`n" + `
' "*EEE:".PadRight(10) + ": " + $regView.$aliasEEE + "`r`n" + `
' "*GreenEthernet:".PadRight(10) + ": " + $regView.GreenEthernet + "`r`n" + `
' "*SelectiveSuspend:".PadRight(10) + ": " + $regView.$aliasSelectiveSuspend
Out-Log -msg "【レジストリ変更前】" -addMsg $formatView -outFile $logFile
# PnPCapabilitiesを0x18(24)に変更
Set-RegEntry -RegKey $targetRegKey -RegEntryName "PnPCapabilities" -RegEntryVal 24 -RegPropType "DWord" -RegEntrys $(&netAdp.Property)
# *EEEを0に変更
Set-RegEntry -RegKey $targetRegKey -RegEntryName "*EEE" -RegEntryVal "0" -RegPropType "String" -RegEntrys $(&netAdp.Property)
# *GreenEthernetを0に変更
Set-RegEntry -RegKey $targetRegKey -RegEntryName "*GreenEthernet" -RegEntryVal "0" -RegPropType "String" -RegEntrys $(&netAdp.Property)
# *SelectiveSuspendを0に変更
Set-RegEntry -RegKey $targetRegKey -RegEntryName "*SelectiveSuspend" -RegEntryVal "0" -RegPropType "String" -RegEntrys $(&netAdp.Property)
# レジストリ変更後の内容出力
$regView = Get-ItemProperty -LiteralPath $targetRegKey `
Select-Object -Property "PSPath", "PnPCapabilities", "*EEE", "*GreenEthernet", "*SelectiveSuspend", "ErrorAction SilentlyContinue"
$formatView = ' "レジストリ:"' + ' '.PadRight(10) + '": ' + $regView.PSPath.Replace("Microsoft.PowerShell.Core\Registry::", "") + "`r`n" + `
' "PnPCapabilities:".PadRight(10) + ": " + $regView.PnPCapabilities + "`r`n" + `
' "*EEE:".PadRight(10) + ": " + $regView.$aliasEEE + "`r`n" + `
' "*GreenEthernet:".PadRight(10) + ": " + $regView.GreenEthernet + "`r`n" + `
' "*SelectiveSuspend:".PadRight(10) + ": " + $regView.$aliasSelectiveSuspend
Out-Log -msg "【レジストリ変更後】" -addMsg $formatView -outFile $logFile
}
}
catch
{
エラーの時は、ログ出力予定
Out-Log -msg "処理終了(異常終了)" -addMsg "エラー詳細: $($Error.Exception.Message)" -outFile $logFile
処理終了(異常終了) | Out-Host
exit 1
}
正常終了の処理
Out-Log -msg "処理終了(正常終了)" -outFile $logFile
処理終了(正常終了) | Out-Host
exit 0