Group-Object and GroupInfo objects

July 18th, 2010 by Mr.M | No Comments » Read More

A couple of days ago, I was the generic group-objects cmdlet:

  1. Get-Service | Group status

This command showed me the numbers of running and stopped services on a computer. The count column is fine and all but the [Group] column itself looked like this:

  1. Group
  2. -----
  3. {System.ServiceProcess.ServiceController, System.ServiceProcess.ServiceController, S...
  4. {System.ServiceProcess.ServiceController, System.ServiceProcess.ServiceController, S...

I wanted to see the service name beyond the ‘…’. To get that information returned back to you, you can use the following; It will list you all the services that are stopped. Change the array from [0] to [1] will show you the running services:

  1. (Get-Service | Group status)[0].group

Rebooting servers

July 16th, 2010 by Mr.M | No Comments » Read More

The other night I was patching a bunch of Windows servers and needed to reboot them afterward. No one wants to sit there and manually reboot xx servers, so I wrote the following:

  1. $servers = Get-Content "D:\Patch\Bin\testServers.txt"
  2. foreach ($server in $servers)
  3. {
  4. restart-computer -computername $server -force -ErrorAction Inquire
  5. Write-Host "ServeName -> $server

Powershell output and illegal characters

June 18th, 2010 by Mr.M | No Comments » Read More

A few days ago, a dev provided me with an application. The application output some stuff to the screen and I wanted to capture this output and redirect it to a batch file. My command is:

  1. echo "def" | out-File test.bat

At first glance, the batch file looks good. However when I attempted to execute the batch file, I got the error:

  1. C:\Users\kwsvc\Desktop>■d
  2. '■d' is not recognized as an internal or external command, operable program or batch file.

So I’m sitting here looking at the file trying to determine where this stupid illegal character is; notepad.exe and notepad++ couldn’t see anything wrong. What give?

Well, to see what the issue is, I had to fire up the binary editor. Turns out, at the beginning of the file, there are two illegal characters. Their binary code is: FF FE. Not only that, there is a binary code of: 00 between every character in the text file. For example:

  1. FF FE 64 00 65 00 66 00 OD 00 0A . . d . e . f

The FF FE indicate that this file was outputted using .Net-standard Unicode encoding and is Powershell’s default string format. The fix is to run the following:

  1. echo "def" | out-File filename -encoding ascii

MSDN XPATH

May 3rd, 2010 by Mr.M | No Comments » Read More

use this to look up stuff in an xml file (for powershell) :

  1. [xml] $xml = gc $path
  2. $root = $xml.get_DocumentElement()
  3. $text = $root.SelectSingleNode("/access-policy/cross-domain-access/policy/allow-from/domain/@uri[contains(.,'http://www.cbtr.net')]")

to replace stuff, use:

  1. $root.SelectNodes("//add[@key='ServiceLayerHost']/@value") | % { $_.Value = $MTServerName+":"+$MTWebSitePort }

If using @uri, then you can use $text.Value. if not, then you need to use $text.InnerText to get the value.

Finding Execution Plan

March 22nd, 2010 by Mr.M | No Comments » Read More

Use the following to execution plans that’s cached on the SQL server.

  1. SELECT [cp].[refcounts]
  2. ,[cp].[usecounts]
  3. ,[cp].[objtype]
  4. ,[st].[dbid]
  5. ,[st].[objectid]
  6. ,[st].[text]
  7. ,[qp].[query_plan]
  8. FROM sys.dm_exec_cached_plans cp
  9. CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) st
  10. CROSS APPLY sys.dm_exec_query_plan(cp.plan_handle)
  11. qp;

Return a status or value from sp_start_job

April 30th, 2009 by Mr.M | No Comments » Read More

In my procedure, I’m calling a job on a remote server and if this call fails, I would like the code to handle it accordingly. So if I get a return value or status of 0, the job started successfully. If 1, then it failed to start.

Now I can’t monitor the entire duration of the SQL job on the remote server, I’ll need another process for it.

The code to return a value or status from sp_start_job is as follow:

  1. declare @rnt int
  2. exec @rnt = msdb.dbo.sp_start_job @job_name = 'JOB_NAME'
  3. IF @rnt = 1
  4. begin
  5. --- code to handle the failure
  6. end

Creating Windows Clustered Resources

April 14th, 2009 by Mr.M | No Comments » Read More

Over the past 3 weeks, I’ve built over 40 pairs of active/active cluster on Windows 2003. I’ve looked around the web, to see if someone has any type of script that will help me, but alas, everyone wants to show me how to PnC (point & click) my way thru the bullshit GUI.

Don’t get me wrong, I’m grateful for the GUI but when I’m setting up a crap load of A/A cluster servers, I want everything to be automated or as automated as possible. I ended up writing my own script. All you need to do is past in the correct variables and make sure you are on the server you are trying to create the shared folder. If not, you will definitely be in for a surprise!

Download Create clustered resources Version v0.1

sql 2005 connection error

April 10th, 2009 by Mr.M | No Comments » Read More

Error:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.

I have a server named: virtualA\virtualSQLA. This server was working fine a few days ago but after a server reboot, it was giving the above error message. I was able to connect via its virtual name (virtualA) and virtualA\virtualSQLA,1433 but not virtualA\virtualSQLA. After 20 minutes of looking around, I found that the SQLBrowser service wasn’t running.

finding a bunch of server’s ip address

March 31st, 2009 by Mr.M | No Comments » Read More
  1. get-content server.txt| foreach {
  2. $strComputer = $_
  3. $colItems = get-wmiobject -class "Win32_NetworkAdapterConfiguration" `
  4. -computername $strComputer | Where{$_.IpEnabled -Match "True"}
  5. foreach ($objItem in $colItems) {
  6. write-host $strComputer " : " $objItem.IPAddress
  7. }
  8. }

in the server.txt file, one server per line and no extra space at the end of each server.

finding physical servername

March 18th, 2009 by Mr.M | No Comments » Read More

For some reason, I have a problem remembering server name but don’t have any problem remembering SQL name instances. Maybe because I connect to the name instance more often then not. But when someone ask me what’s the physical servername of so and so SQL name instances, i’m lost.

a co-worker showed me a SQL function a few days ago that will tell you the physical servername of a SQL name instance:

select serverproperty(‘ComputerNamePhysicalNetBIOS’)

I have to look around some more to see if this function will allow me to identify clustered nodes, be it active/active or active/passive.