Skip to content

Instantly share code, notes, and snippets.

View bovrhovn's full-sized avatar
🏠
Working from home

Bojan Vrhovnik bovrhovn

🏠
Working from home
View GitHub Profile
@bovrhovn
bovrhovn / task-extension-on-failure.cs
Created April 12, 2023 13:27
Executes a callback function when a Task encounters an exception.
//authored by Steven Giesel https://steven-giesel.com/blogPost/d38e70b4-6f36-41ff-8011-b0b0d1f54f6e
//usage: await GetResultAsync().OnFailure(ex => Console.WriteLine(ex.Message));
public static async Task OnFailure(this Task task, Action<Exception> onFailure)
{
try
{
await task.ConfigureAwait(false);
}
catch (Exception ex)
{
@bovrhovn
bovrhovn / task-extension-fire-and-forget.cs
Created April 12, 2023 13:25
This means that you want to start a task but you don't want to wait for it to finish. This is useful when you want to start a task but you don't care about the result (non-critical tasks)
// this is authored by Steven Giesel https://steven-giesel.com/blogPost/d38e70b4-6f36-41ff-8011-b0b0d1f54f6e
//usage: SendEmailAsync().FireAndForget(errorHandler => Console.WriteLine(errorHandler.Message));
public static void FireAndForget(
this Task task,
Action<Exception> errorHandler = null)
{
task.ContinueWith(t =>
{
if (t.IsFaulted && errorHandler != null)
errorHandler(t.Exception);
@bovrhovn
bovrhovn / Connect-PSRemoting.ps1
Created March 31, 2023 11:56
Connect to machine via PowerShell remoting
$Skip = New-PSSessionOption -SkipCACheck -SkipCNCheck
Enter-PSSession -ComputerName "IPOFVM" -port "5986" -Credential (Get-Credential) -useSSL -SessionOption $Skip
@bovrhovn
bovrhovn / Invoke-RunToEnableRemotePS.ps1
Created March 31, 2023 11:52
enable remote PS via run command on Azure VM
Invoke-AzVMRunCommand -ResourceGroupName 'resourcegroupname -Name 'nameofthevm' -CommandId 'RunPowerShellScript' -ScriptString 'Enable-PSRemoting -Force
New-NetFirewallRule -Name "Allow WinRM HTTPS" -DisplayName "WinRM HTTPS" -Enabled True -Profile Any -Action Allow -Direction Inbound -LocalPort 5986 -Protocol TCP
$thumbprint = (New-SelfSignedCertificate -DnsName $env:COMPUTERNAME -CertStoreLocation Cert:\LocalMachine\My).Thumbprint
$command = "winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{Hostname=""$env:computername""; CertificateThumbprint=""$thumbprint""}"
cmd.exe /C $command'
@bovrhovn
bovrhovn / Enable-PSRemoteOnVM.ps1
Created March 31, 2023 11:41
enable remote PS on Azure Virtual Machine
Enable-PSRemoting -Force
New-NetFirewallRule -Name "Allow WinRM HTTPS" -DisplayName "WinRM HTTPS" -Enabled True -Profile Any -Action Allow -Direction Inbound -LocalPort 5986 -Protocol TCP
$thumbprint = (New-SelfSignedCertificate -DnsName $env:COMPUTERNAME -CertStoreLocation Cert:\LocalMachine\My).Thumbprint
$command = "winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{Hostname=""$env:computername""; CertificateThumbprint=""$thumbprint""}"
cmd.exe /C $command
@bovrhovn
bovrhovn / Set-RuleForWinRM.ps1
Created March 31, 2023 11:35
Set WinRM access for VM on Network Security Group
$networkSecurityGroup=Get-AzNetworkSecurityGroup | Where-Object -Property Id -EQ $nic.NetworkSecurityGroup[0].Id
## Create the security rule. ##
Add-AzNetworkSecurityRuleConfig -Name WinRM -NetworkSecurityGroup $networkSecurityGroup `
-Description "Allow WinRM" -Access Allow -Protocol Tcp -Direction Inbound -Priority 300 `
-SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 5986
## Updates the network security group. ##
Set-AzNetworkSecurityGroup -NetworkSecurityGroup $networkSecurityGroup
@bovrhovn
bovrhovn / Get-VmNSG.ps1
Created March 31, 2023 11:11
Get NSG from VM name and resource group
$vmName = "vm-pwsh-remote"
$rgName = "CSARG"
$vm = Get-AzVM -VMName $vmName -ResourceGroupName $rgName
$nic = Get-AzNetworkInterface -ResourceId $vm.NetworkProfile.NetworkInterfaces[0].Id
Get-AzNetworkSecurityGroup | Where-Object -Property Id -EQ $nic.NetworkSecurityGroup[0].Id | format-table Name, Location, ResourceGroupName, ProvisioningState, ResourceGuid
@bovrhovn
bovrhovn / GoAdmin-Windows-Terminal.ps1
Created March 21, 2023 13:08
Run PowerShell as admin from command line
Function GoAdminFunc(){
If (!([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]'Administrator'))
{
Start-Process -Verb RunAs wt.exe '-p "PowerShell"'
Exit
}
}
@bovrhovn
bovrhovn / Get-EnvVars.ps1
Last active March 21, 2023 12:51
PowerShell function for reading env file from
param(
[Parameter(HelpMessage = "File with env variables", Mandatory = $true)]
[string]
$EnvFile
)
if (!(Test-Path $EnvFile -PathType Leaf)) {
Write-Error "$EnvFile is not a file."
return;
}
@bovrhovn
bovrhovn / test.env
Created March 21, 2023 12:18
env file example
PageCounter=10
FilePath=c:\Work