Hi guys, this tip is simple. Did you know that you can run your PowerShell scripts, cmdlets, functions from the SSMS Query Editor ?
yeahhh..using xp_cmdshell
. Just remember two things:
1 – Use Results to text
2 – The context of the computer that will run the PowerShell is the Query Editor SQL Server connection. So if you are connected in the Server2 and running PowerShell functions, be sure that this functions are in your profile in Server2
1 – Disk Space :
Using Get-Diskspace function (@sqlvariant) Locally :
1: xp_cmdshell 'powershell.exe -command "get-diskspace ."'
Remoting :
1: xp_cmdshell 'powershell.exe -command "get-diskspace -servername ObiWan"'
All Servers in Servers.txt
1: xp_cmdshell 'powershell.exe -command "get-diskspace -servername (get-content c:\temp\servers.txt)"'
You can also use get-counter and \LogicalDisk(*)\% Free Space to get all counter instances :
1: xp_cmdshell 'powershell.exe -noprofile -command "Get-counter -counter ''\LogicalDisk(*)\% Free Space'' | select -expand countersamples"'
Remoting :
1: xp_cmdshell 'powershell.exe -noprofile -command "Get-counter -computername ObiWan -counter ''\LogicalDisk(*)\% Free Space'' | select -expand countersamples"'
2 – Services :
1: xp_cmdshell 'powershell.exe -noprofile -command "Get-Service"'
Only Services Stopped :
1: xp_cmdshell 'powershell.exe -noprofile -command "Get-Service | where {$_.status -eq ''Stopped''}"'
Only SQL Server Services stopped :
1: xp_cmdshell 'powershell.exe -noprofile -command "Get-Service -name *sql* | where {$_.status -eq ''Stopped''}"'
Remoting :
1: xp_cmdshell 'powershell.exe -noprofile -command "Get-Service -computername ObiWan -name *sql* | where {$_.status -eq ''Stopped''}"'
Yeahhh !!!!!!
As today is the Rock International Day, I will quote Joan Jett I Love Rock ‘N’ Roll : (with a bit change)
I love rock n’ roll (and PowerShell)
So put another dime in the jukebox, baby
I love rock n’ roll (and PowerShell)
So come and take your time and dance with me
#PowerShellLifeStyle



