How to write PowerShell script using TSQL

Sometimes we are not able to connect the server to execute a power shell script that time we can run the same power shell command using TSQL statement,

To enable CMD Shell

sp_configure 'xp_cmdshell',1
RECONFIGURE

To get process list on server

xp_cmdshell 'powershell.exe -command Get-Process '

To get disk space from server

xp_cmdshell 'PowerShell.exe -command "get-diskspace ."'

To get performance counter

xp_cmdshell 'PowerShell.exe -noprofile -command "Get-counter -counter ''\LogicalDisk(*)\% Free Space'' | select -expand countersamples"'

Spread the love

Leave a Comment