NetworkingDsc
NetworkingDsc copied to clipboard
Need Network Power Settings
Need the ability to disable power saving mode on NIC.
I use this function Get-TargetResource { [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param ( [parameter(Mandatory = $true)] [System.String] $ServerName )
#Write-Verbose "Use this cmdlet to deliver information about command processing."
#Write-Debug "Use this cmdlet to write debug information while troubleshooting."
$nic = Get-WmiObject Win32_NetworkAdapter | where {$_.AdapterType -eq 'Ethernet 802.3'}
$powerMgmt = Get-WmiObject MSPower_DeviceEnable -Namespace root\wmi | where {$_.InstanceName.ToUpper().Contains($nic.PNPDeviceID)}
$returnValue = @{
ServerName = $ServerName
NICPowerSaving = $powerMgmt.Enable
}
$returnValue
}
function Set-TargetResource { [CmdletBinding()] param ( [parameter(Mandatory = $true)] [System.String] $ServerName,
[ValidateSet("Present","Absent")]
[System.String]
$Ensure
)
Write-Verbose "Setting the NIC power Management setting."
#Write-Debug "Use this cmdlet to write debug information while troubleshooting."
Write-Verbose "Configuring the SQL alerts ."
If ($Ensure -eq 'Present'){
DisableNICPowerManagement -ServerName $ServerName
}ELSE{
EnableNICPowerManagement -ServerName $ServerName -InstanceName $InstanceName -OperatorName $OperatorName -NotificationMethod $NotificationMethod
}
}
function Test-TargetResource { [CmdletBinding()] [OutputType([System.Boolean])] param ( [parameter(Mandatory = $true)] [System.String] $ServerName,
[ValidateSet("Present","Absent")]
[System.String]
$Ensure
)
Write-Verbose "Checking to see if the power setting on the NIC."
#Write-Debug "Use this cmdlet to write debug information while troubleshooting."
$nic = Get-WmiObject Win32_NetworkAdapter | where {$_.AdapterType -eq 'Ethernet 802.3'}
$powerMgmt = Get-WmiObject MSPower_DeviceEnable -Namespace root\wmi | where {$_.InstanceName.ToUpper().Contains($nic.PNPDeviceID)}
If ($Ensure -eq 'Present'){
If ($powerMgmt.Enable -eq $false){
Write-Verbose "NIC Power Management setting is disabled."
$result = $true
}ELSE{
Write-Verbose "NIC Power Management setting is not disabled. - Disabling"
$result = $false
}
}
If ($Ensure -eq 'Absent'){
If ($powerMgmt.Enable -eq $false) {
Write-Verbose "NIC Power Management setting is disabled. - Enabling."
$result = $false
}ELSE{
Write-Verbose "NIC Power Management setting is not disabled."
$result = $true
}
}
$result
}
Export-ModuleMember -Function *-TargetResource
Function DisableNICPowerManagement{ param ($ServerName) Write-Verbose "Disabling the NIC Power Management setting." $nics = Get-WmiObject Win32_NetworkAdapter | where {$_.AdapterType -eq 'Ethernet 802.3'}
foreach ($nic in $nics)
{
$powerMgmt = Get-WmiObject MSPower_DeviceEnable -Namespace root\wmi | where {$_.InstanceName.ToUpper().Contains($nic.PNPDeviceID)}
$powerMgmt.Enable = $False
$powerMgmt.psbase.Put() | Out-Null
}
} #DisableNICPowerManagement -ServerName 'SSVdbOlyTest01'
Function EnableNICPowerManagement{ param ($ServerName)
Write-Verbose "Enabling the NIC Power Management setting."
$nics = Get-WmiObject Win32_NetworkAdapter | where {$_.AdapterType -eq 'Ethernet 802.3'}
foreach ($nic in $nics)
{
$powerMgmt = Get-WmiObject MSPower_DeviceEnable -Namespace root\wmi | where {$_.InstanceName.ToUpper().Contains($nic.PNPDeviceID)}
$powerMgmt.Enable = $True
$powerMgmt.psbase.Put() | Out-Null
}
} #EnableNICPowerManagement -ServerName 'SSVdbOlyTest01'
Hi @edycus - this looks like a pretty good resource to me!
I would have one suggestion: The $ServerName parameter doesn't look like it is actually needed or required. I'm thinking this param exists because there always needs to be at least one key? If so, recommended method for handling this is to use the IsSingleInstance param. See this document for more info - it wouldn't take much to tweak your code to use this method.
I might also suggest that rather than have two functions for disabling and enabling Power management it might be better to have one that you can pass in an -Enabled:$true or -Enabled:$false parameter (it would result in less code duplication) - but that is only a minor suggestion.
One other question: Is it possible or desirable to disable/enable power on other types of adapters? E.g. not just 802.3? E.g. 802.11? Just throwing this out there.
But either way this sounds like a great resource to me and shouldn't be hard to add in.
Would you feel like having a go at submitting a PR for this? If you do want to have a go at a PR, I can help give you some pointers on including tests and other bits that need to go in with all resources. Otherwise someone else will get to it - although this can take a bit of time (depends on how busy the community is :grin: )
Yea, that would great and I would love to help out in anyway that I can. This sounds like a great way to help out the community and improve my skills as well.
I didn't mean to close it. Oppsss
You are right the $ServerName is not currently used the idea was maybe in the verbose message would have the servername in it which never happened. I will remove it. I looked at the IsSingleInstance that didn't fit. I want to allow multiple uses of the module for different items. I will make AdapterType the key (Ethernet 802.3) . The discussion I would like to have in regards to the 802.11 is that is wireless. If for terrible reason you are running this on wireless NIC then you may need to keep the power setting on the NIC adapter. Assuming a laptop or some other portable battery driven device. I like the idea on one Function to cut down on code but I want it to be very easy to read. How do I submit a PR?
Here is my cleaned up version of the code.
function Get-TargetResource { [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param ( [parameter(Mandatory = $true)] [System.String] $AdapterType )
#Write-Verbose "Use this cmdlet to deliver information about command processing."
#Write-Debug "Use this cmdlet to write debug information while troubleshooting."
$nic = Get-WmiObject Win32_NetworkAdapter | where {$_.AdapterType -eq 'Ethernet 802.3'}
$powerMgmt = Get-WmiObject MSPower_DeviceEnable -Namespace root\wmi | where {$_.InstanceName.ToUpper().Contains($nic.PNPDeviceID)}
$returnValue = @{
NICPowerSaving = $powerMgmt.Enable
AdapterType = $AdapterType
}
$returnValue
}
function Set-TargetResource { [CmdletBinding()] param ( [parameter(Mandatory = $true)] [System.String] $AdapterType,
[ValidateSet("Present","Absent")]
[System.String]
$Ensure
)
$nics = Get-WmiObject Win32_NetworkAdapter | where {$_.AdapterType -eq $AdapterType}
foreach ($nic in $nics)
{
$powerMgmt = Get-WmiObject MSPower_DeviceEnable -Namespace root\wmi | where {$_.InstanceName.ToUpper().Contains($nic.PNPDeviceID)}
If ($Ensure -eq 'Present'){
Write-Verbose "Disabling the NIC power management setting."
$powerMgmt.Enable = $False #Turn off PowerManagement feature
}ELSE{
Write-Verbose "Enabling the NIC power management setting."
$powerMgmt.Enable = $true #Turn on PowerManagement feature
}
$powerMgmt.psbase.Put() | Out-Null
}
}
function Test-TargetResource { [CmdletBinding()] [OutputType([System.Boolean])] param ( [parameter(Mandatory = $true)] [System.String] $AdapterType,
[ValidateSet("Present","Absent")]
[System.String]
$Ensure
)
Write-Verbose "Checking to see if the power setting on the NIC for Adapter Type $AdapterType."
#Write-Debug "Use this cmdlet to write debug information while troubleshooting."
$nic = Get-WmiObject Win32_NetworkAdapter | where {$_.AdapterType -eq 'Ethernet 802.3'}
$powerMgmt = Get-WmiObject MSPower_DeviceEnable -Namespace root\wmi | where {$_.InstanceName.ToUpper().Contains($nic.PNPDeviceID)}
If ($Ensure -eq 'Present'){
If ($powerMgmt.Enable -eq $false){
Write-Verbose "NIC Power Management setting is disabled."
$result = $true
}ELSE{
Write-Verbose "NIC Power Management setting is not disabled. - Disabling"
$result = $false
}
}
If ($Ensure -eq 'Absent'){
If ($powerMgmt.Enable -eq $false) {
Write-Verbose "NIC Power Management setting is disabled. - Enabling."
$result = $false
}ELSE{
Write-Verbose "NIC Power Management setting is not disabled."
$result = $true
}
}
$result }
Export-ModuleMember -Function *-TargetResource
The one thing I have an issue with is. Present means disabled Power Management setting. Absent mean enable Power Management setting. It seems reversed but I don't like the fact the default would be Absent to disable the Power Management setting when this is the desired state in my case.
@edycus - Rather than using Ensure as the parameter name it might make more sense to use Disable as a boolean parameter. That might make it clearer than using Ensure present/absent.
Do you feel like submitting this new resource as a PR?
Do you have an example of using disable. Not sure I have seen it.
Sent from my Verizon, Samsung Galaxy smartphone
-------- Original message -------- From: Daniel Scott-Raynsford [email protected] Date: 2/14/17 12:31 AM (GMT-08:00) To: PowerShell/xNetworking [email protected] Cc: edycus [email protected], Mention [email protected] Subject: Re: [PowerShell/xNetworking] Need Network Power Settings (#188)
@edycushttps://github.com/edycus - Rather than using Ensure as the parameter name it might make more sense to use Disable as a boolean parameter. That might make it clearer than using Ensure present/absent.
Do you feel like submitting this new resource as a PR?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/PowerShell/xNetworking/issues/188#issuecomment-279639861, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AOO2Oi8dU2NkAsx_cp5P5Ey0YCyZ3ap7ks5rcWZTgaJpZM4L9i6C.
Actually, another resource that has a similar design pattern is: https://github.com/PowerShell/xNetworking/blob/dev/Modules/xNetworking/DSCResources/MSFT_xNetAdapterBinding/MSFT_xNetAdapterBinding.schema.mof#L6
I see. This will take ensure out of the design. Let me work this up and see I'll resubmit
Sent from my Verizon, Samsung Galaxy smartphone
-------- Original message -------- From: Daniel Scott-Raynsford [email protected] Date: 2/14/17 10:23 PM (GMT-08:00) To: PowerShell/xNetworking [email protected] Cc: edycus [email protected], Mention [email protected] Subject: Re: [PowerShell/xNetworking] Need Network Power Settings (#188)
Actually, another resource that has a similar design pattern is: https://github.com/PowerShell/xNetworking/blob/dev/Modules/xNetworking/DSCResources/MSFT_xNetAdapterBinding/MSFT_xNetAdapterBinding.schema.mof#L6
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/PowerShell/xNetworking/issues/188#issuecomment-279927452, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AOO2OuisWBsfUb6Fsd3Uoq77GtqHnY6Rks5rcpnQgaJpZM4L9i6C.