Output results from PowerShell to CSV or XML — Parv The IT Geek

Have you wanted to output results from PowerShell to CSV or XML? Well I am going to show you how.

I have previously explained how to export results of PowerShell queries but did not include how to export the results to CSV or XML. The reason for that is that when you become familiar with PowerShell you will find out that PowerShell commands can return a lot more information than what they show on screen. I will show how to get that information in a future blog post but just be aware that there is a lot more information readily available and only the displayed columns are exported to text(TXT) files.

When exporting to CSV and XML the hidden columns are exported as well. I will give an example of the difference in the exports.

The results of Get-Process are displayed below.

If I run the below commands I would output the results of Get-Process to CSV and XML.

Get-Process | Export-CSV c:PowerShellProcessCSV.csv

Get-Process | Export-CliXML c:PowerShellProcessCliXML.xml

When you look in the text file you will see the same results as shown on screen.

However, the CSV and XML have all the columns available for the results of that command. I have shown an excerpt of the Get-Process results in CSV format below.

I would advise that you use TXT, CSV and XML files where appropriate and check out the results you could get from various commands.


Originally published at https://parvtheitgeek.com on February 23, 2014.

Simple way to output results to a file using PowerShell — Parv The IT Geek

Have you ever wanted to send the output of a PowerShell command to a file? Well I am going to show you how.

When you run a PowerShell command like Get-Process, Get-Host…. It will return useful information but sometimes you want to save that information for future reference. You could cut and paste or take a screenshot but these are way more complicated than simply using the inbuilt functionality within PowerShell.

For example if you wanted to output the processes currently running you would use Get-Process. To output it you would add the below command

Get-Process > yourfilename.txt

Get-Process > C:PowerShellProcess.txt


Originally published at https://parvtheitgeek.com on February 17, 2014.

List active process running using PowerShell — Parv The IT Geek

Have you ever wanted to quickly find the processes running using Powershell? Wanted an alternate to Task Manager? Well I am going to show you how.

Whenever I get a call from someone about a performance issue on any of our SQL Servers I always usually have a quick glance of Task Manager so that I can help identify what maybe causing the problem. However, the problem can be that on a server which is low on resources it may take a long time to open or the opening of task manager may add to the problems or it may not open at all.

This has led me to frequently opening PowerShell and using it to get the information which I need. The script is quite simple and is below.

Get-Process

I have found the advantage of using the script is that it gives me a snapshot whilst in task manager it is always a moving target and if I run it several times I can just scroll up and down to find a pattern which would help diagnose the problem.


Originally published at https://parvtheitgeek.com on February 17, 2014.

Hello World in C# — Parv The IT Geek

Have you ever wanted to reach the first milestone in programming by writing a Hello World application in C#? Well I am going to show you the script to produce the program.

I was asked by a member of our helpdesk team how he could become a programmer. I answered that he should start with the Hello World program in C# which was the first milestone all programmers had to accomplish before becoming programming gurus.

After he downloaded and installed Visual Studio 2012 Express I wrote the below script to show him how easy it was start programming and that he should just practice practice practice.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace HelloWorld

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine(“Hello World”);

Console.ReadLine();

}

}

}


Originally published at https://parvtheitgeek.com on February 16, 2014.

Get the Operating System Properties via PowerShell — Parv The IT Geek

Have you ever wanted to get all the operating system properties of a computer? Well I am going to show you a short PowerShell script which can get loads of information about the operating system.

One of my colleagues was googling around for information about how to find the windows directory, number of users, last boot up time and many other properties of various computers. I told him that I had a short script which had that information and probably a whole lot more information which he might find useful.

$hostname = “parvinder-pc”

Get-WmiObject -class win32_operatingsystem -computer $hostname | select-object *


Originally published at https://parvtheitgeek.com on February 16, 2014.

Change a database state in SQL — Parv The IT Geek

Have you ever wanted to change a databases state from read only to read write or vice versa? Well I am going to show you how using some easy to remember T-SQL which you will be using repeatedly over time.

We received a database from a client the other day and noticed one of my System Admin was struggling with the restore. He thought he was doing something wrong with the restore and I said he hadn’t done anything wrong and that the database was likely put into that state prior to being backed up and sent to us.

I showed him the below T-SQL which enabled him to change the state to read write and then change it to read only if required.

Make Database Read Only

GO

ALTER DATABASE [YourDBName] SET READ_ONLY WITH NO_WAIT

