Use to start all Remote Desktop services that should have been started automatically and are not running:
Get-Service | Where-Object {$_.DisplayName -like "Remote Desktop*"} | Where-Object {$_.StartType -like "Auto*" -and $_.Status -notlike "Run*"} | Start-Service |
Use to find out if a host or device is up/reachable before starting applications. I use this script in Windows autostart, but I only want to launch applications if I know that my company network is reachable (run shell:startup in Windows and place a batch file there that tells PowerShell to run the script at logon: ‘C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file C:\scripts\if_host_is_up_do.ps1’):
#**** | |
# WWW | |
#**** | |
$tryGoogleDNS = Test-NetConnection -ComputerName 8.8.8.8 | |
while ($tryGoogleDNS.PingSucceeded -ne "True") { | |
Start-Sleep 5 | |
$tryGoogleDNS = Test-NetConnection -ComputerName 8.8.8.8 | |
} | |
#***** | |
# HOME | |
#***** | |
$tryHome = Test-NetConnection -ComputerName 192.168.111.1 | |
#Start app if connection is true, but stop if already running | |
if ($tryHome.PingSucceeded -eq "True") { | |
$testApp = Get-Process | Where-Object {$_.ProcessName -eq "caffeine"} | |
if ($testApp) { | |
Write-Host 'Caffeine already running!' | |
} | |
else { | |
Start-Process 'C:\Program Files\caffeine.exe' -ArgumentList '240 -Key:7' #You can modify the key pressed by converting these HEX values to decimal, https://learn.microsoft.com/da-dk/windows/win32/inputdev/virtual-key-codes?redirectedfrom=MSDN | |
} | |
} | |
#***** | |
# WORK | |
#***** | |
$tryWork = Test-NetConnection -ComputerName yourDevice.domain.com | |
#Try connection to work server, if not true, wait for active VPN connection, sleep 5 seconds and try again | |
while ($tryWork.PingSucceeded -ne "True") { | |
Start-Sleep 5 | |
$tryWork = Test-NetConnection -ComputerName yourDevice.domain.com | |
} | |
#Start apps if connection is true | |
if ($tryWork.PingSucceeded -eq "True") { | |
Start-Process 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe' -ArgumentList "https://google.com https://github.com" | |
Start-Process 'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe' -WindowStyle Minimized | |
Start-Process 'C:\Program Files\myApp.exe' | |
Start-Process 'C:\Program Files (x86)\myThirdApp.exe' | |
} | |
Use to export and import Windows NPS server configuration. Run every, e.g. 1 hour, to have 2 servers with same NPS configuration:
#Export configuration on remote server named importServer1 and save to local server share | |
Invoke-Command -ComputerName dc -ScriptBlock { Export-NpsConfiguration -Path \\importServer1\c$\ITOps\nps_conf.xml } | |
#Import configuration on local server | |
Import-NpsConfiguration -Path C:\ITOps\nps_conf.xml |
Use script to RoboCopy files and folders and run a Deduplication job between RoboCopy commands. Wait for Deduplication job to finish before issuing next RoboCopy command:
#Allan Brochorst Kjær | |
#2020-11-23 | |
function Get-DeduppingState { | |
$dedupstate = Get-DedupJob | |
Do { | |
Write-Host "Dedup job still running. Progress is"$dedupstate.Progress"percent finished." | |
Start-Sleep 60 | |
$dedupstate = Get-DedupJob | |
} While ($dedupstate.State -ne $null) | |
} | |
robocopy \\fileshare.int.domain.com\share1\ d:\share1\ /MIR /SEC /R:1 /W:5 /MT | |
Start-DedupJob -Volume "D:" -Type Optimization -Memory 90 | |
Get-DeduppingState | |
robocopy \\fileshare.int.domain.com\share2\ d:\share2\ /MIR /SEC /R:1 /W:5 /MT | |
Start-DedupJob -Volume "D:" -Type Optimization -Memory 90 | |
Get-DeduppingState |
Use script to modify all user profile shares with new server name and new folder name:
#Allan Brochorst Kjær | |
#2020-11-24 | |
$oldfileshares = Get-ADUser -Filter * -Properties HomeDirectory | Where-Object {$_.HomeDirectory -like "*OLD_SERVER_NAME*"} | Select-Object SamAccountName,HomeDirectory | |
foreach($user in $oldfileshares){ | |
$homeDirectory = ($user.HomeDirectory.ToString()) -replace "OLD_SERVER_NAME", "NEW_SERVER_NAME" -replace "OLD_FOLDER_NAME", "NEW_FOLDER_NAME" | |
Set-ADUser $user.SamAccountName -HomeDirectory $homeDirectory | |
} |
Use to get all VHD disks from VMs on Hyper-V hypervisor and export data to CSV
$pcname = $env:computername;Get-VM | Select-Object VMId | Get-VHD | Select ComputerName,VhdFormat,VhdType,Path,@{label='Size(GB)';expression={$_.filesize/1gb -as [int]}} | Export-CSV \\fileshare01\temp\ak\$pcname"_VHD_disks.csv" |
Use to move all .log files older than 1 day to another folder:
Get-ChildItem -Path "C:\Program Files (x86)\Eaton\IntelligentPowerManager\run*.log" | Where{$_.LastWriteTime -lt (Get-Date).AddDays(-1)} | Move-Item -Destination \\fileserver01\destination\ |
Use script to check if HyperV virtual machines are not running. If any are not running, then send warning mail and start them. Check again after 3 minutes and send a new status mail:
$From = "hypervisor@domain.com" | |
$To = "ak@domain.com" | |
$SMTPServer = "monitor.domain.com" | |
$offlineVMs = Get-VM | Where-Object {$_.State -NE "Running"} | |
if ($offlineVMs -ne $null) { | |
$body = $offlineVMs.Name | |
$bodyString = Out-String -InputObject $body | |
Send-MailMessage -From $From -to $To -Subject "VMs are not running! New update in 3 minutes!" -Body $bodyString -SmtpServer $SMTPServer | |
Start-VM -Name * | |
Start-Sleep 180 | |
$offlineVMs = Get-VM | Where-Object {$_.State -NE "Running"} | |
if ($offlineVMs -eq $null) { | |
Send-MailMessage -From $From -to $To -Subject "All VMs are now running!" -Body "All is good!" -SmtpServer $SMTPServer | |
} | |
else { | |
Send-MailMessage -From $From -to $To -Subject "VMs could not be started!" -Body $bodyString -SmtpServer $SMTPServer | |
} | |
} |
Use script to replace text or string in description on MS Active Directory objects:
#Find all groups and replace words in description | |
$description = Get-ADGroup -Filter * -Properties Description | Where-Object {$_.Description -like "*oldText*"} | Select-Object SamAccountName,Description | |
foreach($user in $description){ | |
$objectDescription = ($user.Description.ToString()) -replace "oldText", "newText" | |
$objectDescription = $objectDescription.ToUpper() | |
Set-ADGroup $user.SamAccountName -Description $objectDescription | |
} | |
#Find all users and replace words in description | |
$description = Get-ADUser -Filter * -Properties Description | Where-Object {$_.Description -like "*oldText*"} | Select-Object SamAccountName,Description | |
foreach($user in $description){ | |
$objectDescription = ($user.Description.ToString()) -replace "oldText", "newText" | |
$objectDescription = $objectDescription.ToUpper() | |
Set-ADUser $user.SamAccountName -Description $objectDescription | |
} |
Use script to test connection to website. If the test fails, run and log a traceroute:
$netTest = Test-NetConnection -ComputerName "google.com" | |
if($netTest.PingSucceeded -ne "True") { | |
date | Out-File C:\itops\logs\log.txt -Append | |
Test-NetConnection -ComputerName "google.com" -InformationLevel Detailed | Out-File C:\itops\logs\log.txt -Append | |
tracert google.com | Out-File C:\itops\logs\log.txt -Append | |
echo *** | Out-File C:\itops\logs\log.txt -Append | |
} |
Use to check for available Windows updates and send a warning mail if any:
<# | |
Install module first: | |
Install-Module -Name PSWindowsUpdate | |
Possibly install updates every time: | |
Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -AutoReboot | |
#> | |
$WUhistory = Get-WULastResults | |
$WUhistory | Out-File C:\ITOps\WU_lastInstallationSuccessDate.log -Append | |
$WUlist = Get-WUList | |
If ($null -ne $WUlist) { | |
$mailParams = @{ | |
SmtpServer = 'domain.mail.protection.outlook.com' | |
Port = '25' | |
UseSSL = $true | |
From = 'wsus@domain.dk' | |
To = 'ak@domain.dk' | |
Subject = "Windows Update status, Azure VM NPSAPWPRAZ1" | |
Body = 'Windows updates were last installed on '+ $WUhistory.LastInstallationSuccessDate + '. There are ' + (Get-WUlist).Count + ' available updates.' | |
} | |
Send-MailMessage @mailParams | |
} |