Skip to content

Instantly share code, notes, and snippets.

@keyan1603
Last active May 16, 2024 08:24
Show Gist options
  • Save keyan1603/40f3dc3343293acda5dff1ca188d42aa to your computer and use it in GitHub Desktop.
Save keyan1603/40f3dc3343293acda5dff1ca188d42aa to your computer and use it in GitHub Desktop.
Kubernetes - Some useful Powershell commands
# Get into Windows Pod and access Powershell inside the pod
kubectl exec -it <podname> -- powershell
# To view the content inside a file, for example web.config
Get-Content web.config
# Get top 100 lines of the file
Get-Content web.config -First 100
# Get last 100 lines of the file
Get-Content web.config -Last 100
# Add content to a file
Add-Content C:\Windows\System32\drivers\etc\hosts "127.0.0.1 mytestwebsite.com"
# Replace content in a file
(Get-Content Web.config).Replace('<httpProtocol>','<!--<httpProtocol>') | Set-Content Web.config
# Create a file
New-Item -Path . -Name "testfile1.txt" -ItemType "file"
# Create a file with Content
New-Item -Path . -Name "MyTestFile.txt" -ItemType "file" -Value "This is a sample string."
# Create a folder
New-Item -Path "c:\" -Name "logs" -ItemType "directory"
# Create multiple files
New-Item -ItemType "file" -Path "c:\logs\log1.txt", "c:\logs\log2.txt"
# Delete a file
Remove-Item web.config
# Rename a file
Rename-Item ReWriteRules.config ReWriteRules_old.config.disabled
# Get all the Scheduled task in the Pod
Get-ScheduledTask
# Get specific details of a Scheduled task
Get-ScheduledTaskInfo 'My Scheduled job'
# Restart App pool
Restart-WebAppPool (Get-Website -Name MyWebProject).applicationPool
# Add new IIS binding
New-IISSiteBinding -Name "MyTestWebsite" -BindingInformation "*:80:mytestwebsite.com" -Protocol http
# Compress files to Archive
Compress-Archive -Path C:\App_Config -DestinationPath C:\app_config.zip
# Expand Archive
Expand-Archive C:\app_config.zip -DestinationPath C:\App_Config
# Browse an URL inside the Pod
iwr https://www.google.com/ -UseBasicParsing
# Install a certificate - Option 1
$pwd = ConvertTo-SecureString -String "MyPassword" -AsPlainText -Force
Import-PfxCertificate -FilePath C:\certificates\mysitecertificate.p12 -CertStoreLocation Cert:\LocalMachine\My\ -password $pwd
# Install a certificate - Option 2
Import-PfxCertificate -FilePath C:\certificates\MySiteCertificate.p12 -CertStoreLocation Cert:\LocalMachine\My\ -password (ConvertTo-SecureString -String "MyPassword" -AsPlainText -Force)
# Test connection to SQL server
$conn = New-Object System.Data.SqlClient.SqlConnection
$conn.ConnectionString = "Data Source=MySitecoreDB;Initial Catalog=sitecore_master;User ID=admin;Password=password123;"
$conn.Open()
$conn.Close()
# To test connectivity with Solr
$url = "https://mysolrurl/solr"
$webClient = New-Object System.Net.Webclient
$rawHTML = $webClient.DownloadString($url)
echo $rawHTML
# To test connectivity to endpoints to specific ports
Test-NetConnection mytestendpointurl.com -port 443
# Download a file from internet
Invoke-WebRequest "https://download.microsoft.com/download/1/2/8/128E2E22-C1B9-44A4-BE2A-5859ED1D4592/rewrite_amd64_en-US.msi" -OutFile "c:\downloads"
# Install using installer
Start-Process msiexec.exe -ArgumentList '/i', 'C:\install\rewrite_amd64_en-US.msi', '/quiet', '/norestart' -NoNewWindow -Wait
# Copy files from local to Pod
kubectl cp Rules.config <pod namespace>/<podname>:c:\inetpub\wwwroot\rules.config
# Copy files from Pod to local
kubectl cp <pod namespace>/<podname>:\inetpub\wwwroot\web.config web.config
# To view all the environment variables (you can use this to get the connection strings applied through environment variables and secrets)
Get-ChildItem -Path Env:
# To view a specific variable
Get-ChildItem -Path Env:\Sitecore_ConnectionStrings_Master
# To view the CPU and RAM usage (press ctrl + c to stop)
$totalRam = (Get-CimInstance Win32_PhysicalMemory | Measure-Object -Property capacity -Sum).Sum
while($true) {
$date = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$cpuTime = (Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue
$availMem = (Get-Counter '\Memory\Available MBytes').CounterSamples.CookedValue
$date + ' > CPU: ' + $cpuTime.ToString("#,0.000") + '%, Avail. Mem.: ' + $availMem.ToString("N0") + 'MB (' + (104857600 * $availMem / $totalRam).ToString("#,0.0") + '%)'
Start-Sleep -s 2
}
systeminfo |find "Available Physical Memory"
Get-ChildItem c:\inetpub\wwwroot\app_data\logs\ | Measure-Object -Property Length -sum
(gci c:\inetpub\wwwroot\app_data\logs\ | measure Length -s).sum / 1Mb
((gci -force c:\inetpub\wwwroot\app_data\logs\ -Recurse -ErrorAction SilentlyContinue| measure Length -s).sum / 1Gb)
Get event logs:
Get-EventLog -LogName System -Newest 50
Get-ChildItem Cert:\LocalMachine\My\
Get-ChildItem Cert:\LocalMachine\My\ | Select-Object NotAfter, Subject
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment