Developer42

2014-10-13

PowerShell Script :: Get HotFix Info from Local Machine & Web

Filed under: Microsoft, Technology — Tags: , , , , , , , , — Developer42 @ 22:02

In answering a question on Stack Overflow[1], I discovered that the Get-Hotfix cmdlet doesn’t list all hotfixes on your machine. Rather, if the fix is included in a CU, details of the installed CU will be presented, with no apparent way to drill down into the information and check for a specific fix.
So far as I can tell, there are no services / open databases which would allow you to programmatically retrieve this information. The only way currently is to manually head to the CU or fix’s related knowledge base site and read the contents.
As a very rough hack-around, I created a scraping script which will interrogate the MS knowledge-base for information on each hotfix, finding whether it’s a CU or not (based on the hotfix’s description in the page’s title) and if so try to find all related fixes. I only go down one level – to avoid the risk of an infinite loop & save time; should anyone want to go deeper, a caching mechanism could be used to store previous results and thus prevent the risk of a loop and also potentially improve performance.
As with most of the scripts on this site, this is more for me to play with PowerShell than to be of much practical use; but hopefully it could be to someone…

function get-hotfixInfo()
{
	process 
	{
		#$url = "http://support2.microsoft.com/kb/{0}" -f ($_.HotFixId -replace "KB(\d*)",'$1')
		$url = $_.Caption
		if($url -like "*.microsot.com")
		{
			$url = "{0}/{1}" -f $url,($_.HotFixId -replace "(KB)(\d*)",'$1/$2') 
		}
		try
		{
			$response = (Invoke-Webrequest $url -ea stop)
		} catch {
			$response = @{
				ParsedHTML = @{
					Title = "{0}`n`nURL: {1}" -f $error[0].Exception,$url 
				}
			}
		}
		$html = $response.ParsedHTML
		$isCU = $html.title -like "*cumulative*update*"
		$kblets = $null
		if($isCU) #this bit can be even slower than the above, hence only run if we believe we have a CU
		{
			$baseUri = $response.BaseResponse.ResponseURI
			$kblets = $html.getElementsByTagName('a') `
				| ? { ($_.parentNode.tagname -eq 'TD') -and ($_.parentNode.nextsibling.tagname = 'TD') } `
				| ? { $_.className -eq 'KBlink' } `
				| % { New-Object -TypeName PSObject -Prop @{
					Id = "KB{0}" -f $_.innerText
					Uri = (new-object System.URIBuilder($baseUri.scheme,$baseUri.dnssafehost,$baseUri.port,($_.href -replace "about:/(.*),'$1'"))).ToString()
					Title = $_.parentNode.nextsibling.innerText
				}}
		}
		New-Object -TypeName PSObject -Prop @{
			Id = $_.HotFixId
			IsCU = $isCU 
			Title = $html.title
			Source = $_.source
			Description = $_.description
			InstalledBy = $_.installedby
			InstalledOn = $_.installedon
			Uri = "http://support2.microsoft.com/kb/{0}" -f ($_.HotFixId -replace "KB(\d*)",'$1')
			KBlets = $kblets
		}
	}
}

#demos

#get the hotfix id for IE11 CU Feb 2014, and it's component hotfixes
get-hotfix -id kb2909921 | get-hotfixInfo | %{ $_; $_.KBlets | %{ $_; }} | ft -property id,title -autosize

#get the first 2 cumulative update hotfixes
get-hotfix | select -first 2 | get-hotfixInfo | ?{$_.isCU} 


2014-10-12

PowerShell Script: Demo of Calling a (StackExchange’s) Rest Web Service

Filed under: Microsoft, Technology — Tags: , , , , , , — Developer42 @ 01:25

Warning: this post is not useful unless you just like playing with scripts & APIs.

Reading a few posts on StackExchange’s Meta site, I saw that there was demand for, but no intent to, create some kind of synonym sub-type feature for tags.
e.g. Users looking for items tagged PowerShell would also see those tagged only PowerShell-1.0, PowerShell-2.0, PowerShell-ISE, etc.
Users looking for PowerShell-1.0 are being more specific though, so shouldn’t see those tagged simply PowerShell, and definitely not those tagged PowerShell-2.0.

Since SE have no intention of creating this, I figured it would be useful to have a site which could present a view over Stack Exchange sites, giving users these additional features.
Since I don’t have time to do that though, I satisfied my craving to build something by knocking up a quick script to play with the SE API.
…And since implementing synonyms would require a huge dictionary, which I’d have to create, I sufficed myself with simply playing with the API without the synonymn hierarchy feature.
So this is just me playing about with my new favourite scripting language and an API I hope one day to play more with (though then it would be with some other language).
Here’s what I came up with…

Code:

$tagList='c#','.net'
$tags = $taglist -join ';'
[Reflection.Assembly]::LoadWithPartialName("System.Web") | Out-Null
$tags = [System.Web.HttpUtility]::UrlEncode($tags)
$url = "http://api.stackexchange.com/2.2/search/advanced?page=1&pagesize=10&order=desc&sort=creation&ans
wers=0&closed=False&tagged={0}&site=stackoverflow" -f $tags
$json = Invoke-RestMethod $url
$json.items | select title,link | %{ "{0}`n{1}`n" -f [System.Web.HttpUtility]::HtmlDecode($_.title), $_.link; }

Output:

ASP.NET Web Forms and MySql Entity Framework: "Nested transactions are not supported"
http://stackoverflow.com/questions/26320679/asp-net-web-forms-and-mysql-entity-framework-nested-transactions-are-not-su
ppo

.NET C# - Both Bitmap and Image Loads Images Smaller than Expected
http://stackoverflow.com/questions/26320637/net-c-both-bitmap-and-image-loads-images-smaller-than-expected

Mono TypeLoadException in an executable
http://stackoverflow.com/questions/26320146/mono-typeloadexception-in-an-executable

Developing a music player in C# without using com component
http://stackoverflow.com/questions/26319978/developing-a-music-player-in-c-without-using-com-component

Fastest way to get directory data
http://stackoverflow.com/questions/26319973/fastest-way-to-get-directory-data

c# datagridview multi-page print
http://stackoverflow.com/questions/26319852/c-datagridview-multi-page-print

How to access an object in c sharp from outside my method?
http://stackoverflow.com/questions/26319550/how-to-access-an-object-in-c-sharp-from-outside-my-method

.NET & jQuery Jcrop Initialization Causing Double Page Load
http://stackoverflow.com/questions/26319428/net-jquery-jcrop-initialization-causing-double-page-load

Why i'm getting exception InvalidOperationException when using backgroundworker?
http://stackoverflow.com/questions/26319024/why-im-getting-exception-invalidoperationexception-when-using-backgroundwor
ker

Index was out of range error when I add a UserControl to a panel, Microsoft VS C#
http://stackoverflow.com/questions/26318864/index-was-out-of-range-error-when-i-add-a-usercontrol-to-a-panel-microsoft-
vs-c

So not that useful, but a fun play project.

Blog at WordPress.com.

%d bloggers like this: