Skip to content

Instantly share code, notes, and snippets.

View giuliov's full-sized avatar

Giulio Vian giuliov

View GitHub Profile
@giuliov
giuliov / elegant-switch.ps1
Created April 29, 2022 09:42
Generate Powershell functions to wrap version-specific functions and avoid switches in code
function pippo_1([string]$x) {
echo "pippo v1: $x"
}
function pippo_2([string]$x) {
echo "pippo v2: $x"
}
function pluto_2([string]$x) {
echo "pluto v2: $x"
@giuliov
giuliov / CollectServerInfo.ps1
Created May 30, 2019 10:36
Dump infor about users, installed packages, firewall
# see https://mcpmag.com/articles/2015/04/15/reporting-on-local-accounts.aspx
Function Get-LocalUser {
[Cmdletbinding()]
Param(
[Parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[String[]]$Computername = $Env:Computername
)
Begin {
@giuliov
giuliov / RunCommand.ps1
Created May 30, 2019 10:26
Run a process analyzing output for errors
# see https://stackoverflow.com/questions/23239127/powershell-stream-process-output-and-errors-while-running-external-process/28440479
function RunCommand
{
[CmdletBinding()]
param (
[Parameter(Mandatory=$True)]
[string] $FileName,
[Parameter(Mandatory=$True)]
[string[]] $Arguments,
[string] $OutputPrefix = "> ",
@giuliov
giuliov / Show-ProjectReferences.ps1
Created May 30, 2019 10:25
this is probably from a blog post
param(
[Parameter(Mandatory)]
[string] $rootProjectFolder,
[Parameter(Mandatory)]
[string] $outputGraphFile,
[string[]] $excludedProjects = @()
)
function Get-ProjectReferences
param
(
[string]$instanceUrl,
[string]$PAT,
[string]$outputFile = "ReleaseDefinitionInfo.csv"
)
if (
[string]::IsNullOrEmpty($instanceUrl) -or
RES_GROUP=myRG
VM_NAME=gvian-optim-vm
# QUERIES TO SEE WHICH VMs NEED TREATMENT
# az network nic list --resource-group $RES_GROUP --query "[].[name, enableAcceleratedNetworking]"
# az vm list -g $RES_GROUP --query "[].[name, storageProfile.imageReference.publisher, storageProfile.imageReference.sku, hardwareProfile.vmSize, storageProfile.osDisk.caching, storageProfile.dataDisks[*].caching]"
# stop VM
az vm deallocate -g $RES_GROUP -n $VM_NAME
# az display clearly the version of the image
az vm image list --publisher MicrosoftSQLServer --offer SQL2014SP2-WS2012R2 --sku Enterprise --location eastus2 --all
# install time of windows package
[timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($(Get-ItemProperty 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion').InstallDate))
# from within an Azure VM
Invoke-RestMethod -Headers @{"Metadata"="true"} -URI "http://169.254.169.254/metadata/instance/compute?api-version=2017-04-02" -Method Get
# it shows the image version BTW
@giuliov
giuliov / Remove-VstsAgents.ps1
Created April 1, 2019 13:22
Works for TFS2018, AzureDevOps use different API
# Get agent pools (Request method: Get):
$uri = "${vstsUrl}/_apis/distributedtask/pools?api-version=${apiVersion}"
Write-Host ">>> calling $uri"
$allPoolsResult = Invoke-RestMethod -Uri $uri -Method Get @restCallSplat
foreach ($poolRec in $allPoolsResult.value) {
Write-Host "Processing Agent Pool '$( $poolRec.name )'"
# Get agents of an agent pool (Request method: Get):
# run this on the Server (IIS) machine
$FQDNs = @("mysite.example","mysite.example.com")
$cert = New-SelfSignedCertificate -DnsName $FQDNs -CertStoreLocation "cert:\LocalMachine\My"
Write-Host "Created Certificate $($cert.Thumbprint)"
Move-Item -Path "cert:\LocalMachine\My\$($cert.Thumbprint)" -Destination "cert:\LocalMachine\WebHosting"
@giuliov
giuliov / ArchivePastMonthLogs.ps1
Created February 11, 2019 17:49
Zip all files from past month
$month = (Get-Date).AddMonths(-1).ToString('yyyy-MM')
New-Item -Name $month -ItemType Directory
Get-ChildItem | where LastWriteTime -lt "${month}-01" | Move-Item -Destination .\${month}
Compress-Archive .\${month}\* -Update -CompressionLevel Optimal -DestinationPath .\${month}.zip
Remove-Item $month -Recurse