Skip to main content

Posts

How to make an HTTP request with PowerShell

If you are making an HTTP request to a RESTful web service, you can use the PowerShell  Invoke-RestMethod cmdlet. This provides a very simple HTTP REST interface, and will also format the result into a PowerShell object. If you would like to use your own functions, you can follow the instructions below. This is a helper function to format (indent) an XML response from a web service. function Format-XML { Param ([string]$xml) $out = New-Object System.IO.StringWriter $Doc=New-Object system.xml.xmlDataDocument $doc.LoadXml($xml) $writer=New-Object system.xml.xmltextwriter($out) $writer.Formatting = [System.xml.formatting]::Indented $doc.WriteContentTo($writer) $writer.Flush() $out.flush() Write-Output $out.ToString() } Here is the function to make the http call. It dumps the response data on the terminal and also returns it as a string to the caller. If there is an error it will dump the HTTP status code and comment on the terminal and return the re...

How to delete a file in C# that might be in use

int retries = 5; while (retries-- >= 0) { try { retries = -1; System.IO.File.Delete(@"C:\deleteme.txt"); } catch (System.IO.IOException ex) // "The specified file is in use." { // File is locked? Sleep and try again. System.Threading.Thread.Sleep(100); // Wait 100ms if (retries >= 0) continue; // go back to while loop throw ex; // give up } } This will also work for writing to a file, etc. This should be enclosed in a try/catch block to catch the other exceptions thrown by the System.IO.File.Delete method.

How to Self-Sign an SSL Certificate

To self-sign SSL certificates using openssl, you will need to set up your own certificate authority using the following steps. Generate a key for your certificate authority openssl genrsa -des3 -out server.key 2048 Remove the password from your server's key. This step is optional, but is required to get the below Perl script to work. Obviously you wouldn't do this to a real key that you had signed by a real certificate authority. cp server.key server.key.org openssl rsa -in server.key.org -out server.key Generate a CSR for your certificate authority. openssl req -new -nodes -key server.key -out server.csr Sign your certificate request openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt Now you can sign certificate requests. Here is an example for a CSR named test.csr openssl x509 -req -days 365 -in test.csr -out test.crt -CA server.crt -CAkey server.key -set_serial 01 Optional: Add your certificate authority to your browser so you don'...

Getting Next and Previous Item by ID From MySQL Database With PHP

If you know the ID of an item you want to get from a MySQL database with PHP, but you also want to get the next and previous items when the IDs are in numerical order but may contain gaps due to deleted items, you can do something like this: $curID = NULL; $curRow = NULL; $prevID = NULL; $nextID = NULL; $lastID = NULL; // Get the latest one (for the "Last" link) $result = mysql_query("SELECT itemID FROM tableName ORDER BY itemID desc LIMIT 1"); $row = mysql_fetch_array( $result ); $lastID = $row['itemID']; // Get the ID for the requested item if (isset($_GET['curID'])) { // if $_GET['curID'] defined, use it as curID $curID = $_GET['curID']; } else { // Use the latest one $curID = $lastID; } // Get requested row and next/prev rows if they are there $sql = " SELECT * FROM tableName WHERE tableName.itemID IN ( (select itemID from tableName where itemID < $curID order by i...

Cmd.exe - Using For Loops To Simulate "grep -r */Filename" on Windows

FOR %variable IN (set) DO command [command-parameters] %variable Specifies a single letter replaceable parameter. (set) Specifies a set of one or more files. Wildcards may be used. command Specifies the command to carry out for each file. command-parameters Specifies parameters or switches for the specified command. To perform a grep operation on all files with a certain name in subdirectories of the current directory, although this will try to grep Folder\Filename.ext even if Filename.ext doesn't exist in the folder. for /d %I in (*.*) do grep grepString %~sI\Filename.ext This is slower, but it won't pass files that don't exist to grep, and will recursively search all subdirectories under the current directory. The first example only goes one level deep. for /f "usebackq delims=;" %I in (`dir /s/b Filename.ext`) do grep grepString "%I" The "delims=;" (you can use almost anything for the ;) is required if there are spaces in t...

Get your current IP Address with Powershell

There are lots of ways to do this, but I haven't found an elegant one that works well. Here is my approach that queries the registry: [array] $IPArray = @() $Adapters = Get-Childitem "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Adapters" foreach ($uid in $Adapters) { if ($uid.PSChildName -ne "NdisWanIp") { $Interface = "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\" + $uid.PSChildName ## Write-Host "Checking: $Interface" $IPs = Get-ItemProperty $Interface $IPArray += $IPs.DhcpIPAddress } } $IPArray You can change "$IPs.DhcpIPAddress" to "$IPs.IPAddress" if you have a static IP.