forked from IvanovOleg/powershell_tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lesson5.ps1
131 lines (105 loc) · 3.74 KB
/
lesson5.ps1
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<#
Здесь может находиться описание работы скрипта
#>
param(
[Parameter(Mandatory=$false)]
[string]
$firstName = "Oleg",
[Parameter(Mandatory=$false)]
[string]
$lastName = "Ivanov",
[Parameter(Mandatory=$false)]
[string]
$email,
[Parameter(Mandatory=$true)]
[securestring]
$password,
[Parameter(Mandatory=$false)]
[bool]
$execution = $true
)
#
# Variables
#
$user = @{
"firstname" = $firstName
"lastname" = $lastname
"email" = $email
}
$connectionParameters = @(
"server=127.0.0.1",
"username=${firstname}",
"password=${password}"
)
$Env:yourPassword = "qwerty" # имитируем наличие пееменной окружения
$connectionString = $connectionParameters -join ';' # объеденим элементы массива в строку
$newPassword = ConvertTo-SecureString $Env:yourPassword -AsPlainText -Force # поместим пароль в строку в зашифрованом виде
$externalip = ((Invoke-WebRequest http://myexternalip.com/raw).Content).trim() # получить внешний ip с помошью веб-запроса
#
# Functions
#
function CreatePowershellCredential {
param([string]$username, [securestring]$password)
$powershellCredential = New-Object -TypeName pscredential -ArgumentList $username, $password
return $powershellCredential
}
function DecodeSecureString {
$byteString = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($script:psCredential.password)
$plaintextPassword = [Runtime.InteropServices.Marshal]::PtrToStringBSTR($byteString)
return $plaintextPassword
}
function DeleteFile ($path) {
Remove-Item -Path $path -ErrorAction SilentlyContinue # проигнорирует ошибку если файл не существует
Write-Output "The ${path} file has been deleted"
}
function GetFakeResource {
$uri = "http://jsonplaceholder.typicode.com/posts/1"
$header = @{
"Content-Type" = "application/json"
}
$result = Invoke-RestMethod -Uri $uri -Method "GET" -Headers $header
return $result
}
function SetFakeResource {
$uri = "http://jsonplaceholder.typicode.com/posts"
$header = @{
"Content-Type" = "application/json"
}
$body = @{
"data" = @{
"title" = "foo"
"body" = "bar"
"userId" = 1
}
} | ConvertTo-Json
$result = Invoke-RestMethod -Uri $uri -Method "POST" -Headers $header -Body $body
return $result
}
#
# Execution
#
if ($execution) {
$Env:yourPassword
$connectionString
$newPassword
# -foreground позволяет выделить текст другим цветом
Write-Host "`n[ Creating a powershell credential ]`n" -foreground "green"
$psCredential = CreatePowershellCredential -username $firstName -password $newPassword
$psCredential.GetType()
$psCredential
Write-Host "`n[ Decoding a secure string ]`n" -foreground "green"
$decodedPassword = (DecodeSecureString)
$decodedPassword
Write-Host "`n[ Deleting a file ]`n" -foreground "green"
DeleteFile -path ".\delete.me"
Write-Host "`n[ Getting a fake resource using a REST API call ]`n" -foreground "green"
$response = (GetFakeResource)
$response
Write-Host "`n[ Creating a fake resource using a REST API call ]`n" -foreground "green"
$response = (SetFakeResource)
$response
}
# Домашнее задание
# http://jsonplaceholder.typicode.com/
# https://github.com/PoshCode/PowerShellPracticeAndStyle
# https://en.wikipedia.org/wiki/Don't_repeat_yourself