header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
running powershell from SQL
Last Post 29 Sep 2011 10:13 AM by Gene Hunter DBA extraordinaire. 4 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Not Resolved
ITUser is Offline
New Member
New Member
Posts:55
Avatar

--
28 Jan 2009 06:14 AM
    I am trying to run a PowerShell script  from an SQL script.
    halr9000User is Offline
    PowerShell MVP, Site Admin
    Advanced Member
    Advanced Member
    Posts:565
    Avatar

    --
    28 Jan 2009 06:24 AM
    Can you run shell commands from T-SQL? I don't know it that well. I do know however that you can run PowerShell stuff from within the management studio for SQL 2008. Probably in Agent jobs as well, although I've not tried.
    Community Director, PowerShellCommunity.org
    Co-host, PowerScripting Podcast
    Author, TechProsaic
    ITUser is Offline
    New Member
    New Member
    Posts:55
    Avatar

    --
    28 Jan 2009 06:26 AM
    I know that you can run PowerShell from management studio, but I was seeing if anyone knew the script to do so.
    Chad MillerUser is Offline
    Basic Member
    Basic Member
    Posts:160
    Avatar

    --
    28 Jan 2009 02:23 PM

    Provided xp_cmdshell is enabled you can execute something like this.

    xp_cmdshell 'powershell.exe -c get-service'

    Gene Hunter DBA extraordinaireUser is Offline
    New Member
    New Member
    Posts:1
    Avatar

    --
    29 Sep 2011 10:13 AM
    declare @svrName varchar(255)
    declare @sql varchar(400)
    --by default it will take the current server name, we can the set the server name as well
    set @svrName = @@SERVERNAME
    set @sql = 'powershell.exe -c "Get-WmiObject -Class Win32_Volume -Filter ''DriveType = 3'' | select name,capacity,freespace | foreach{$_.name+''|''+$_.capacity/1048576+''%''+$_.freespace/1048576+''*''}"'
    --creating a temporary table
    CREATE TABLE #output
    (line varchar(255))
    --inserting disk name, total space and free space value in to temporary table
    insert #output
    EXEC xp_cmdshell @sql

    --script to retrieve the values in GB from PS Script output
    select @@SERVERNAME as servername ,rtrim(ltrim(SUBSTRING(line,1,CHARINDEX('|',line) -1))) as drivename
    ,round(cast(rtrim(ltrim(SUBSTRING(line,CHARINDEX('|',line)+1,
    (CHARINDEX('%',line) -1)-CHARINDEX('|',line)) )) as Float)/1024,0) as 'capacityGB'
    ,round(cast(rtrim(ltrim(SUBSTRING(line,CHARINDEX('%',line)+1,
    (CHARINDEX('*',line) -1)-CHARINDEX('%',line)) )) as Float) /1024 ,0)as 'freespaceGB',
    round(100 * (round(cast(rtrim(ltrim(SUBSTRING(line,CHARINDEX('%',line)+1,
    (CHARINDEX('*',line) -1)-CHARINDEX('%',line)) )) as Float) /1024 ,0))/
    (round(cast(rtrim(ltrim(SUBSTRING(line,CHARINDEX('|',line)+1,
    (CHARINDEX('%',line) -1)-CHARINDEX('|',line)) )) as Float)/1024,0)),0) as percentfree

    from #output
    --select * from #output
    where line like '[A-Z][:]%'
    order by drivename
    --script to drop the temporary table
    --drop table #output
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer