SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Powershell Tool Time: The Tool Framework


Powershell Tool Time: The Tool Framework

Author
Message
sake.god
sake.god
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 170
Comments posted to this topic are about the item Powershell Tool Time: The Tool Framework
Jon Lacey
Jon Lacey
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 383
Do functions need to be stored in a specific location?
Kedazs
Kedazs
SSC-Enthusiastic
SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)

Group: General Forum Members
Points: 125 Visits: 220
This is great! I would love to start using powershell more!

Question - Do you need to load the function every powershell session? Or, can they be stored somewhere so they can be called easily?

Thanks!
sake.god
sake.god
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 170
Thanks for the feedback. To answer both of your questions, there are a few ways to allow functions to be easily called. I will be covering them in upcoming articles, but the short answer is either your profile or a module.
rahul.me.sharma
rahul.me.sharma
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 154
@John Lacey

Function only remain active in your current session. If you plan to use a specific function frequently you can create a profile of your's and insert all the required functions in it. Every time you open the Powershell console all those functions will be loaded in your session.

@all
You can also try and feed the output to a grid using "| Out-GridView" at the end of the script to give it a more fancy look :-D
Jared Karney
Jared Karney
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13924 Visits: 3697
Thanks for this series Mike! I look forward to it!

Thanks,
Jared
PFE - Microsoft
SQL Know-It-All
How to post data/code on a forum to get the best help - Jeff Moden
robert.sterbal 56890
robert.sterbal 56890
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2418 Visits: 1236
I do remote support, so I'm guessing I'll have to create the functions on each site.

Can I run this from xp_cmdshell?
rahul.me.sharma
rahul.me.sharma
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 154
You can pass the remote system name to the function as a parameter and you should get results for that server. Like :

Get-FreeSpace -HostName Servername

the only thing you need to take care of is that the remote system is connect to your machine through the network.

You can also use xp_cmdshell to run this. My advise would be to save the function script as a file and then use xp_cmdshell to call that file.

exec xp_cmdshell 'powershell.exe -c "c:\temp\Get_freespace.ps1"'
sake.god
sake.god
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 170
Stay tuned for other articles, we'll go over how you can manage these and run them remotely or deploy them to different sites.

And you won't use xp_cmdshell to either manage or use these. Or at least shouldn't.
robert.sterbal 56890
robert.sterbal 56890
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2418 Visits: 1236
Generally I'd have to create the file by echoing the contents....

My current script to get hard drive space is fine, except for the lack of delimiters in the numbers:

master..xp_cmdshell 'WMIC LogicalDisk Where DriveType="3" Get DeviceID,FileSystem,FreeSpace,Size'



here is the plain text:

master..xp_cmdshell 'WMIC LogicalDisk Where DriveType="3" Get DeviceID,FileSystem,FreeSpace,Size'
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search