full_path
stringlengths
31
232
filename
stringlengths
4
167
content
stringlengths
0
48.3M
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/unit-testing-your-code/step3/Get-FileContents.ps1
Get-FileContents.ps1
function Get-FileContents { [CmdletBinding()] Param( [Parameter(Mandatory=$True, ValueFromPipeline=$True)] [string[]]$Path ) PROCESS { foreach ($folder in $path) { Write-Verbose "Path is $folder" $segments = $folder -split "\...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/unit-testing-your-code/lab-start/Set-ServiceStatus.ps1
Set-ServiceStatus.ps1
function Set-ServiceStatus { [CmdletBinding()] Param( [string[]]$ServiceName ) foreach ($name in $ServiceName) { $svc = Get-Service $name -EA SilentlyContinue if ($svc) { if ($svc.Status -ne 'Running') { $svc | Start-Service } ...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/unit-testing-your-code/lab-start/Set-ServiceStatus.Tests.ps1
Set-ServiceStatus.Tests.ps1
$here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.' . "$here\$sut" Describe "Set-ServiceStatus" { It "does something useful" { $true | Should Be $false } }
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/unit-testing-your-code/step1/Get-FileContents.Tests.ps1
Get-FileContents.Tests.ps1
$here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.' . "$here\$sut" Describe "Get-FileContents" { It "does something useful" { $true | Should Be $false } }
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/unit-testing-your-code/step1/Get-FileContents.ps1
Get-FileContents.ps1
function Get-FileContents { [CmdletBinding()] Param( [Parameter(Mandatory=$True, ValueFromPipeline=$True)] [string[]]$Path ) PROCESS { foreach ($folder in $path) { Write-Verbose "Path is $folder" $segments = $folder -split "\...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/unit-testing-your-code/lab-results/Set-ServiceStatus.ps1
Set-ServiceStatus.ps1
function Set-ServiceStatus { [CmdletBinding()] Param( [string[]]$ServiceName ) foreach ($name in $ServiceName) { $svc = Get-Service $name -EA SilentlyContinue if ($svc) { if ($svc.Status -ne 'Running') { $svc | Start-Service } ...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/unit-testing-your-code/lab-results/Set-ServiceStatus.Tests.ps1
Set-ServiceStatus.Tests.ps1
$here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.' . "$here\$sut" Describe "Set-ServiceStatus" { It "starts BITS" { Stop-Service BITS $result = Set-ServiceStatus BITS $result.status | Should Be '...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/unit-testing-your-code/step2/Get-FileContents.Tests.ps1
Get-FileContents.Tests.ps1
$here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.' . "$here\$sut" Describe "Get-FileContents" { MkDir TESTDRIVE:\Part1 MkDir TESTDRIVE:\Part1\Part2 MkDir TESTDRIVE:\Part1\Part3 "sample" | Out-File TESTDRIVE:...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/unit-testing-your-code/step2/Get-FileContents.ps1
Get-FileContents.ps1
function Get-FileContents { [CmdletBinding()] Param( [Parameter(Mandatory=$True, ValueFromPipeline=$True)] [string[]]$Path ) PROCESS { foreach ($folder in $path) { Write-Verbose "Path is $folder" $segments = $folder -split "\...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/scripting-at-scale/jobfunctiontemplate.ps1
jobfunctiontemplate.ps1
Function Get-Foo { [cmdletbinding()] Param( [Parameter(Position = 0, ValueFromPipeline)] $This, $That, $TheOtherThing ) Begin { #initialize an array to hold job objects $jobs = @() $mycode = { #define your code to run with parameters if necessary #parameters will need to be ...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/scripting-at-scale/SquareRoot.ps1
SquareRoot.ps1
Function SquareRoot { [cmdletbinding()] Param( [Parameter(Position = 0, Mandatory,ValueFromPipeline)] [int[]]$Value ) Begin { Write-Verbose "[BEGIN ] Starting: $($MyInvocation.Mycommand)" } #begin Process { foreach ($item in $value) { [pscustomobject]@{ Value = $item SquareRoot = [mat...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/scripting-at-scale/progress-samples.ps1
progress-samples.ps1
#Write-Progress demonstrations return "This is a demo script file." 1..50 | foreach { Write-progress -Activity "My main activity" -Status "Status here" -CurrentOperation "Working on $_" start-sleep -Milliseconds 100 } get-process | where starttime | foreach { Write-Progress -Activity "Get-Process" -stat...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/scripting-at-scale/pipelinesamples.ps1
pipelinesamples.ps1
#demonstrate pipeline performance differences return "This is a demo script file." #region pipeline vs parameter $names = get-service | select -expand name #57ms measure-command { $names | get-service } #40ms measure-command { get-service $names } $big = $names+$names+$names+$names+$names #25...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/scripting-at-scale/geteventlogs.ps1
geteventlogs.ps1
#Requires -version 5.0 [cmdletbinding()] Param( [Parameter(Position = 0, Mandatory)] [ValidateNotNullorEmpty()] [string[]]$Computername, [ValidateSet("Error","Warning","Information","SuccessAudit","FailureAudit")] [string[]]$EntryType = @("Error","Warning"), [ValidateSet("System","Application","Security",...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/scripting-at-scale/scaling-backgroundjobs.ps1
scaling-backgroundjobs.ps1
#create a big list of names from whatever list you want to use $computers = 1..5 | foreach {get-content C:\scripts\servers.txt} #using nothing 52 sec #85 items 4:45 measure-command { $all = foreach ($item in $computers) { test-wsman $item } } #using background jobs 56 sec #85 items 4:32 measure-command { $all...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/scripting-at-scale/getdiskspace.ps1
getdiskspace.ps1
Function Get-DiskSpace { [cmdletbinding()] Param( [Parameter(Position = 0, Mandatory)] [string[]]$Computername ) Invoke-Command -scriptblock { Get-CimInstance -ClassName win32_logicaldisk -filter "deviceid='c:'" | Select @{Name="Computername";Expression={$_.SystemName}},DeviceID,Size,Freespace, @{Name="Pct...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/scripting-at-scale/getfoldersize.ps1
getfoldersize.ps1
Function Get-FolderSize { [cmdletbinding()] Param( [Parameter(Position = 0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [ValidateNotNullorEmpty()] [string]$Path = $env:temp ) Begin { Write-Verbose "[BEGIN ] Starting: $($MyInvocation.Mycommand)" } #begin Process { Write-Verbose "[PROCESS] Analyzing:...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/scripting-at-scale/geteventlogs-start.ps1
geteventlogs-start.ps1
#Requires -version 5.0 [cmdletbinding()] Param( [Parameter(Position = 0, Mandatory)] [ValidateNotNullorEmpty()] [string[]]$Computername, [ValidateSet("Error","Warning","Information","SuccessAudit","FailureAudit")] [string[]]$EntryType = @("Error","Warning"), [ValidateSet("System","Application","Security",...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/just-enough-administration-primer/RegisterEndpoint.ps1
RegisterEndpoint.ps1
#Requires -version 5.1 <# this needs to be run ON the remote server which means you'll need to copy .psrc and .pscc files to the server #> $s = New-PSSession -ComputerName chi-fp02 #copy the pssc file to C:\ or some remote folder copy .\shareadmin.pssc -Destination C:\ -ToSession $s -force #copy the mo...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/just-enough-administration-primer/CreateRole.ps1
CreateRole.ps1
#Requires -version 5.1 <# SYNTAX New-PSRoleCapabilityFile [-Path] <String> [-AliasDefinitions <IDictionary[]>] [-AssembliesToLoad <String[]>] [-Author <String>] [-CompanyName <String>] [-Copyright <String>] [-Description <String>] [-EnvironmentVariables <IDictionary>] [-FormatsToProcess <String[]>]...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/just-enough-administration-primer/CreateEndpoint.ps1
CreateEndpoint.ps1
#Requires -version 5.1 <# SYNTAX New-PSSessionConfigurationFile [-Path] <String> [-AliasDefinitions <IDictionary[]>] [-AssembliesToLoad <String[]>] [-Author <String>] [-CompanyName <String>] [-Copyright <String>] [-Description <String>] [-EnvironmentVariables <IDictionary>] [-ExecutionPolicy {Unres...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/emitting-objects-as-output/snippets.ps1
snippets.ps1
# functional overview of tool - will not execute as-is # Query data $os = Get-CimInstance -ClassName Win32_OperatingSystem ` -CimSession $session # Close session $session | Remove-CimSession # Output data $os | Select-Object -Prop @{n='...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/working-with-json/snippets.ps1
snippets.ps1
# json { "Name": "bits", "DisplayName": "Background Intelligent Transfer Service", "Status": 4 } # more json [ { "Name": "BITS", "DisplayName": "Background Intelligent Transfer Service", "Status": 4 }, { "Name": "Bluetooth Device Monitor", "Displ...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/working-with-json/SummaryReport.ps1
SummaryReport.ps1
#requires -version 5.0 <# JSON Processing script for Toolmaking book Process the directory of JSON files and create a summary report in the form of an object with these properties: Number of files processed Total number of items processed Average number of items processed Total number of Errors A...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/tool-design/snippets.ps1
snippets.ps1
# example - this will not execute Get-Content computernames.txt | Test-PCConnection | Get-Whatever # examples - will not execute Get-Content names.txt | Set-MachineStatus Get-ADComputer -filter * | Select -Expand Name | Set-MachineStatus Get-ADComputer -filter * | Set-MachineStatus Set-MachineStatus -ComputerName (G...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/writing-full-help/snippets.ps1
snippets.ps1
# install module install-module platyps # view help file info get-command get-service | Select HelpFile # small chunk of XML help <?xml version="1.0" encoding="utf-8"?> <helpItems xmlns="http://msh" schema="maml"> <!-- Updatable Help Version 5.0.7.0 --> <command:command xmlns:maml="http://schemas.microsoft.co...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/comment-based-help/snippets.ps1
snippets.ps1
# completed... function Get-MachineInfo { <# .SYNOPSIS Retrieves specific information about one or more computers, using WMI or CIM. .DESCRIPTION This command uses either WMI or CIM to retrieve specific information about one or more computers. You must run this command as a user who has permission to remotely query CIM...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/using-net-framework-raw/Example.ps1
Example.ps1
function Get-SpecialFolders { [CmdletBinding()] Param() $folders = [enum]::GetNames([System.Environment+SpecialFolder]) Write-Verbose "Got $($folders.counnt) folders" foreach ($folder in $folders) { [pscustomobject]@{ Name = $folder Path = [environment]::GetFolderPath($...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/using-net-framework-raw/Solution.ps1
Solution.ps1
function ConvertTo-RoundNumber { [CmdletBinding()] Param( [Parameter(Mandatory=$True)] [double]$Number, [int]$DecimalPlaces ) if ($PSBoundParameters.ContainsKey('DecimalPlaces')) { [System.Math]::Round($Number, $DecimalPlaces) } else { [System...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/extending-output-types/snippets.ps1
snippets.ps1
# example function Get-MachineInfo { <# .SYNOPSIS Retrieves specific information about one or more computers, using WMI or CIM. .DESCRIPTION This command uses either WMI or CIM to retrieve specific information about one or more computers. You must run this command as a user who has permission to remotely query CIM or W...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/extending-output-types/End.ps1
End.ps1
function Get-MachineInfo { <# .SYNOPSIS Retrieves specific information about one or more computers, using WMI or CIM. .DESCRIPTION This command uses either WMI or CIM to retrieve specific information about one or more computers. You must run this command as a user who has permission to remotely query CIM or WM...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/extending-output-types/Start.ps1
Start.ps1
function Get-MachineInfo { <# .SYNOPSIS Retrieves specific information about one or more computers, using WMI or CIM. .DESCRIPTION This command uses either WMI or CIM to retrieve specific information about one or more computers. You must run this command as a user who has permission to remotely query CIM or WM...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/extending-output-types/using-add-member.ps1
using-add-member.ps1
function Get-MachineInfo { <# .SYNOPSIS Retrieves specific information about one or more computers, using WMI or CIM. .DESCRIPTION This command uses either WMI or CIM to retrieve specific information about one or more computers. You must run this command as a user who has permission to remotely query CIM or WMI on the ...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/final-exam/Step4.Tests.ps1
Step4.Tests.ps1
$here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.' . "$here\$sut" Describe "Get-TMIPInfo" { # count number of interfaces and # addresses so we know what to expect $count = Get-NetAdapter | Where Status -ne ...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/final-exam/Step1.ps1
Step1.ps1
function Get-TMIPInfo { [CmdletBinding()] Param( [Parameter(Mandatory=$True, ValueFromPipeline=$True)] [string[]]$ComputerName ) } #function
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/final-exam/Step2.ps1
Step2.ps1
# get adapters $comp = 'localhost' $s = New-CimSession -ComputerName $comp $adapters = Get-NetAdapter -CimSession $s | Where Status -ne 'Disconnected' # given an adapter, get addresses # we hardcoded an interface index we know exists $index = 7 $addresses = Get-NetIPAddress -InterfaceIndex 7 -...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/final-exam/Step4.ps1
Step4.ps1
function Get-TMIPInfo { [CmdletBinding()] Param( [Parameter(Mandatory=$True, ValueFromPipeline=$True)] [string[]]$ComputerName ) BEGIN {} PROCESS { ForEach ($comp in $computername) { Write-Verbose "Connecting to $comp" ...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/final-exam/Step3.ps1
Step3.ps1
function Get-TMIPInfo { [CmdletBinding()] Param( [Parameter(Mandatory=$True, ValueFromPipeline=$True)] [string[]]$ComputerName ) BEGIN {} PROCESS { ForEach ($comp in $computername) { ForEach ($adapter in $adapters) { ...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/final-exam/Prototype.ps1
Prototype.ps1
function Get-TMLocalGroup { [CmdletBinding()] Param( [Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)] [string[]]$ComputerName, [string]$LogFailedNamesToPath ) BEGIN {} PROCESS { ...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/going-deeper-with-parameters/snippets.ps1
snippets.ps1
# same thing: Get-Service -Name BITS Get-Service BITS # view help Help get-service # quick example function test { param( [string[]]$one, [int]$two, [switch]$three ) } help test -Full # assigned positions function test { param( [Parameter(Position=1)] [strin...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/going-deeper-with-parameters/Get-DiskCheck.ps1
Get-DiskCheck.ps1
Function Get-DiskCheck { [cmdletbinding(DefaultParameterSetName = "name")] Param( [Parameter(Position = 0, Mandatory, HelpMessage = "Enter a computer name to check", ParameterSetName = "name", ValueFromPipeline)] [Alias("cn")] [ValidateNotNullorEmpty()] [string[]]$Computername, [Parameter(Mandatory, HelpMessage = ...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/publishing-your-tools/snippets.ps1
snippets.ps1
# # Module manifest for module 'PowerShell-Toolmaking' # # Generated by: Don Jones & Jeffery Hicks # # Generated on: 1/3/2017 # @{ # Script module or binary module file associated with this manifest. RootModule = '.\PowerShell-Toolmaking.psm1' # Version number of this module. ModuleVersion = '1.0.0.3' # Supported P...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/basic-controller-scripts-and-menus/processcontroller.ps1
processcontroller.ps1
#Requires -version 5.0 [cmdletbinding()] Param( [Parameter(Position = 0, Mandatory)] [ValidateNotNullorEmpty()] [string[]]$Computername, [ValidateSet("Error","Warning","Information","SuccessAudit","FailureAudit")] [string[]]$EntryType = @("Error","Warning"), [ValidateSet("System","Application","Security",...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/basic-controller-scripts-and-menus/fancymenu.ps1
fancymenu.ps1
#a fancier function using Write-Host Function Invoke-MyMenu { [cmdletbinding()] Param() #start with a clear screen Clear-Host $title = "Help Desk Menu" $menuwidth = 30 #calculate how much to pad left to center the title [int]$pad = ($menuwidth/2)+($title.length/2) #define a here string for the menu options $menu =...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/basic-controller-scripts-and-menus/CimMenuSolution.ps1
CimMenuSolution.ps1
#Requires -version 5.0 clear-host $menu = @" System Information Menu 1 LogicalDisks 2 Services 3 Operating system 4 Computer system 5 Processes 6 Quit "@ Write-Host $menu -ForegroundColor Yellow $coll=@() $item = new-object System.Management.Automation.Host.C...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/basic-controller-scripts-and-menus/choicemenu.ps1
choicemenu.ps1
#this will give you a graphical menu in the PowerShell ISE. Function Invoke-Choice { [CmdletBinding()] Param() #a nested function to prompt for the computername Function promptComputer { [CmdletBinding()] Param() $Computername = Read-Host "Enter a computername or press Enter to use the localhost" if ($Computername ...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/basic-controller-scripts-and-menus/basicmenu-improved.ps1
basicmenu-improved.ps1
#a function based menu Function Invoke-MyMenu { [cmdletbinding()] Param() #start with a clear screen Clear-Host #define a here string for the menu options $menu = @" MyMenu -------------------------- 1. Get services 2. Get processes 3. Get System event logs 4. Check free disk space (MB) 5. Quit Select a m...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/basic-controller-scripts-and-menus/basicmenu.ps1
basicmenu.ps1
#create a basic console based menu #define a here string for the menu options $menu = @" MyMenu -------------------------- 1. Get services 2. Get processes 3. Get System event logs 4. Check free disk space (MB) 5. Quit Select a menu choice "@ #Read-Host writes strings but we can specifically treat the resul...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/converting-a-function-to-a-class/Coded.ps1
Coded.ps1
class ToolmakingMachineInfo { # Properties [string]$ComputerName [string]$OSVersion [string]$SPVersion [string]$OSBuild [string]$Manufacturer [string]$Model [string]$Procs [string]$Cores [string]$RAM [string]$SysDriveFreeSpace [string]$Arch # Construc...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/converting-a-function-to-a-class/ClassFramework.ps1
ClassFramework.ps1
class ToolmakingMachineInfo { # Properties [string]$ComputerName [string]$OSVersion [string]$SPVersion [string]$OSBuild [string]$Manufacturer [string]$Model [string]$Procs [string]$Cores [string]$RAM [string]$SysDriveFreeSpace [string]$Arch # Construc...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/converting-a-function-to-a-class/Start.ps1
Start.ps1
function Get-MachineInfo { <# .SYNOPSIS Retrieves specific information about one or more computers, using WMI or CIM. .DESCRIPTION This command uses either WMI or CIM to retrieve specific information about one or more computers. You must run this command as a user who has permission to remotely query CIM or WM...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/converting-a-function-to-a-class/Revised.ps1
Revised.ps1
class ToolmakingMachineInfo { # Properties [string]$ComputerName [string]$OSVersion [string]$SPVersion [string]$OSBuild [string]$Manufacturer [string]$Model [string]$Procs [string]$Cores [string]$RAM [string]$SysDriveFreeSpace [string]$Arch hidden [stri...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/graphical-controllers-wpf/HelloWorld.ps1
HelloWorld.ps1
#Requires -version 5.0 Add-Type -AssemblyName PresentationFramework #create a window form $form = New-Object System.Windows.Window #define what it looks like $form.Title = "Hello WPF" $form.Height = 200 $form.Width = 300 #create a button $btn = New-Object System.Windows.Controls.Button $btn.Content = ...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/graphical-controllers-wpf/stack-services.ps1
stack-services.ps1
#Requires -version 5.0 Add-Type -AssemblyName PresentationFramework #WPF Demonstration using a stack panel $form = New-Object System.Windows.Window #define what it looks like $form.Title = "Services" $form.Height = 200 $form.Width = 300 #create the stack panel $stack = New-object System.Windows.Controls....
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/graphical-controllers-wpf/DiskStats.ps1
DiskStats.ps1
#disk stat code #start with PowerShell code that already works $computername = "localhost",$env:computername Get-ciminstance -class win32_logicaldisk -filter "drivetype=3" -ComputerName $computername | Select @{Name="Computername";Expression={$_.SystemName}}, DeviceID,@{Name="SizeGB";Expression={$_.Size/1GB -as [int...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/graphical-controllers-wpf/DiskStatForm1.ps1
DiskStatForm1.ps1
#requires -version 4.0 #get the XAML Content <# #include directly in the script file [xml]$xaml = @" <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Disk Report" Height="355" Width="535" Background="#FFBDB3B3"> ...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/graphical-controllers-wpf/display-services.ps1
display-services.ps1
#Requires -version 5.0 Add-Type -AssemblyName PresentationFramework $form = New-Object System.Windows.Window #define what it looks like $form.Title = "Services Demo" $form.Height = 400 $form.Width = 500 $stack = New-object System.Windows.Controls.StackPanel #create a label $label = New-Object System.Wi...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/interlude-changing-your-approach.md/snippets.ps1
snippets.ps1
# consider this: $UserNames = Get-ADUser -Filter * -SearchBase ` "OU=NAME_OF_OU_WITH_USERS3,OU=NAME_OF_OU_WITH_USERS2, OU=NAME_OF_OU_WITH_USERS1,DC=DOMAIN_NAME,DC=COUNTRY_CODE" | Select -ExpandProperty samaccountname $UserRegex = ($UserNames | ForEach{[RegEx]::Escape($_)}) -join "|" $myArray = (Get-ChildItem -Path "...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/analyzing-your-script/snippets.ps1
snippets.ps1
# install module install-module psscriptanalyzer # assumes you're in sample code folder Invoke-ScriptAnalyzer .\Script.ps1 # replace Param() block in script with: Param( [Parameter(ValueFromPipeline=$True, Mandatory=$True)] [Alias('CN','MachineName','Name')] [string[...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/analyzing-your-script/Script.ps1
Script.ps1
function Get-MachineInfo { <# .SYNOPSIS Retrieves specific information about one or more computers, using WMI or CIM. .DESCRIPTION This command uses either WMI or CIM to retrieve specific information about one or more computers. You must run this command as a user who has permission to remotely query CIM or WM...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/analyzing-your-script/Start.ps1
Start.ps1
function Query-Disks { [CmdletBinding(SupportsShouldProcess=$True)] Param( [Parameter(Mandatory=$true)] [string[]]$ComputerName = 'localhost' ) foreach ($comp in $computername) { $logfile = "errors.txt" write-host "Trying $comp" try { gwmi w...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/analyzing-your-script/Result.ps1
Result.ps1
function Get-Disk { [CmdletBinding()] Param( [Parameter(Mandatory=$true)] [string[]]$ComputerName ) foreach ($comp in $computername) { $logfile = "errors.txt" Write-Verbose "Trying $comp" try { Get-CimInstance -ClassName win32_logicaldisk -Co...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/build-a-basic-function-and-module/snippets.ps1
snippets.ps1
# design input parameters function Get-MachineInfo { Param( [string[]]$ComputerName, [string]$LogFailuresToPath, [string]$Protocol, [switch]$ProtocolFallback )} # write the code function Get-MachineInfo { Param( [string[]]$ComputerName, [string]$LogFailuresToPath, [string]$Protocol = "Wsman", [swit...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/measuring-tool-performance/test.ps1
test.ps1
Write-Host "Round 1" Measure-Command -Expression { Get-Service | ForEach-Object { $_.Name } } Write-Host "Round 2" Measure-Command -Expression { Get-Service | Select-Object Name } Write-Host "Round 3" Measure-Command -Expression { ForEach ($service in (Get-Service)) { $ser...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/dynamic-parameters/example.ps1
example.ps1
Param( [string]$UserLevel ) DynamicParam { If ($UserLevel -eq "Administrator") { # create an $AdminType parameter $attr = New-Object System.Management.Automation.ParameterAttribute $attr.HelpMessage = "Enter admin type" $attr.Mandatory = $true $attr.ValueFromPipelineByPropertyName = $true $attrC...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/basic-debugging/snippets.ps1
snippets.ps1
# eco-friendly errors $host.privatedata.errorforegroundcolor = 'green' # code to debug function Get-DriveInfo { [CmdletBinding()] Param( [Parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$True)] [string[]]$ComputerName ) PROCESS { ForEach ($comp i...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/basic-debugging/Step1.ps1
Step1.ps1
function Get-DriveInfo { [CmdletBinding()] Param( [Parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$True)] [string[]]$ComputerName ) PROCESS { Write-Debug "[PROCESS] Beginning" ForEach ($comp in $ComputerName) { Writ...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/basic-debugging/Step2.ps1
Step2.ps1
function Get-DriveInfo { [CmdletBinding()] Param( [Parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$True, ValueFromPipeline=$True)] [string[]]$ComputerName ) PROCESS { Write-Debug "[PROCESS] Beginning" ForEach (...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/basic-debugging/Step4.ps1
Step4.ps1
function Get-DriveInfo { [CmdletBinding()] Param( [Parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$True, ValueFromPipeline=$True)] [string[]]$ComputerName ) PROCESS { Write-Debug "[PROCESS] Beginning" ForEach (...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/basic-debugging/Exercise.ps1
Exercise.ps1
function Set-TMServiceLogon { <# .SYNOPSIS Sets service login name and password. .DESCRIPTION This command uses either CIM (default) or WMI to set the service password, and optionally the logon user name, for a service, which can be running on one or more remote machines. You must run this command as a user wh...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/basic-debugging/Step3.ps1
Step3.ps1
function Get-DriveInfo { [CmdletBinding()] Param( [Parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$True, ValueFromPipeline=$True)] [string[]]$ComputerName ) PROCESS { Write-Debug "[PROCESS] Beginning" ForEach (...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/basic-debugging/Step5.ps1
Step5.ps1
function Get-DriveInfo { [CmdletBinding()] Param( [Parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$True, ValueFromPipeline=$True)] [string[]]$ComputerName ) PROCESS { Write-Debug "[PROCESS] Beginning" ForEach (...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/basic-debugging/Step6.ps1
Step6.ps1
function Get-DriveInfo { [CmdletBinding()] Param( [Parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$True, ValueFromPipeline=$True)] [string[]]$ComputerName ) PROCESS { Write-Debug "[PROCESS] Beginning" ForEach (...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/globalizing-tools/StartingPoint.ps1
StartingPoint.ps1
function Get-MachineInfo { [CmdletBinding()] Param( [Parameter(ValueFromPipeline=$True, Mandatory=$True)] [Alias('CN','MachineName','Name')] [string[]]$ComputerName, [string]$LogFailuresToPath, [ValidateSet('Wsman','Dcom')] ...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/adding-cmdletbinding-and-parameterizing/snippets.ps1
snippets.ps1
# basic function model function test { Param( [string]$ComputerName ) } help test # again: function test { [CmdletBinding()] Param( [string]$ComputerName ) } test # starting with... function Get-MachineInfo { Param( [string[]]$ComputerName, [string]$LogFailuresT...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/using-verbose-warning-informational-output/snippets.ps1
snippets.ps1
# simply notice the use of Write- commands in the below... function Get-MachineInfo { [CmdletBinding()] Param( [Parameter(ValueFromPipeline=$True, Mandatory=$True)] [Alias('CN','MachineName','Name')] [string[]]$ComputerName, [string]$LogFailuresToPath,...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/using-verbose-warning-informational-output/Demo-InformationFunctions.ps1
Demo-InformationFunctions.ps1
#requires -version 5.0 Function Test-Me { [cmdletbinding()] Param() Write-Information "Starting $($MyInvocation.MyCommand) " -Tags Process Write-Information "PSVersion = $($PSVersionTable.PSVersion)" -Tags Meta Write-Information "OS = $((Get-CimInstance Win32_operatingsystem).Caption)" -Tags Meta Write-Verbose "Get...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/handling-errors/snippets.ps1
snippets.ps1
# notice error behavior Get-Service -Name BITS,Nobody,WinRM # try each in turn Get-Service -Name BITS,Nobody,WinRM -EA Continue Get-Service -Name BITS,Nobody,WinRM -EA SilentlyContinue Get-Service -Name BITS,Nobody,WinRM -EA Inquire Get-Service -Name BITS,Nobody,WinRM -EA Stop # moving from this... Write-...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/proxy-functions/snippets.ps1
snippets.ps1
# proxy base $cmd = New-Object System.Management.Automation.CommandMetaData (Get-Command ConvertTo-HTML) [System.Management.Automation.ProxyCommand]::Create($cmd) | Out-File ConvertToHTMLProxy.ps1 # result [CmdletBinding(DefaultParameterSetName='Page', HelpUri='http://go.microsoft.com/fwlink/?LinkID=113290', Remoting...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/proxy-functions/step1/ConvertToHTMLProxy.ps1
ConvertToHTMLProxy.ps1
[CmdletBinding(DefaultParameterSetName='Page', HelpUri='http://go.microsoft.com/fwlink/?LinkID=113290', RemotingCapability='None')] param( [Parameter(ValueFromPipeline=$true)] [psobject] ${InputObject}, [Parameter(Position=0)] [System.Object[]] ${Property}, [Parameter(ParameterSe...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/proxy-functions/lab-results/result.ps1
result.ps1
function Export-TDF { [CmdletBinding(DefaultParameterSetName='Delimiter', SupportsShouldProcess=$true, ConfirmImpact='Medium', HelpUri='http://go.microsoft.com/fwlink/?LinkID=113299')] param( [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [psobject] ${Inpu...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/proxy-functions/step2/ConvertToHTMLProxy.ps1
ConvertToHTMLProxy.ps1
function NewConvertTo-HTML { [CmdletBinding(DefaultParameterSetName='Page', HelpUri='http://go.microsoft.com/fwlink/?LinkID=113290', RemotingCapability='None')] param( [Parameter(ValueFromPipeline=$true)] [psobject] ${InputObject}, [Parameter(Position=0)] [System.Object[]] ${Property},...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/powershell-workflow-primer/solution.ps1
solution.ps1
#Requires -version 5.0 <# - Create a local user account called ITApp with a default password. - Create a folder called C:\ITApp and share it as ITApp giving Everyone ReadAccess and the ITApp user full control. - Under C:\ITApp create folders Test_1 to Test_10 - Set the Remote Registry service to auto start ...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/powershell-workflow-primer/Example2.ps1
Example2.ps1
workflow Example { Param( [string]$Value ) $procs = Get-Process $total_ram = 0 $services = $null $events = $null $result = "" foreach -parallel ($proc in $procs) { $workflow:total_ram += $proc.ws } #foreach Write-Output "Total RAM used $total_ram" ...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/powershell-workflow-primer/Example3.ps1
Example3.ps1
workflow Example { Param( [string]$Value ) $procs = Get-Process $total_ram = 0 $services = $null $events = $null $result = "" foreach -parallel ($proc in $procs) { $workflow:total_ram += $proc.ws } #foreach Write-Output "Total RAM used $total_ram" ...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/powershell-workflow-primer/Example.ps1
Example.ps1
workflow Example { Param( [string]$Value ) $procs = Get-Process $total_ram = 0 $services = $null $events = $null $result = "" foreach -parallel ($proc in $procs) { $workflow:total_ram += $proc.ws } #foreach Write-Output "Total RAM used $total_ram" ...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/powershell-workflow-primer/DirSizer.ps1
DirSizer.ps1
workflow Get-UserFolderSizes { Param( [string[]]$RootPath ) foreach -parallel ($path in $RootPath) { Write-Verbose "Scanning $path" # Get subdirectories $subs = Get-ChildItem -Path $path -Directory Write-Verbose "$($subs.count) user folders" for...
PowerShellCorpus/PowerShellGallery/PowerShell-Toolmaking/1.0.0.19/Chapters/powershell-workflow-primer/Example1.ps1
Example1.ps1
workflow Example { Param( [string]$Value ) $procs = Get-Process $total_ram = 0 $services = $null $events = $null foreach -parallel ($proc in $procs) { $workflow:total_ram += $proc.ws } #foreach Write-Output "Total RAM used $total_ram" sequence { ...
PowerShellCorpus/PowerShellGallery/ISE_Cew/0.1.10/ISE_Cew.Tests.ps1
ISE_Cew.Tests.ps1
$Here = Split-Path -Parent $MyInvocation.MyCommand.Path $PrivateFunctions = Get-ChildItem "$here\Private\" -Filter '*.ps1' -Recurse | Where-Object {$_.name -NotMatch "Tests.ps1"} $PublicFunctions = Get-ChildItem "$here\Public\" -Filter '*.ps1' -Recurse | Where-Object {$_.name -NotMatch "Tests.ps1"} $PrivateFunct...
PowerShellCorpus/PowerShellGallery/ISE_Cew/0.1.10/Public/Save-AllUnnamedFile.ps1
Save-AllUnnamedFile.ps1
Function Save-AllUnnamedFile { <# .SYNOPSIS Saves all Files that have no filename based on the FirstLine of the File .DESCRIPTION This uses $PSISE.CurrentFiles to save all files on the basis that the first line has the following Structure #Script#SP CSOM Testing Lists# ...
PowerShellCorpus/PowerShellGallery/ISE_Cew/0.1.10/Public/Request-YesOrNo.ps1
Request-YesOrNo.ps1
Function Request-YesOrNo { <# .SYNOPSIS Requests from the End User a Yes or a No to the Question passed to it .DESCRIPTION As SYNOPSIS .EXAMPLE $CustomCommitMessage = Get-CustomCommitMessage #> [CmdletBinding()] param ( [Parameter(Mandatory=$false, Position=1)] ...
PowerShellCorpus/PowerShellGallery/ISE_Cew/0.1.10/Public/Get-CustomCommitMessage.ps1
Get-CustomCommitMessage.ps1
Function Get-CustomCommitMessage { <# .SYNOPSIS Grabs Custom Git Commit Message .DESCRIPTION Requests User to Add in Custom Commit Message for the File in question .EXAMPLE $CustomCommit = Request-YesOrNo -title 'Pre-Commit Message' -message 'Do you want to provide a Custom Commit Message' if ($custo...
PowerShellCorpus/PowerShellGallery/ISE_Cew/0.1.10/Public/Save-CurrentISEFile.ps1
Save-CurrentISEFile.ps1
Function Save-CurrentISEFile { <# .SYNOPSIS Saves Current File that you have open in ISE - only saves the Open and active File .DESCRIPTION This uses $PSISE.CurrentFile to save the file on the basis that the first line has the following Structure #Script#SP CSOM Testing Li...
PowerShellCorpus/PowerShellGallery/ISE_Cew/0.1.10/Public/AlignEquals.ps1
AlignEquals.ps1
Function AlignEquals { <# .SYNOPSIS Used to Align text based on the = delimiter - For Splatting .DESCRIPTION Used to Align text based on the = delimiter - For Splatting .EXAMPLE $MyMenu.Submenus.Add("Align = signs in selected text.", { AlignEqu...
PowerShellCorpus/PowerShellGallery/ISE_Cew/0.1.10/Public/Get-AlignedText.ps1
Get-AlignedText.ps1
Function Get-AlignedText { <# .SYNOPSIS Uses the Highlighted Text to Align based on the = delimiter - For Splatting .DESCRIPTION Uses the Highlighted Text to Align based on the = delimiter - For Splatting .EXAMPLE $psise.CurrentFile.Editor.Inse...
PowerShellCorpus/PowerShellGallery/ISE_Cew/0.1.10/Public/New-ISETab.ps1
New-ISETab.ps1
Function New-ISETab { <# .SYNOPSIS Creates a New ISE Tab .DESCRIPTION Creates a New ISE Tab .EXAMPLE nt .EXAMPLE Get-ProxyCode Get-Command | New-IseTab .NOTES AUTHOR ...
PowerShellCorpus/PowerShellGallery/ISE_Cew/0.1.10/Public/Get-ProxyCode.ps1
Get-ProxyCode.ps1
Function Get-ProxyCode { <# .SYNOPSIS Used to get Proxy Code from a function .DESCRIPTION Used to get Proxy Code from a function .EXAMPLE gpc Get-Command | nt .EXAMPLE Get-ProxyCode Get-Command | New-IseTab ...
PowerShellCorpus/PowerShellGallery/ISE_Cew/0.1.10/Public/CleanWhitespace.ps1
CleanWhitespace.ps1
Function CleanWhitespace { <# .SYNOPSIS Used to clean whitespace in Current file for Git Commits .DESCRIPTION Used to clean whitespace in Current file for Git Commits .EXAMPLE $MyMenu.Submenus.Add("Clean up whitespace", { CleanWhitespac...
PowerShellCorpus/PowerShellGallery/ISE_Cew/0.1.10/Public/Save-AllNamedFile.ps1
Save-AllNamedFile.ps1
Function Save-AllNamedFile { <# .SYNOPSIS Saves all Files in a PowerShell ISE Tab that already have a filename .DESCRIPTION This uses $PSISE.CurrentFiles to save all files that have been edited in the session This will save the file in the location dete...
PowerShellCorpus/PowerShellGallery/LabBuilder/0.8.3.1116/lib/private/utils.ps1
utils.ps1
<# .SYNOPSIS Throws a custom exception. .DESCRIPTION This cmdlet throws a terminating or non-terminating exception. .PARAMETER errorId The Id of the exception. .PARAMETER errorCategory The category of the exception. It must be a valid [System.Management.Automation.ErrorCategory] value. .PA...