question

QiangminLiu-1430 avatar image
0 Votes"
QiangminLiu-1430 asked QiangminLiu-1430 answered

powershell EnableStatic method return 2147786788

OS:
Windows Server 2008 R2 English


I use a script to do boot initialization and call the EnableStatic method, but this method always makes an error when it starts up and runs. After logging in to the system, it can run normally when manually executed.

I'm very sorry, my English is very poor. This is from google translation.

startup,log:
126204-startup.png


manual execution log:
126174-manual.png




This is my code:

 Function WaitNICExist {
     $NIC = Get-WmiObject -Class Win32_NetworkAdapterConfiguration | where {$_.IPEnabled -eq $true}
    
     if ($NIC -eq $null) {
         Start-Sleep -sec 3
         Write-Host "Waiting another 3 sec for NIC"
         $NIC = WaitNICExist
         return $NIC
     } else {
         return $NIC
     }
 } 
    
     $IPS = @()
     $MASKS = @()
    
     $IPS += $ip
     $MASKS += $subnet
    
     Start-Transcript
     $NIC = WaitNICExist
    
     Write-Host "3333"
     ps
     Write-Host "3333"
    
     $NIC.EnableStatic($IPS, $MASKS)
     $NIC.SetGateways($gateway)
     #$NIC.SetDNSServerSearchOrder($dns)






windows-serverwindows-server-powershell
startup.png (8.1 KiB)
manual.png (39.4 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

QiangminLiu-1430 avatar image
0 Votes"
QiangminLiu-1430 answered

Still did not find that the program occupied the lock.
But this program will exit, so judge the return value of EnableStatic. If the configuration fails, try again.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

QiangminLiu-1430 avatar image
0 Votes"
QiangminLiu-1430 answered RichMatheisen-8856 commented

EnableStatic method return value is 2147786788, "Write lock not enabled. For more information, see INetCfgLock::AcquireWriteLock".

I don't know how to fix this problem. The INetCfgLock::AcquireWriteLock is method of C#, but I use powershell.




https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/enablestatic-method-in-class-win32-networkadapterconfiguration

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

If this is a timing problem that causing the acquisition of the lock to fail, you can either delay calling the function WaitNICExist, or you can try wrapping line 27 in a Try/Catch block and check the error object for the error code cited in your posting. If the lock fails, try sleeping for a few seconds and then retry the EnableStatic call (or maybe even retry it a few times -- but not forever!).

Another possibility is permission to acquire the lock. Does the script use the same account when it fails as it does when it runs successfully?

1 Vote 1 ·
LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered

Hello @QiangminLiu-1430,

Thank you for your question.

Please Follow these steps :
The EnableStatic WMI class method enables static TCP/IP addressing for the target network adapter. As a result, DHCP for this network adapter is disabled.

Syntax

uint32 EnableStatic(
[in] string IPAddress[],
[in] string SubnetMask[]
);


For more information you can visit this link:
https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/enablestatic-method-in-class-win32-networkadapterconfiguration

If the reply was helpful,please don't forget to upvote or accept as answer.
Thanks,

Aradhya C

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.