Powershell Web Service
This script demonstrates how to query a web service to retrieve a computer name using a MAC address.
Normally, you would check a database or a CSV file for this information. However, for the purpose of this example, we will use a switch statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | # Requires administrative privileges to execute Add-Type -AssemblyName System.Net.Http $listener = New-Object System.Net.HttpListener $listener .Start() Write-Host "Listening on http://localhost:8000/" $logPath = "c:\windows\temp\webservice2.log" Write-Host "Log file on $logPath" try { while ( $listener .IsListening) { $context = $listener .GetContext() $request = $context .Request $response = $context .Response # Retrieve the 'macaddress' parameter from the URL $macaddress = $request .QueryString[ "macaddress" ] # Log the macaddress to the log file Add-Content -Path $logPath -Value "MAC Address : $macaddress" $Computername = " " $POSTYPE = "" # Response based on the parameter switch ($macaddress) { " 20-C1-9B-08-27-21 " { $Computername = " PCdavid " $POSTYPE = " TypeA " # Example value for POSTYPE } " 00-BE-43-23-10-2C " { $Computername = " PCromain " $POSTYPE = " TypeB " # Example value for POSTYPE } default { $Computername = $null $POSTYPE = " Unknown " # POSTYPE value for unknown or not provided cases } } # Handle the case where 'macaddress' is not provided or unknown if (-not $Computername) { $response.StatusCode = 404 # HTTP code for " Not Found " or " Not Provided " Add-Content -Path $logPath -Value " ERROR 404 " Add-Content -Path $logPath -Value "" Write-Host " $macaddress = No Found " $jsonResponse = @{ Error = " No computer found with the provided MAC address. " POSTYPE = $POSTYPE } | ConvertTo-Json } else { # To return JSON Add-Content -Path $logPath -Value " $Computername " Add-Content -Path $logPath -Value "" Write-Host " $macaddress = $Computername " $response.Headers.Add(" Content-Type ", " application/json") $jsonResponse = @{ Computername = $Computername POSTYPE = $POSTYPE } | ConvertTo-Json } # Write the response $buffer = [System.Text.Encoding] ::UTF8.GetBytes( $jsonResponse ) $response .ContentLength64 = $buffer .Length $response .OutputStream.Write( $buffer , 0, $buffer .Length) $response .OutputStream.Close() } } finally { $listener . Stop() } |
Other option use PHP for Web service
I am hosting this PHP script on my Synology NAS

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <?php // Récupérer le paramètre 'macaddress' de l'URL $macaddress = $_GET [ 'macaddress' ] ?? '' ; // Utilisation de l'opérateur Null Coalesce pour PHP 7+ // Logique pour définir Computername et POSTYPE switch ( $macaddress ) { case "20-C1-9B-08-27-21" : $computername = "PCdavid" ; $postype = "TypeA" ; break ; case "00-BE-43-23-10-2C" : $computername = "PCromain" ; $postype = "TypeB" ; break ; default : $computername = null; $postype = "Unknown" ; } if ( $computername === null) { http_response_code(404); $response = json_encode([ "Error" => "No computer found with the provided MAC address." , "POSTYPE" => $postype ]); } else { $response = json_encode([ "Computername" => $computername , "POSTYPE" => $postype ]); } // Renvoyer la réponse header( 'Content-Type: application/json' ); echo $response ; ?> |
Prompt web service
This script retrieves the MAC address and uses a web service to obtain the computer name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | # Obtain all network interfaces with their MAC addresses $macAddresses = Get-NetAdapter | Where-Object { $_ .Status -eq "Up" } | Select-Object -ExpandProperty MacAddress # Webservice URL # Flag to indicate a successful response $found = $false # Loop through each MAC address and call the webservice foreach ( $mac in $macAddresses ) { if ( $found ) { break # Exit the loop if a successful match was already found } # Construct the URL with the MAC address as a parameter $url = $webserviceUrl + "?macaddress=$mac" # Call the webservice try { $response = Invoke-RestMethod -Uri $url # Check if the response contains the Computername if ( $response .Computername) { Write-Host "Response from server for MAC ${mac}: Computername = $($response.Computername), POSTYPE = $($response.POSTYPE)" $found = $true # Set the flag as true to indicate a match was found } else { Write-Host "No valid computer found for MAC ${mac}: $($response.Error)" } } catch { Write-Host "Error contacting webservice for MAC ${mac}: $_" } } if ( -not $found ) { Write-Host "No valid entries were found for any MAC addresses." } |
Script to change unattend.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | # Obtain all network interfaces with their MAC addresses $macAddresses = Get-NetAdapter | Where-Object { $_.Status -eq "Up" } | Select-Object -ExpandProperty MacAddress # Webservice URL for your NAS $webserviceUrl = "http://nas.wuibaille.fr/webservice/get-computer-info.php" # Path to the unattend.xml file $xmlFilePath = "C:\Windows\Panther\unattend.xml" # Ensure the XML file exists before proceeding if (Test-Path $xmlFilePath) { # Load the XML file [xml]$xmlDoc = Get-Content $xmlFilePath # Define the namespace manager $ns = New-Object System.Xml.XmlNamespaceManager($xmlDoc.NameTable) $ns.AddNamespace("ns", "urn:schemas-microsoft-com:unattend") # Loop through each MAC address and call the webservice foreach ($mac in $macAddresses) { # Construct the URL with the MAC address as a parameter $url = $webserviceUrl + "?macaddress=$mac" # Call the webservice try { $response = Invoke-RestMethod -Uri $url # Check if the response contains the ComputerName if ($response.Computername) { Write-Host "Response from server for MAC ${mac}: Computername = $($response.Computername)" # Attempt to find and modify the ComputerName element $computerNameNode = $xmlDoc.SelectSingleNode("//ns:settings[@pass='specialize']/ns:component/ns:ComputerName", $ns) if ($computerNameNode -ne $null) { $computerNameNode.InnerText = $response.Computername # Save the modified XML file $xmlDoc.Save($xmlFilePath) Write-Host "The ComputerName in unattend.xml has been updated to $($response.Computername)" break # Exit the loop as we've successfully updated the file } else { Write-Host "No ComputerName element found in XML." } } else { Write-Host "No valid computer found for MAC ${mac}" } } catch { Write-Host "Error contacting webservice for MAC ${mac}: $_" } } } else { Write-Host "The unattend.xml file does not exist at the specified path." } |
0 Comments