GO

Or

USE [master]

GO

ALTER DATABASE [YourDBName] SET READ_ONLY

GO

Make Database Read/Write

USE [master]

GO

ALTER DATABASE [YourDBName] SET READ_WRITE WITH NO_WAIT

GO

Or

USE [master]

GO

ALTER DATABASE [YourDBName] SET READ_WRITE

GO


Originally published at https://parvtheitgeek.com on February 12, 2014.

List SQL databases in SQL Server — Parv The IT Geek

Have you ever wanted to list out all the databases you have on a server? Well I am going to show you how to do it quickly and easily.

The other day I had completed a migration from a creaky old dinosaur server to a brand new powerful server which always brings a smile on my face as I like new stuff. I stopped all jobs, put the databases into offline, updated my documentation and diarised a week in the future to finish my clean-up work.

The next day my colleague asked me where I had moved the databases as they did not appear to still be on the server. When he listed the databases using the two following procedures it showed that we were 8 databases down which was the same amount I had put into offline mode.

EXEC sp_databases
 EXEC sp_helpdb

I took a step back and did a quick manual search on the server which when you have a few thousand databases can take a while. I found all eight databases and came to the realisation that those two stored procedures can’t find offline databases.

I used the below query to get an accurate list of the databases.

SELECT name
 FROM sys.sysdatabases


Originally published at https://parvtheitgeek.com on February 12, 2014.

List of DBCC commands in SQL Server — Parv The IT Geek

Have you wanted a list of DBCC commands to enable you to do a deep dive? Well I am going to list them out for you.

Below is a list which complied after a discussion with a fellow DBA who said there were only about twenty whilst I was sure there were way more. We were both disappointed when I found only 39 commands but I will be using this to further my knowledge of the DBCC commands. Please note these are the documented functions as we always risk using undocumented functions on Production servers.

DBCC ALLOC CHECK

DBCC ALLOC REPAIR

DBCC CHECKALLOC

DBCC CHECKCATALOG

DBCC CHECKCONSTRAINTS

DBCC CHECKDB

DBCC CHECKFILEGROUP

DBCC CHECKIDENT

DBCC CHECKTABLE

DBCC CLEANTABLE

DBCC DBREINDEX

DBCC dllname (FREE)

DBCC DROPCLEANBUFFERS

DBCC FREEPROCCACHE

DBCC FREESESSIONCACHE

DBCC FREESYSTEMCACHE

DBCC HELP

DBCC INDEXDEFRAG

DBCC INPUTBUFFER

DBCC IVIEW CHECK

DBCC OPENTRAN

DBCC OUTPUTBUFFER

DBCC PROCCACHE

DBCC SHOW_STATISTICS

DBCC SHOWCONTIG

DBCC SHRINKDATABASE

DBCC SHRINKFILE

DBCC SQLPERF

DBCC SSB CHECK

DBCC SYS CHECK

DBCC SYS REPAIR

DBCC TABLE CHECK

DBCC TABLE REPAIR

DBCC TRACEOFF

DBCC TRACEON

DBCC TRACESTATUS

DBCC UPDATEUSAGE

DBCC USEROPTIONS


Originally published at https://parvtheitgeek.com on February 11, 2014.

Find todays date & time in SQL Server part 2 — Parv The IT Geek

Have you ever wanted to return the time found in GETDATE or CURRENT_TIMESTAMP to a higher level off accuracy in SQL? Well I am going to show you how.

The other day I needed to get the current time to greater accuracy due to the time stamp given to some records and I had to use the following functions which give the time to the millisecond.

SELECT SYSDATETIME () — System time and date

SELECT SYSDATETIMEOFFSET () — The time is included.

SELECT SYSUTCDATETIME () — The date and time is returned as UTC time


Originally published at https://parvtheitgeek.com on February 9, 2014.

Have you ever wanted to convert a US date to UK format using SQL? — Parv The IT Geek

Have you ever come across the situation where you have data in US format and need it changed to UK format? Well I am going to show you how to use SQL to easily reformat the date.

I have had this problem several times in the past dealing with international clients who supply databases with the incorrect date format and use the below query to change the format.

SELECT CONVERT(VARCHAR(30), CONVERT(DATETIME, ‘6/1/2009 12:00:00 AM‘, 101), 103);


Originally published at https://parvtheitgeek.com on February 9, 2014.