This commit is contained in:
OriolFilter 2022-04-04 04:35:11 +02:00
parent aeb411790f
commit 393d025b14
3 changed files with 148 additions and 6 deletions

View File

@ -7,18 +7,71 @@
```ps
$x="Text"
```
#### Invoking / Data types
##### String
```ps
[string]$x="Text"
```
##### Int
```ps
PS C:\Windows\system32> [int]$x=1
PS C:\Windows\system32> [int]$x="1"
PS C:\Windows\system32> [int]$x="1a"
Cannot convert value "1a" to type "System.Int32". Error: "Input string was not in a correct format."
At line:1 char:1
+ [int]$x="1a"
+ ~~~~~~~~~~~~
+ CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException
+ FullyQualifiedErrorId : RuntimeException
```
##### Bool
(MS Documentation)[https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_booleans?view=powershell-7.2]
### Array
```ps
$arry = @("a", "b", "c")
```
### Hash Array
### Hash Table / Dictionary
[MS documentation](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_hash_tables?view=powershell-7.2)
```ps
[ordered]@{ <name> = <value>; [<name> = <value> ] ...}
```
$X=@{ <name> = <value>; [<name> = <value> ] ...}
(By default sorts by values, I believe ordered means the array trust you as the elements are ordered.)
```ps
$X = [ordered]@{A=1;C=2;B=3}
```
### Call a single elemnt from the array
#### *Normal* Array
```ps
@("a", "b", "c")[0]
>> a
```
```ps
@("a", "b", "c")[-1 ]
>> c
```
#### Hash array
```ps
@{A=1;C=3;B=2}["A"]
>> 1
```
## User Interaction
@ -55,12 +108,13 @@ Write-Output "text"
```ps
Write-Output "text" | Select-String -pattern $filters -notMatch
```
## File interactions
### Read file
```ps
Get-Contents $path
Get-Content $path
```
### write to file
@ -69,7 +123,6 @@ Get-Contents $path
Write-Host $text | Out-File $path
```
## Operators
### Check if last command executed correctly
@ -79,13 +132,14 @@ Write-Host $text | Out-File $path
echo $?
```
### Looping
#### For
#### For each
(MS Documentation)[https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-7.2]
## Management
### Access remote PS server
```ps
@ -99,6 +153,21 @@ Enter-PSSession ComputerName $hostname [-Credential username]
Invoke-Command -ComputerName hostname1, hostname2 -ScroptBlock {$COMMAND}
```
## Text formatting
### ?
https://devblogs.microsoft.com/scripting/understanding-powershell-and-basic-string-formatting/
## Objects
### Functions
### Classes
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_classes?view=powershell-7.2
[//]: # (https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.2)
# Hypver-V specific

2
Testing/hyperv_list Normal file
View File

@ -0,0 +1,2 @@
host1
host2 host3

View File

@ -1 +1,72 @@
## For example
# Author: Oriol Filter
# Date: 04/04/2022
$SCRIPT_PATH=$PSScriptRoot
## Objects
class VirtualizationServer {
[string]$URL
[bool]$Authenticated=$false
}
class HypervServer: VirtualizationServer{}
class VMWareServer: VirtualizationServer{}
# Get hyperv's
filter Get-Hyperv
{
$HYPERV_FILE = "$SCRIPT_PATH/hyperv_list"
## In a future use a yaml as a .conf (with the hyperv and vmware listed)
#$__HYPERV_FILE = Get-Content "$_SCRIPT_PATH"
$HYPERV_FILE = "host1
host2 host3"
$HYPERV_LIST = $HYPERV_FILE.Split()
$HYPERV_ARRAY=@()
# $i=0
foreach ($url in $HYPERV_LIST)
{
if ($url)
{
[HypervServer]$new_hyerv=[HypervServer]::new()
$new_hyerv.URL=$url
$HYPERV_ARRAY+=$new_hyerv
# $i++
}
}
return $HYPERV_ARRAY
}
function Check-Hyperv-Servers($hv_arr) {
foreach ($connection in $hv_arr)
{
$connection.cla
}
}
function Print-Connections-Status($vs_arr) {
foreach ($connection in $vs_arr)
{
$connection
}
}
$hyperv_arr=Get-Hyperv
#$hyperv_arr.GetType()
Print-Connections-Status($hyperv_arr)
#echo "1"
# Advanced params?
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.2