Tagged: .NET

SQL in Powershell without cmdlets

I am currently playing around with ConfigMgr 2012 cmdlets… and they only run in X86 (*doh*).

Anyway, I need to query a database with information about some collections and other stuff I need to create… and running SQL Server cmdlets in X86 doesn’t work.

So to get this to work I found a Powershell function that uses SqlClient from .NET instead… I rewrote the function a bit and here is the result:

Function Run-SqlQuery {
		[string] $Server = ".",
		[string] $Database = "master",
		[string] $Query,
		[Int32]  $QueryTimeout=60
	$sqlConnection = New-Object System.Data.SqlClient.SQLConnection
	$sqlConnection.ConnectionString = "Data Source=$($Server);Initial Catalog=$($Database);Integrated Security=True;"
	$sqlCommand = New-Object System.Data.SqlClient.SqlCommand($Query,$sqlConnection)
	$sqlCommand.CommandTimeout = $QueryTimeout
	$sqlDataSet = New-Object System.Data.DataSet
	$sqlDataAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($sqlCommand)

	return $sqlDataSet.Tables[0]

Then you can use it like this:

Run-SqlQuery -Query "EXEC SP_HelpDB"
Run-SqlQuery -Query "SELECT Col1, Col2 FROM MyTable" -Database "MyDatabase" -Server "SomeServer" | Format-Table -AutoSize

This can probably be useful if you need to run a SQL command from a box where your’e not sure if you have SQL cmlets or not.

PSOL: Read web page

If you need to output something from a webpage via Powershell, then .NET is a easy way to do it.

Write-Host ([String] (New-Object Net.WebClient).DownloadString('http://snowland.se/demo/'))

Softgrid & .NET-problem

Stötte på ett lite halvmysko problem med version av Softgrid-servern.

Vid installation verkar den missa att sätta lite rättigheter… eller så var det någonting mysko med den servern.

I alla fall så får man ett error-event 1088 från ASP.NET där texten säger (bland annat) “Error: 0x80070005 Access is denied”
Eventet innan är en varning med nummer 1073, även den från ASP.NET. En hel del mer text men det står bland annat “Failed to initialize the AppDomain:/LM/W3SVC/1/Root/SoftGridManagement”.

Efter en liten stunds surfande så komm jag fram till att ASPNET-kontot inte hade några rättigheter i katalogen …\Softricity\SoftGrid Management Service

Efter att jag gav den lite rättigheter där så vart allting frid och fröjd.

Written by Comments Off on Softgrid & .NET-problem Posted in App-V Tagged with