How can I view my reputation with a PowerShell function ?
You can use the following function
Function Get-StackOverFlowReputation {
param($userID)
$client = new-object System.Net.WebClient
$JSONFlair = $client.DownloadString("http://stackoverflow.com/users/flair/$userid.json")
$JSONFlair.split(",") | select-string "reputation","displayName"
}
260 > Get-StackOverFlowReputation -userID 45571
"displayName":"Andy Schneider"
"reputation":"344"
It's quick and dirty. I am sure you could use some nifty library to convert JSON to a PSobject but this will get the job done.
This question looked very fun and I had to give it a try even though its already has an accepted answer. Plus, the accepted answer does not seem to properly work for reputations that are greater than 999 (i.e. 1,000 contains a comma which is being also being split).
Being that the format of Flair is in JSON, simply splitting on it does not always work and regex against JSON is almost impossible. While there are .NET JSON libraries out there I wanted to keep the solution all within PowerShell (including V1).
The following uses the 3.5 JavaScriptSerializer class, which requires us to load the assembly in our script.
With PowerShell 2.0 it's a lot easier to create "custom objects" with hashes.
function Get-StackOverflowReputation
{
param ( $UserId )
$assembly = [Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")
$client = New-Object System.Net.WebClient
$json = $client.DownloadString("http://stackoverflow.com/users/flair/$UserId.json")
$transmogrifer = New-Object System.Web.Script.Serialization.JavaScriptSerializer
$jsonFlair = $transmogrifer.DeserializeObject( $json )
$flair = New-Object PSObject -Property @{ user = $jsonFlair["displayName"]; rep = $jsonFlair["reputation"] }
$flair
}
1> Get-StackOverflowReputation -UserId 45571
user rep
---- ---
Andy Schneider 779
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With