-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path3.0-CodeReuse.ps1
More file actions
71 lines (52 loc) · 2.27 KB
/
Copy path3.0-CodeReuse.ps1
File metadata and controls
71 lines (52 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#Code reuse and extension
#Reusing scripts
#open 3.1-FileCount.ps1
#now call the script
C:\IntroToPowershell\3.1-FileCount.ps1 'C:\IntroToPowershell\'
C:\IntroToPowershell\3.1-FileCount.ps1 -PathName 'notvalid'
#We can convert the script to a function call for better reuse
#open 3.2-FileCount_Function.ps1
. C:\IntroToPowershell\3.2-FileCount_Function.ps1
Get-FileCount 'C:\IntroToPowershell\'
Remove-Item Function:\Get-FileCount
#We can also write our own modules to extend Powershell
#let's take our file count function, open 5c-FileCount_module.psm1
#once we import it, we can re-use it
Import-Module C:\IntroToPowershell\3.3-FileCount_module.psm1
Get-FileCount 'C:\IntroToPowershell\'
Get-FileCount 'Not Valid'
#functions can be extremely useful for code reuse. For example, if we re-wrote our code for getting a free space report:
function Get-FreeSpace{
param([string] $hostname = ($env:COMPUTERNAME))
gwmi win32_volume -computername $hostname | where {$_.drivetype -eq 3} | Sort-Object name `
| ft name,label,@{l="Size(GB)";e={($_.capacity/1gb).ToString("F2")}},@{l="Free Space(GB)";e={($_.freespace/1gb).ToString("F2")}},@{l="% Free";e={(($_.Freespace/$_.Capacity)*100).ToString("F2")}}
}
Get-FreeSpace TARKIN
Get-FreeSpace localhost
#We can get a listing of all our available modules
Get-Module -ListAvailable
#working with the profile
#We can use any of the functions in the profile, they're loaded at session start
#This function is part of my default profile
Get-FreeSpace
#easiest way to edit is...
powershell_ise $profile
#The profile may not exist, so you'd have to create it
#Let's rename the profile so we can create it, then we'll clean up afterwards.
$profilebak = "$profile.bak"
Move-Item $profile $profilebak
if(!(Test-Path $profile)){New-Item -Path $profile -ItemType file -Force}
powershell_ise $profile
#Add 'Import-Module SQLPS -disablenamechecking'
#Add the following function
function Beam-MeUp{
param([string] $target)
"Scotty, beam $target up."
}
#If we make changes, we can reload by "executing" the profile
. $profile
Beam-MeUp -target Kirk
Beam-MeUp -target Spock
#see, created. Boom. Now I'm going to move the previous profile back.
Remove-Item $profile
Move-Item $profilebak $profile