Skip to content

Instantly share code, notes, and snippets.

# Syntax
<condition> ? <if-true> : <if-false>
# Example
$message = (Test-Path $path) ? "Path exists" : "Path not found"
# Example
$SomeValue ??= 100
# taki sam efekt w PowerShell 5.1
if(-not $SomeValue)
{
$SomeValue = 100
}
# PowerShell 7 - ForEach and Parallel
$logNames = 'Security','Application','System','Windows PowerShell','Microsoft-Windows-Store/Operational'
$logEntries = $logNames | ForEach-Object -Parallel {
Get-WinEvent -LogName $_ -MaxEvents 10000
} -ThrottleLimit 5
$logEntries.Count
50000
# Install or update PowerShell 7 on Windows 10
iex "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI"
# Install or update PowerShell 7 on Linux
wget https://aka.ms/install-powershell.sh; sudo bash install-powershell.sh; rm install-powershell.sh
# One-liner to install or update PowerShell 7 on Windows 10
iex "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI"
# One-liner to install or update PowerShell 7 on Linux
wget https://aka.ms/install-powershell.sh; sudo bash install-powershell.sh; rm install-powershell.sh
resource "azurerm_resource_group" "rg" {
name = "rg-akademia-tf"
location = "west europe"
}
resource "azurerm_sql_server" "instance" {
name = "sql-tf"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
version = "12.0"
$ResourceGroup = New-AzResourceGroup -Name "rg-akademia-ps" -Location "westeurope" -Force
$Credentials = $(New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'sqladmin',
$(ConvertTo-SecureString -String "JakieTrudneHaslo99" -AsPlainText -Force))
$ParamSqlServer = @{
ResourceGroupName = $ResourceGroup.ResourceGroupName
ServerName = "sql-ps"
Location = $ResourceGroup.Location
SqlAdministratorCredentials = $Credentials
function Set-Tls12
{
<# Get-Help Set-Tls12 -Online #>
[CmdletBinding(HelpURI = "https://akademiapowershell.pl/11")]
[Alias('tls12')]
param()
Write-Verbose "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
}
$SqlInstance = 'SERWER-SQL\MSSQLSERVER17'
$Database = 'master'
$ScriptBlock = {
param(
[Parameter(Mandatory)]
$SQLInstance,
[Parameter(Mandatory)]
$Database,
[Parameter(Mandatory)]
function Join-PathElement {
param(
[string[]]$Element
)
[array]$FullPath = $Element[0]
for ($i = 1; $i -lt $Element.Count; $i++) {
$FullPath += Join-Path -Path $FullPath -ChildPath $Element[$i]
}
$Id = $FullPath.Count - 1