Powershell Tool Time: The Tool Framework

  • Comments posted to this topic are about the item Powershell Tool Time: The Tool Framework

  • Do functions need to be stored in a specific location?

  • 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 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.

  • @john-2 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.


    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 😀

  • Thanks for this series Mike! I look forward to it!

    CE - Microsoft

  • 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?

    412-977-3526 call/text

  • 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"'

  • 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.

  • 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'

    412-977-3526 call/text

  • Would it be prudent to include the $hostname you've specified as a column in the output so that you can script this, execute it against several systems, and keep the results straight? (or is that a future post spoiler?)

    Do people use the PowerShell ISE? I saw that mentioned once but most people seem to stick to the shell prompt.

    Thanks for starting this, I'm anxious to see more!

  • I absolutely recommend the ISE. The basic shell is functional, but the ISE provides a better experience in general for working on scripts. I use it all the time, though for code development I've been working with the VS2013 plugin for Powershell.

    As for adding $hostname, you could definitely do that. I wrote this function as a simple way for me to get a free space report on demand (as opposed to a regularly executing script), but extending it out wouldn't be difficult.

  • robert.sterbal 56890 (8/19/2015)

    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?

    It depends on how you define "remote support". In my case I work from home 100% and do not always have one machine in a client environment to run scripts from; in that scenario then yes you have to move your scripts to each "site" or environment you want to run them on.

    Another scenario I had at a previous employer, I used a VM on my laptop and connected via VPN to each client environment. In that case if the domain supports authentication over the VPN, then you can use the remote ability in PowerShell cmdlets to pull information. You would not have to push the scripts to every server.

    With regard to using xp_cmdshell, I have used this in one PowerShell project to build out disk space report for all the servers in a client environment (included pulling all the data to a database and then alerting based on configured thresholds). The one thing you have to understand is if you want to do something with the data returned by PowerShell, via xp_cmdshell, it takes a bit more than just having it call the script/commands. I used a variation of Laerte's script here for using XML[/url].

    Shawn Melton
    Twitter: @wsmelton
    Blog: wsmelton.github.com
    Github: wsmelton

  • Agreed. I have watched some of the Microsoft virtual academy labs and they state that the are moving everyone to the ise.

  • Thanks for the help on this.

Viewing 15 posts - 1 through 15 (of 16 total)

You must be logged in to reply to this topic. Login to reply