TechShizz | All posts by rich

Get server or computer memory usage via PowerShell (Ideal for server core)

Here is a PowerShell function that I found online:

Function Test-MemoryUsage {
[cmdletbinding()]
Param()

$os = Get-Ciminstance Win32_OperatingSystem
$pctFree = [math]::Round(($os.FreePhysicalMemory/$os.TotalVisibleMemorySize)*100,2)

if ($pctFree -ge 45) {
$Status = "OK"
}
elseif ($pctFree -ge 15 ) {
$Status = "Warning"
}
else {
$Status = "Critical"
}

$os | Select @{Name = "Status";Expression = {$Status}},
@{Name = "PctFree"; Expression = {$pctFree}},
@{Name = "FreeGB";Expression = {[math]::Round($_.FreePhysicalMemory/1mb,2)}},
@{Name = "TotalGB";Expression = {[int]($_.TotalVisibleMemorySize/1mb)}}

}

Simply run this and then you have a new cmdlet "Test-MemoryUsage".

Full credit to:

https://www.petri.com/display-memory-usage-powershell 

Disable Copy and Paste and Right Mouse Click

Javascript to Disable Copy and Paste and Right Mouse Click.

<script type="text/javascript">
            $(document).ready(function () {
            //Disable cut copy paste
            $('body').bind('cut copy paste', function (e) {
            e.preventDefault();
            });
            
            //Disable mouse right click
            $("body").on("contextmenu",function(e){
            return false;
            });
            });
 </script>

 

How to run a Power Shell script from Task Scheduler

Problem

You are unable to get a Power Shell file to execute using the Task Scheduler

Cause

Two reasons:

  1. The execution policy needs to be set to Unrestricted
  2. The command in Task Scheduler needs to adhere to the correct syntax
  3. You must store the script in a place PowerShell trusts.
Solution

Firstly, store your powershell script in a place PowerShell trusts. To find this, open PowerShell and run:

$env:path

You will note that one of the safe places is "C:\Windows\System32".

Next, Set the execution Policy to Unrestricted using PowerShell. Open PowerShell as administrator and run:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted

Press "A" to accept the changes. If you are using a computer account to run the script, you may need to set the execution policy on the local machine to Unrestricted like this:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope LocalMachine

Create a sheduled task as desired. On the actions tab the script must be run like this:

PowerShell.exe "C:\Windows\System32\MyScript.ps1"

Taking care to wrap the path in double quotes. 

Group Managed Service Accounts

#Create KDC root Key (This command takes 10 hours to take effect)

 

Add-KDSRootKey -EffectiveImmediatly

#Install a Group Managed Service Account and configure it to work with the "Web Servers" group and a DNS CNAME which resolves to all machines.

New-ADSeriveAccount -Name GroupMSAAccount -DNSHostName WebClusterA.mydomain.local -PrincipalAllowedToRetrieveManagedPassword "Web Servers"

#Target machines need the RSAT-AD-PowerShell feature instralled

Invoke-Command -ComputerName Web01,Web02,Web03 -ScriptBlock { Install-WindowsFeature RSAT-AD-PowerShell }

#Install the GMSA

Install-ADServiceAccount GroupMSAAccount

#On the target server

Install-ADServiceAccount GroupMSAAccount

Test-ADServiceAccount -Identity GroupMSAAccount

Go to your service you wish to run on a service account, on the logon tab, set the credentials for the service as a network account. Use the browse button to find your MSA (You'll need to change the location to the domain to find the account instead of the local machine. Remove the pre-populated password from the fields and save.