I am an occasional casual programmer (for about 60 years now!). I wrote 2 functional SNMP PowerShell desktop shortcut scripts 4 years ago. Now I am writing two scripts for my old Windows 10 system: one to Enable the Ethernet port and one to Disable it. The scripts actually work fine, but I have come across one anomaly that is mind boggling. Here is the "Enable" test case script; the "Disable" script just changes Enable-NetAdapter to Disable-NetAdapter.
When one of these scripts CHANGES the status of the NetAdapter, in approximately 4 seconds the light on the RJ45 connector responds appropriately and the Network Icon on the task bar changes appropriately.
# Windows 10
# C:\WINDOWS\System32\WindowsPowerShell\v1.0\
$adapterName = "Ethernet"
$adapter = Get-NetAdapter -Name $adapterName
# TEST STATUS REPORT:
Write-Host "Adapter Status: "$adapter.Status
Enable-NetAdapter -Name $adapterName -Confirm:$false
Start-Sleep -Seconds 10
Write-Host "Adapter Status: "$adapter.Status
# Try it again just for kicks ...
Enable-NetAdapter -Name $adapterName -Confirm:$false
Start-Sleep -Seconds 10
Write-Host "Adapter Status: "$adapter.Status
Start-Sleep -Seconds 5
The weird part is that: Whatever Status is reported in the first command that fetches the Ethernet status (Correctly!) is ALWAYS the Status reported in every following instance of the $adapter.Status command. This looks like nonsense to me in the case where the Status of the Ethernet port actually changes?
ServiceController status does not correctly reflect the actual service status Has an interesting note: "The ServiceController.Status property is not always live; it is lazily evaluated the first time it is requested, but (unless requested) only that time, subsequent queries to Status will not normally check the actual service. To force this, add: sc.Refresh();"
This might explain what is happening in PowerShell, but in the PowerShell ISE window:
" Get-NetAdapter -Name Ethernet. " options list shows "Status", but does NOT show "Refresh". I tried $adapter.Refresh both in the ISE and in the script, but it seems to be ignored.
Google AI flat-out couldn't process the question no matter how I refined it.
I would like to be able to validate 'change-of-state' in my script. Is that possible?
EDIT: Ahhh! I just figured out how to add my final explanation code ...
# Windows 10
# Enter this script with Ethernet ** CONNECTED **
$adapterName = "Ethernet"
$adapter = Get-NetAdapter -Name $adapterName
write-host " Adapter Status: "$adapter.Status
# Reports: Adapter Status: Up (Actual Status)
Disable-NetAdapter -Name $adapterName -Confirm:$false
Start-Sleep -Seconds 5
write-host "Adapter Status: "(Get-NetAdapter -Name Ethernet).Status
# Reports: Adapter Status: Disabled <-- CORRECT!
write-host "Adapter Status: "$adapter.Status
# Reports: Adapter Status: Up <-- WRONG !!!
Start-Sleep -Seconds 30
<#### Screen Output:
Adapter Status: Up
Adapter Status: Disabled
Adapter Status: Up
####>
This proves that embedding the 'command' in an assignment has not been accomplished ... perhaps because I do not know the proper syntax. I would like to be able to do that if possible.
EDIT2: FYI I tried the following assignment with added parentheses, but it did NOT change anything ...
$adapter = (Get-NetAdapter -Name $adapterName)
FINAL ANSWER: Syntax - (untested Google AI answer): In windows 10 how do I make an assignment of a PowerShell command to a variable so that when the variable is used it executes the command and not merely returns the previous result of the command?
# Assign the command as a script block
$GetTimeCommand = { Get-Date -Format 'HH:mm:ss' }
# Execute the command at different times
Write-Host "Current time (1st execution):"& $GetTimeCommand
Start-Sleep -Seconds 2 # Wait for 2 seconds
Write-Host "Current time (2nd execution):"& $GetTimeCommand