How can I do it better instead of adding a lot of duplicate same command for the purpose?

Discussion in 'Scripting' started by RemixPL1994, May 9, 2023.

    i learn from you Sir. expertised professional . a lot lot respect :worthy:
     
  1. RemixPL1994

    RemixPL1994 MDL Junior Member

    Jul 16, 2017
    54
    6
    0
    #22 RemixPL1994, May 11, 2023
    Last edited by a moderator: May 11, 2023
    (OP)
    I only managed to find this and after testing I found that it works for windows 10 but it didn't work on windows 7 on my test virtualbox:
    Code:
    powershell.exe -executionpolicy bypass -command "&{$PnPValue=24;$Adapter=Get-NetAdapter | Where-Object {($_.Status -eq 'Up') -and ($_.PhysicalMediaType -eq '802.3')};$KeyPath='HKLM:\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\';foreach ($Entry in (Get-ChildItem $KeyPath -ErrorAction SilentlyContinue).Name) {If ((Get-ItemProperty REGISTRY::$Entry).DriverDesc -eq $Adapter.InterfaceDescription) {$Value=(Get-ItemProperty REGISTRY::$Entry).PnPCapabilities;If ($Value -ne $PnPValue) {Set-ItemProperty -Path REGISTRY::$Entry -Name PnPCapabilities -Value $PnPValue -Force;Disable-PnpDevice -InstanceId $Adapter.PnPDeviceID -Confirm:$false;Enable-PnpDevice -InstanceId $Adapter.PnPDeviceID -Confirm:$false;$Value=(Get-ItemProperty REGISTRY::$Entry).PnPCapabilities};If ($Value -eq $PnPValue) {Write-Host 'Allow the computer to turn off this device is configured'} else {Write-Host 'Failed';Exit 1}}}}"
    
    is there a chance to modify it so that it works from windows vista up to windows 11?


    @(\_/)^(\_/) @abbodi1406 @Dark Dinosaur @Carlos Detweiller
     
  2. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    4,189
    6,018
    150
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. RemixPL1994

    RemixPL1994 MDL Junior Member

    Jul 16, 2017
    54
    6
    0
  4. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    4,189
    6,018
    150
    Some command req minimum ps version..
    So .. using wmi to get data .. it might saves you
    And you can use even 7 sp 1
    Question is .. which one don't work on 7 sp 1...
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    4,189
    6,018
    150
    #26 Dark Dinosaur, May 11, 2023
    Last edited: May 11, 2023
    Look what I found..
    Is it help?

    Not relevant anyway.
    not supported


    https://learn.microsoft.com/en-us/w...powerstate-method-in-class-cim-networkadapter

    Code:
    Minimum supported client
    Windows Vista
    Minimum supported server
    Windows Server 2008
    Namespace
    Root\CIMV2
    MOF
    CIMWin32.mof
    DLL
    CIMWin32.dll
    Code:
    Parameters
    PowerState [in]
    
    A ValueMap value that specifies the desired power state for this logical device.
    
    1
    
    Full power.
    
    2
    
    Power save low-power mode.
    
    3
    
    Power save standby.
    
    4
    
    Power save other.
    
    5
    
    Power cycle.
    
    6
    
    Power off.
    
    Time [in]
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. RemixPL1994

    RemixPL1994 MDL Junior Member

    Jul 16, 2017
    54
    6
    0
    #27 RemixPL1994, May 11, 2023
    Last edited: May 11, 2023
    (OP)
    I have now checked the powershell script physically on the installed windows 7 SP1 + windows update rights on the laptop and it has been confirmed that the powershell script does not work on Win7 SP1.

    Probably as I'm guessing its some commands/commands inside are not supported by the older version of powershell which is in windows7 even though powershell itself is present and for it to work a newer version of powershell is required which possibly uses the replaced other commands and therefore on windows 10 it works.

    So the powershell script won't work because it doesn't work on vista or win7.

    This below is a completely different setting that is regulated in powercfg and I already know it and I have commands to set it, but it's something different than PnPCapabilities:

    upload_2023-5-11_22-44-0.png

    Going back to the starting point and beginning, this code works best:

    Code:
    setlocal enabledelayedexpansion
    
    set start=0
    set end=20
    
    for /L %%i in (%start%,1,%end%) do (
      set key=000%%i
      set key=!key:~-4!
    
      reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\!key!" /v DriverDesc 2>nul | findstr /i /c:"WAN Miniport" /c:"VirtualBox" /c:"Hyper-V" /c:"Microsoft Kernel" >nul && (
        echo DriverDesc zawiera niedozwoloną nazwę w kluczu !key! >NUL 2>&1
      ) || (
        reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\!key!" /v DriverDesc 2>nul | findstr /i /c:"WAN Miniport" /c:"VirtualBox" /c:"Hyper-V" /c:"Microsoft Kernel" >nul || (
          reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\!key!" /v PnPCapabilities /t REG_DWORD /d 24 /f
        )
      )
    )


    Is anyone able to help me just not to create new values of type 0013, 0014, 0015, 0016, 0017 in the registry if they do not exist and are not there?

    It should only add PnPCapabilities where there are already given values e.g. from 0000 to 0001 and e.g. up to 0010.

    But it shouldn't create 0016 even though its operation and search range is 0020 eg max.

    I would like the range of its operation to be 0020 but to work only and add where the numbers and settings in the register are present.

    Can anyone help? Please.

    As you can see, I currently have 0000 to 0010 in my registry.

    The script should search from 0000 to 0020.

    But it should not add new ones i.e. 0011 until 0020.

    Thanks to this, it will only add PnPCapabilities to one item, e.g. 0007 which is currently the one where my Intel(R) Ethernet Connection (2) I218-V is located.

    I'm sure it can be done but I've tried and I can't do it on my own.

    upload_2023-5-11_22-50-56.png

    @(\_/)^(\_/) @abbodi1406 @Dark Dinosaur @Carlos Detweiller

    EDIT:

    Very thank you @Dark Dinosaur

    Here's all the correct code you need if anyone needs it:

    Code:
    SetLocal EnableDelayedExpansion
    
    SET START=0
    SET END=50
    
    FOR /L %%I IN (%START%,1,%END%) DO (
      SET KEY=000%%I
      SET KEY=!KEY:~-4!
      REG QUERY "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\!KEY!" /V DriverDesc >NUL 2>NUL  && (
      (reg QUERY "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\!KEY!" /V DriverDesc 2>NUL | FINDSTR /I /C:"WAN Miniport" /C:"VirtualBox" /C:"Hyper-V" /C:"Microsoft Kernel" >NUL) || (
        ECHO ADD KEY :: !KEY! >NUL 2>&1
        REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\!KEY!" /V PnPCapabilities /T REG_DWORD /D 24 /F >NUL 2>&1
        )
      )
    )
     
  7. yea i know he can resolve almost any issue . creds from my side too @Dark Dinosaur . you r the best of the best.
     
  8. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    4,189
    6,018
    150
    (\_/)^(\_/) is way better than me ..
    See his project ..
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...