# Powershell


## Data storing

### Variable
```powershell
$x="Text"
```

#### Invoking / Data types

##### String
```powershell
[string]$x="Text"
```

##### Int
```powershell
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

```powershell
$arry = @("a", "b", "c")
```

### Hash Table / Dictionary

[MS documentation](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_hash_tables?view=powershell-7.2)

https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-powershell-1.0/ee692803(v=technet.10)?redirectedfrom=MSDN

```powershell
[ordered]@{ <name> = <value>; [<name> = <value> ] ...}
```

(By default sorts by values, I believe ordered means the array trust you as the elements are ordered.)

```powershell
$X = [ordered]@{A=1;C=2;B=3}
```


### Call a single elemnt from the array

#### *Normal* Array

```powershell
@("a", "b", "c")[0]
>> a
```

```powershell
@("a", "b", "c")[-1 ]
>> c
```

#### Hash array

```powershell
@{A=1;C=3;B=2}["A"]
>> 1
```

## User Interaction

### Output

#### Write-Host

Also accepts from redirection

```
Write-Host
     [[-Object] <Object>]
     [-NoNewline]
     [-Separator <Object>]
     [-ForegroundColor <ConsoleColor>]
     [-BackgroundColor <ConsoleColor>]
     [<CommonParameters>]
```


```powershell
Write-Host "text"
```

#### Write-Output

```powershell
Write-Output "text"
```

## Filtering

```powershell
Write-Output "text" | Select-String -pattern $filters -notMatch
```

### Select-Object

```powershell
Select-Object HostName
```

## File interactions

### Read file

```powershell
Get-Content $path
```

### write to file

```powershell
Write-Host $text | Out-File $path
```

## Operators

### Check if last command executed correctly

```powershell
# Do something
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
```powershell
Enter-PSSession –ComputerName $hostname [-Credential username]
```
> If username gets left behind, the current user will be used
### Send command remotely


```powershell
Invoke-Command -ComputerName hostname1, hostname2 -ScroptBlock {$COMMAND}
```

## Text formatting

### ?
https://devblogs.microsoft.com/scripting/understanding-powershell-and-basic-string-formatting/

https://adamtheautomator.com/hyper-v-powershell/

## Objects

### Functions

[//]: # (https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.2)

### Classes

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_classes?view=powershell-7.2

https://livebook.manning.com/book/windows-powershell-in-action-third-edition/chapter-19/64

https://xainey.github.io/2016/powershell-classes-and-concepts/#introduction

# Hypver-V specific 

## Management
[//]: # (https://adamtheautomator.com/hyper-v-powershell/)

### Create new host

```powershell
New-VM -Name "HYPER" -MemoryStartupBytes 512MB
```

### List current VM
```powershell
Get-VMHost
    [[-ComputerName] <String[]>]
    [[-Credential] <PSCredential[]>]
    [<CommonParameters>]
```
#### Example
##### Find VM by name
```powershell
Get-VM -ComputerName Server1
```


# VMWARE

## Connect-VIServer

```powershell
Connect-VIServer
```


## Snapshot

https://developer.vmware.com/docs/powercli/latest/products/vmwarevsphereandvsan/categories/snapshot/

Find hosts, iterate with domains?

$Hostname+.domain.dom