Archive
powershell crash course part 2
Hi all ,
part 2 of PowerShell .. took a back stage and I just figured out that I do not need to reinvent the wheel .. because some of the dedicated powershell
sites do a lot better work at it … YES ,, its extremely useful for SQL admins too .. so I will direct you to that sites instead …
SITES –> http://powershell.com/cs/
on this sites .. GO to TAB named EBOOKV2 .. which has 18 lesson online eBook
and it covers most of the basics we need to start with …
–> http://powershell.com/cs/blogs/ebookv2/default.aspx
below I will provide small descriptions of lessons and topics covered in the same ..
Chapter 1. The PowerShell Console
This chapter will introduce you to the PowerShell console and show you how to configure it, including font colors and sizes, editing and display options. Topics Covered: Starting PowerShell First Steps with the Console Incomplete
Chapter 2. Interactive PowerShell
what are my privileges or rights on an SQL instance
whoops!!! it happens we are given certain rights on SQL instance .. we tend to ask “WHAT are MY RIGHTS on THIS instance ??? ”
and you may be said “WELL FIND IT OUT yourSELF”
then we search for a PILL .. which could help us on the spot ..
fn_my_permissions is similar to the PILL .. that could work wonders for you ..
the results you get after running this will vary according to your access level on an instance
select * from fn_my_permissions (NULL,’SERVER’) — gives access details at server level
select * from fn_my_permissions (NULL,’Database”) — gives access details at DB level
list of privileges if your are sysadmin on a SQL server instance
select * from fn_my_permissions (NULL,’SERVER’)
| entity_name | subentity_name | permission_name |
| server | CONNECT SQL | |
| server | SHUTDOWN | |
| server | CREATE ENDPOINT | |
| server | CREATE ANY DATABASE | |
| server | ALTER ANY LOGIN | |
| server | ALTER ANY CREDENTIAL | |
| server | ALTER ANY ENDPOINT | |
| server | ALTER ANY LINKED SERVER | |
| server | ALTER ANY CONNECTION | |
| server | ALTER ANY DATABASE | |
| server | ALTER RESOURCES | |
| server | ALTER SETTINGS | |
| server | ALTER TRACE | |
| server | ADMINISTER BULK OPERATIONS | |
| server | AUTHENTICATE SERVER | |
| server | EXTERNAL ACCESS ASSEMBLY | |
| server | VIEW ANY DATABASE | |
| server | VIEW ANY DEFINITION | |
| server | VIEW SERVER STATE | |
| server | CREATE DDL EVENT NOTIFICATION | |
| server | CREATE TRACE EVENT NOTIFICATION | |
| server | ALTER ANY EVENT NOTIFICATION | |
| server | ALTER SERVER STATE | |
| server | UNSAFE ASSEMBLY | |
| server | ALTER ANY SERVER AUDIT | |
| server | CONTROL SERVER |
select * from fn_my_permissions (NULL,’DATABASE’)
| entity_name | subentity_name | permission_name |
| server | CONNECT SQL | |
| server | SHUTDOWN | |
| server | CREATE ENDPOINT | |
| server | CREATE ANY DATABASE | |
| server | ALTER ANY LOGIN | |
| server | ALTER ANY CREDENTIAL | |
| server | ALTER ANY ENDPOINT | |
| server | ALTER ANY LINKED SERVER | |
| server | ALTER ANY CONNECTION | |
| server | ALTER ANY DATABASE | |
| server | ALTER RESOURCES | |
| server | ALTER SETTINGS | |
| server | ALTER TRACE | |
| server | ADMINISTER BULK OPERATIONS | |
| server | AUTHENTICATE SERVER | |
| server | EXTERNAL ACCESS ASSEMBLY | |
| server | VIEW ANY DATABASE | |
| server | VIEW ANY DEFINITION | |
| server | VIEW SERVER STATE | |
| server | CREATE DDL EVENT NOTIFICATION | |
| server | CREATE TRACE EVENT NOTIFICATION | |
| server | ALTER ANY EVENT NOTIFICATION | |
| server | ALTER SERVER STATE | |
| server | UNSAFE ASSEMBLY | |
| server | ALTER ANY SERVER AUDIT | |
| server | CONTROL SERVER |
Enjoy 🙂
get running job name from sysprocesses
sometimes we need to find out the job details if multiple jobs are running at once and are involved in blocking or any other server performance issues …
the master..sysprocesses have column named program_name that represents the program from where a particular SPID is originating ..
for running SQL jobs this will be a hexadecimal string .. we can use below method to get the actual name of the job from sysjobs table
1> get the substring of the running job from sysprocess .. have filtered out generic refresher ETC
select substring(program_name,55,7) ‘to search’
from master..sysprocesses
where program_name like ‘%agent%’ and program_name not like ‘%DatabaseMail – SQLAGENT -%’
and program_name not in (‘SQLAgent – Alert Engine’,’SQLAgent – Generic Refresher’,’SQLAgent – Email Logger’,’SQLAgent – Job invocation engine’,’SQLAgent – Job Manager’)
output will be similar to below
Output :
to search
2FE3386
A68D437
3253286
Get the job details from MSDB database by using the results you got from above query and replace them in where clause
1>
select *
from msdb..sysjobs
where job_id like ‘%A68D437%’
OR
2> if you have more than one job running replace below job_id %strings%
select *
from msdb..sysjobs
where job_id like ‘%A68D437%’ or job_id like ‘%4E742EB%’ or job_id like ‘%FAEDF18%’
Enjoy 🙂

