Web Service
For this example, I want to query the IVANTI EPM database by providing the MAC address as a parameter and in return receive the computername.
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 | # Import the required modules Import-Module Pode.Web Import-Module SqlServer # Start the Pode server Start-PodeServer { Add-PodeEndpoint -Address * -Port 8080 -Protocol Http # Define the route to get the computer name based on the MAC address Add-PodeRoute -Method Get -Path '/GetName' -ScriptBlock { # Retrieve the MAC address from the query parameters $macaddress = $WebEvent .Query[ 'macadress' ] Write-Host "Received MAC Address: $macaddress" $user = 'sa' $password = "Password1" $creds = New-Object -TypeName System.Management.Automation.PsCredential -ArgumentList ( $user , ( ConvertTo-SecureString -String $password -AsPlainText -Force )) $database = "EPM" $dataSource = "epm2024.monlab.lan" Write-Host "--- Connecting to SQL" $PassSQL = $creds .GetNetworkCredential().Password # Connecting to the database $connectionString = "Server=$dataSource;uid=$user;pwd=$PassSQL;Database=$database;Integrated Security=False;" $connection = New-Object System.Data.SqlClient.SqlConnection $connection .ConnectionString = $connectionString $connection .Open() $query = "SELECT DISTINCT A0.DISPLAYNAME, A1.PHYSADDRESS FROM Computer A0 (nolock) LEFT OUTER JOIN BoundAdapter A1 (nolock) ON A0.Computer_Idn = A1.Computer_Idn WHERE (A0.DISPLAYNAME IS NOT NULL) ORDER BY A0.DISPLAYNAME " $command = $connection .CreateCommand() $command .CommandText = $query $result = $command .ExecuteReader() $table = New-Object System.Data.DataTable $table .Load( $result ) $connection .Close() $computername = "Not found" foreach ( $element in $table ) { $Tabmac = $( $element .PHYSADDRESS) #write-host "Compare $Tabmac - $macaddress" If ( $Tabmac -eq $macaddress ) { $computername = $( $element .DISPLAYNAME) #write-host "Match .." } } Write-host "Result $computername" # Return the response Write-PodeJsonResponse -Value @{ Computername = $computername SecondVariable = "ABCDE" } } } |

Query Web service
Query the Webservice
1 |
note : macaddress and not macadress

https://github.com/DavidWuibaille/Repository/tree/main/Powershell/Module%20Pode.web
0 Comments