The past few months I’ve been fiddling with Powershell and leveraging it at work. For instance, with a recent roll-out of an in-place Windows 10 upgrade (it was a money thing) needs a lot of garbage cleaned up (Store, Xbox, ect)…
The command that was listed in an old TS Windows 10 “cleanup” video Get-AppxPackage -allusers | Remove-AppxPackage
doesn’t exactly work correctly in 1709 (Fall Creator’s Update). In previous releases this command would remove ALL the apps for ALL users, but now it’s per user.
You might think “run as admin” and feel free to drop the -allusers
flag. But that actually won’t do anything for the user at hand. In this case, the command needs to be run as that user… BUT admin rights are still needed.
The first option could be make the user an admin while needing this, but that isn’t very efficient. Fortunately Windows handles user rights in a strange way, so pipe-ing in Get-Credential
is the sauce to make this magic work.
Thus, to remove the garbage like the store for a non-admin is Get-AppxPackage | Remove-AppxPackage | Get-Credential
and use an admin account.
Now for something a little more niche… Hyper-V Replication can be a somewhat unreliable, so it’s a good idea to keep an eye on it. But checking through Hyper-V Manager (especially with a large number of VM’s) is tedious and annoying.
Measure-VMReplication
is command that solves this, but it doesn’t show all of the useful data outright (not to mention the -ComputerName
flag doesn’t seem to work the way other commands usually do. So using Invoke-Command
is what I needed to be able to run this remotely.
Even though Measure-VMReplication
doesn’t show all of the useful data, it does have it available in its containing objects. So I wrote a function that would make it more useful.
function Check-VMReplication {
param ($virtsrv)
$tstsrv = Test-Connection $virtsrv -Count 1 -Quiet
if ($tstsrv -eq “True”){
Invoke-Command -ComputerName $virtsrv -ScriptBlock {
Measure-VMReplication | % {
Write-Host $.Name $.ReplicationState $.ReplicationHealth “with” $.ReplicationErrors “errors.” $.SuccessfulReplicationCount “succeeded, last replica” $.LastReplicationTime “with” ("{0:N0}" -f ($_.PendingReplicationSize/1MB)) “MB pending”
}
}
}
else { Write-Error -Category DeviceError -Message $virtsrv “could not be reached via PING.”}
}
For those who don’t know much about powershell functions, they’re a lot like custom commands… Or how alias
functions in BASH.
The function sends a ping, and runs Measure-VMReplication
and displays info for each VM such as errors and pending data. If the ping fails, it displays an error message. Just run Check-VMReplication HyperVHost
Building on my last function, I threw together a loop.
function Monitor-VMReplication {
param ($mvirtsrv)
while ($true) {cls; Get-Date -DisplayHint DateTime; Check-VMReplication $mvirtsrv; sleep 10}
}
Monitor-VMReplication HyperVHost
it’ll clear the text, show the time, and run the check command I wrote.
I’m working on a few other functions that I think would be useful but I’m still working out the details of how I want to go about writing it. One of said functions is to search an OU in Active Directory for computer names to put into other commands’ -ComputerName
flag.