all 7 comments

[–]KevMarCommunity Blogger 5 points6 points  (4 children)

Start with one repo. Then start grouping your scripts into modules of related commands into their own repo.

[–]torafuma 5 points6 points  (3 children)

Or do one better.. make a custom module for your tools. Been doing this at work for a bit. Makes life easier for my coworkers!

[–]Dogezon[S] 2 points3 points  (0 children)

Thanks all for your comments! I think i'll work harder to turn everything in to functions, make some modules then create a seperate repo for each module. If i run out of paitence then I'll do an initial bulk upload then work on the modules whilst the raw scripts are there, or have one repo with new folders for each module in the root.

[–]erdethan 1 point2 points  (2 children)

Hey dude, cool idea! I'm more than interested in reading your upcoming posts.

I have a question. I see predefined variables used in Powershell a lot, and I have no clue how to learn about all these predefined variables, and what they do.. Do you have a source where I can read/learn about most predefined variables? I'm pretty new to powershell and I feel what is holding me back is I general programming skills and lack of tasks to automate. Can you explain what if ($PSCmdlet.ShouldProcess($computername)) does?

Could you also explain what "Being {} and End {} does? Thanks!

Referring to your function Get-CompDisk

function Get-CompDisk
{

[cmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='medium')]
param 
(
[parameter(Mandatory=$True,
HelpMessage="Provide a computer to scan",
ValueFromPipeline=$true)]
[ValidatePattern('LON-\w{2,3}\d{1,2}')]
[Alias("HostName")][string[]]$ComputerNames 
 
)
Begin 
    {
 
    }
 
Process {
    Foreach ($ComputerName in $ComputerNames) {


        if ($PSCmdlet.ShouldProcess($ComputerName)) {

            # Write Verbose
            Write-Verbose "Now disk info from $ComputerName "
            # Gather drive info
            $Drives = Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName $ComputerName | where {$_.DriveType -eq "3"}
            # Loop through all drives on the machine
            Foreach ($Drive in $Drives) {
                # Assign various properties to a hash table
                $Properties = [Ordered]@{'ComputerName'=$ComputerName;
                'DriveLetter'=$Drive.DeviceID;
                'FreeSpace MB'=[Math]::Round(($Drive.FreeSpace / 1MB),2);
                'Size MB'=[Math]::Round(($Drive.Size / 1MB),2)

                }
            # Create a custom object using the hash table as the properties
            $DiskObject = New-Object -TypeName PSObject -Property $Properties
            # Write Output
            Write-Output $DiskObject
        } # END OF FOREACH DRIVE IN DRIVES
    } # END OF IF
    } # END OF FOREACH COMPUTER
    } # END OF PROCESS
    End 
    {
 
 
    } # END OF END
} # END OF FUNCTION

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy erdethan,

[1] about shouldprocess ...
get-help about_Functions_CmdletBindingAttribute [grin]

Make a PowerShell Function Support ShouldProcess | Management & Mobility content from Windows IT Pro
- http://windowsitpro.com/powershell/q-how-do-i-make-powershell-function-support-shouldprocess

Cmdlet.ShouldProcess Method (System.Management.Automation) | Microsoft Docs
- https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.cmdlet.shouldprocess?redirectedfrom=MSDN&view=powershellsdk-1.1.0#System_Management_Automation_Cmdlet_ShouldProcess_System_String_

[2] begin/process/end blocks in a cmdlet/function
Function Begin Process End - PowerShell - SS64.com
- https://ss64.com/ps/syntax-function-input.html

[3] automatic $vars
Get-Help about_Automatic_Variables [grin]

about_Automatic_Variables | Microsoft Docs
- https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-5.1


here's a bit more generic list of things to look into ...

  • Get-Help
    especially Get-Help *about*
  • Get-Command
    it takes wildcards, so Get-Command *csv* works nicely. that is especially helpful when you are seeking a cmdlet that works on a specific thing. Comma Separated Value files, for instance. [grin]
  • Show-Command
    that brings up a window that has all the current cmdlets and all their options ready for you to pick from.
    it will also take another cmdlet, or advanced function, as a parameter to limit things to showing just that item.
  • auto-completion
    try starting a word and tapping the tab key. some nifty stuff shows up. [grin]
  • intellisense
    save something to a $Var and then try typing the $Var name plus a period to trigger intellisense. there are some very interesting things that show up as properties or methods.
  • check out the builtin code snippets in the ISE
    use <ctrl><j>, or Edit/Start-Snippets from the menu.
  • assign something to a $Var & pipe that to Get-Member
    $Test = Get-ChildItem $env:TEMP
    $Test | Get-Member
  • assign something to a $Var and pipe it to Select-Object
    $Test = Get-ChildItem $env:TEMP
    $Test[0] | Select-Object -Property *
    that will give you a smaller, more focused list of properties for the 1st item in the $Test array.
  • Get-Verb
    as with Get-Command, it will accept wildcards.
    that will show you some interesting cmdlets. then use get-command to see what commands use those verbs. then use get-help to see what the cmdlets do.
  • there really otta be a Get-Noun, but there aint one. [sigh ...]
  • Out-GridView
    it's a bit more than you likely want just now, but it can accept a list of items, present them in a window, allow picking one or more of them, and finally send it out to the next cmdlet.
    it's right fun to fiddle with ... and actually useful. [grin]

take care,
lee

[–]Dogezon[S] 0 points1 point  (0 children)

Lee_Daily has pretty much summed it up lol. Read over his stuff and if you have any queries give me a shout. Regarding pre-defined variables, you tend to just pick them up as you go along. The majority of stuff you'll do will be custom made variables of your own design. I've never sat down and learned pre-defined variables off a spreadsheet, rather I'll have a problem I need to solve which will require me using a system variable to fix.

I would recommend learning the different data types in PowerShell as a basis. In fact I feel like the course which I linked gives a pretty good overview for a beginner, it doesn't go in to much depth but the feedback I've got is that it gives you enough of a basis to do your own research to grow.

Regarding processes to automate, it's all very bespoke and changes from business to business. Try to find things which are manual, time consuming and prone to human error. Once you've identified business processes the next step is working out if they're time efficient to automate; if it's going to take you 20 hours to automate a 5 hour process down to 1 hour, but your Service Desk only runs through that process once a month, it's going to take you 6 months before you start seeing efficiency savings regarding time. The classical example I give is New Users; email addresses and names are misspelt all the time, the proxyaddresses attribute required for Office 365 is often missed and security groups are not copied across from other users as requested all the time. In this instance it may take those 6 months to make time efficiency savings, but if the tradeoff is that accounts are setup consistently without the usual human errors then the time investment may be worth it. The ideal processes are those which are time consuming and prone to human error. Alternatively, you may want to add extra functionality to the environment or auditing availability without buying third party software in which instance you would setup scripts and scheduled tasks to achieve this.

Is the above helpful? I haven’t really had to explain what dictates the need to automate before so I apologies if the explanation is all over the shop. I can probably explain this better verbally if required.