How to setup IIS App Pool Cpu rate limit using powershell - powershell

I am trying to set cpu rate limit i was able to perform this action in UI using the action link
https://theitbros.com/set-cpu-usage-limit-for-an-application-pool-iis-8/
but i want to set three values below using powershell
CPU limit=20%
CPU limit Action= Killw3p
Cpu Limit Interval= 1
Iam already using powershell to setup startmode to always running.
set-itemproperty IIS:\AppPools\AddressBroker.API -name startMode -value AlwaysRunning
I have tried something like below but does not work.
set-itemproperty IIS:\AppPools\AddressBroker.API -name CPU -value Limit=20%

If you're looking for the PowerShell solution, you'll need to correct the property name:
Import-Module WebAdministration
$appPool = Get-Item IIS:\AppPools\AddressBroker.API
$appPool | Set-ItemProperty -name "cpu.limit" -value 20000
$appPool | Set-ItemProperty -name "cpu.action" -value "ThrottleUnderLoad"
$appPool | Set-ItemProperty -name "startMode" -value "AlwaysRunning"

I figured out the correct command that works for me is below.
C:\windows\system32\inetsrv\appcmd.exe set apppool AppPOOLName /cpu.limit:20000 /cpu.action:KillW3wp /cpu.resetInterval:00:01:00

According to your description, I suggest you could try to use below command to acheive your requirement.
C:\windows\system32\inetsrv\appcmd.exe set apppool /cpu.limit:20000 /cpu.action:KillW3wp /cpu.resetInterval:00:01:00
Result:

Related

PowerShell - Having trouble setting Specific Recycle Time in IIS App Pool

When it comes to IIS App Pool Recycling, under the Recycling settings, you can set a Specific Time to Recycle. You can also set multiple times by doing 00:15, 04:30. See following screenshot.
I am attempting to set multiple Recycle Times using PowerShell. The odd thing is, when I run my code - it only shows 00:15 in the GUI - not 00:15 and 04:30.
But - if I run this check, it shows both times set.
Get-ItemProperty "IIS:\AppPools\DefaultAppPool" -Name Recycling.periodicRestart.schedule.collection
Now, if I go into the GUI and add a time that way - it shows, but it wipes out 04:30.
I am wanting to know why when I set multiple times in PS - they do not all show in the GUI. Other people can run my code and see both times in the GUI. Any thoughts?
Thanks.
Import-Module WebAdministration
$RecycleTime1 = '00:15'
$RecycleTime2 = '04:30'
$PrivateMemory = 1468006
$sites = Get-ChildItem 'IIS:\AppPools\' <# | Where-Object { $_.recycling.periodicRestart.schedule.TotalMilliseconds -gt 1 } #>
Clear-ItemProperty "IIS:\AppPools\DefaultAppPool" -Name Recycling.periodicRestart.schedule
Set-ItemProperty "IIS:\AppPools\DefaultAppPool" Recycling.periodicRestart.schedule -Value #{value=$RecycleTime1}
New-ItemProperty "IIS:\AppPools\DefaultAppPool" -Name Recycling.periodicRestart.schedule -Value #{value=$RecycleTime2}
Get-ItemProperty "IIS:\AppPools\DefaultAppPool" -Name Recycling.periodicRestart.schedule.collection
EDIT:
Looks like value may need an index. Trying to figure out if so.
AppCmd set config
-section:system.applicationHost/applicationPools
"/+applicationPoolDefaults.recycling.periodicRestart.schedule.[#0,value='04:00:00']"
/commit:apphost

Use powershell to configure "Use start fullscreen" setting?

Windows 10 allows you to configure Settings > Start > Use Start full screen, I'm trying to find a way to configure this through powershell/dsc scripting/automation. I was able to find the MDM and GPO documentation (https://learn.microsoft.com/en-us/windows/configuration/windows-10-start-layout-options-and-policies) but this does not appear to apply to desktop Windows 10 Pro - powershell has no commands/cmdlets with GP* nouns.
The scripts below, inspired by the .bat-files in this article adjust the local policies and should probably work. I have tested on 10.0.16299.431 (Enterprise).
Based on the article (Created by Shawn Brink, January 24th 2015):
To force fullscreen:
$forceStartSizePath = "\Software\Policies\Microsoft\Windows\Explorer"
New-ItemProperty -Path "HKCU:$forceStartSizePath" -Name "ForceStartSize" -Value 2 -Force
New-ItemProperty -Path "HKLM:$forceStartSizePath" -Name "ForceStartSize" -Value 2 -Force
Stop-Process -name explorer
To force normal mode:
$forceStartSizePath = "\Software\Policies\Microsoft\Windows\Explorer"
New-ItemProperty -Path "HKCU:$forceStartSizePath" -Name "ForceStartSize" -Value 1 -Force
New-ItemProperty -Path "HKLM:$forceStartSizePath" -Name "ForceStartSize" -Value 1 -Force
Stop-Process -name explorer
To reset to default:
$forceStartSizePath = "\Software\Policies\Microsoft\Windows\Explorer"
Remove-ItemProperty -Path "HKCU:$forceStartSizePath" -Name "ForceStartSize"
Remove-ItemProperty -Path "HKLM:$forceStartSizePath" -Name "ForceStartSize"
Stop-Process -name explorer
Note: The last line (making explorer restart) may not desirable, but it will make sure the settings are picked up instantly. Your screen will flicker (if running local) as explorer is restarted.
Also; if parts of the registry-path is missing, you will get an error message. Use Test-Pathand New-Item to check for and create the missing part of the path.

Get Specific Recycling Time For IIS With PowerShell

I'm trying to find the specific time that my app pool is set to recycle on my server using PowerShell. I know it's set to recycle at 1 AM daily on the test server that I'm looking at. I'm running:
Get-ItemProperty -Path IIS:\AppPools\AppPool -Name recycling.periodicRestart.time
and the value that's showing is 00:00:00
For testing purposes, I attempted to run:
Set-ItemProperty -Path IIS:\AppPools\AppPool -Name recycling.periodicRestart.time -Value 3.00:00:00
I checked to see how this changed. The "Specific Time(s)" field still reads 1:00 AM, but now the "Regular Time Intervals (in minutes)" is set to 4320 minutes. So apparently I'm looking at the wrong value... Any idea how I can see the value in the "Specific Time" field?
Here you go:
Set-ItemProperty -Path IIS:\AppPools\DefaultAppPool -Name recycling.periodicRestart.schedule -Value #{value = '03:00:00'}
Get it with:
Get-ItemProperty -Path IIS:\AppPools\DefaultAppPool -Name recycling.periodicRestart.schedule.collection

Windows VM proxy aware at provisioning time

We are currently leveraging a VNet at Azure that is configured to force all traffic over our site-to-site connection to on-premise networks and then out through our corporate firewall. All HTTP/HTTPS traffic is proxied.
This is causing significant issue in that newly-provisioned VMs can not see the outside world to access necessary configuration items like Azure Extensions or even no-proxied internal IPs.
To date, I've created a perverse work-around via PowerShell where I provision the VM, bootstrap it with Chef which overrides the proxy settings in order to get to the Chef server which then configures the proxies.
But, until the user fires up Internet Explorer, it does not set the proxies so that they can be used in PowerShell. Specifically, before IE is fired up, [System.Net.GlobalProxySelection]::Select is empty. Once IE has been started, it is populated.
I also tried preconfiguring an image and sysprep'ing it, but sysprep wipes the proxy settings.
As a final step, I created a PowerShell script which I thought would set the proxies. I was going to use this script via Azure Script Extension (assuming it is on the VM by default), but I can't get it to give me the desired results.
So, my question is, how do I make a Windows 2012 R2 (and Windows 2008 R2) newly provisioned VM proxy aware so that other processes in the provisioning sequence will work (i.e., extensions like the Chef extension)?
Here is the script I created (that doesn't perform as expected):
# See: http://www.geoffchappell.com/notes/windows/ie/firstrun.htm
$regKey = 'HKCU:\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings'
$proxyServerToDefine = 'http://<PROXY URI>:<PORT>'
Set-ItemProperty -path $regKey ProxyEnable -value 1
Set-ItemProperty -path $regKey ProxyServer -value $proxyServerToDefine
Set-ItemProperty -path $regKey ProxyOverride -value '137.185.235.196;137.185.235.199'
New-Item -Path 'HKLM:\\Software\\Policies\\Microsoft\\Internet Explorer' -Name 'Main' -Force # Does not exist by default
Set-ItemProperty -Path 'HKLM:\Software\Policies\Microsoft\Internet Explorer\Main' -Name DisableFirstRunCustomize -Value 1
(New-Object System.Net.WebClient).Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
The trick here was assigning both the HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings AND system account's Internet Settings.
Suggestion: Set your proxy and overrides up once, confirm that it works and then grab the registry values to use in your script.
Here's the list of values I set to make the Users and the System accounts proxy aware:
$userReg = 'HKCU:\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings'
Set-ItemProperty -path $userKey ProxyEnable -value 1
Set-ItemProperty -path $userKey ProxyServer -value ''
Set-ItemProperty -path $userKey ProxyOverride -value ''
$sysReg = 'HKU:\\S-1-5-18\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings'
Set-ItemProperty -path $sysReg ProxyEnable -value 1
Set-ItemProperty -path $sysReg ProxyServer -value ''
Set-ItemProperty -path $sysReg ProxyOverride -value ''
$sysRegCon = 'HKU:\\S-1-5-18\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\Connections'
$DefaultConnectionSettings = ([byte[](0x46,0x00,...))
$SavedLegacySettings = ([byte[](0x46,0x00,...))
Set-ItemProperty -path $sysRegCon DefaultConnectionSettings -value $DefaultConnectionSettings
Set-ItemProperty -path $sysRegCon SavedLegacySettings -value $SavedLegacySettings
Hope this helps someone else more rapidly down the path...

Automate process of Disk Cleanup cleanmgr.exe without user intervention

I am developing a powershell script file which shall execute some disk cleanup without user intervention. The user shall not be able to configure anything.
When I run cleanmgr.exe /d c: sageset:1 a popup window appears to select files/folders to be cleaned(cleanup options).
This will create a registry entry containing the settings with the cleanup options and after this, you can run cleanmgr.exe /sagerun:1 which will actually execute the cleanup.
Is there a way to specify the cleanup options directly with powerhell/command line(without the need to manually select things to be deleted)?
The following Powershell script automates CleanMgr.exe. In this case, it removes temporary files and runs the Update Cleanup extension to purge superseded Service Pack Backup files (Windows 10 now does this automatically via a scheduled task). To automate other extensions, create a "StateFlags0001" property in the corresponding Registry key, as done in the New-ItemProperty lines. You will find the Registry key names in the "VolumeCaches" branch.
As far as being silent, this script attempts to start CleanMgr.exe in a hidden window. However, at some point CleanMgr spawns new processes which are visible and must be waited on separately.
Write-Host 'Clearing CleanMgr.exe automation settings.'
Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\*' -Name StateFlags0001 -ErrorAction SilentlyContinue | Remove-ItemProperty -Name StateFlags0001 -ErrorAction SilentlyContinue
Write-Host 'Enabling Update Cleanup. This is done automatically in Windows 10 via a scheduled task.'
New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Update Cleanup' -Name StateFlags0001 -Value 2 -PropertyType DWord
Write-Host 'Enabling Temporary Files Cleanup.'
New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files' -Name StateFlags0001 -Value 2 -PropertyType DWord
Write-Host 'Starting CleanMgr.exe...'
Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:1' -WindowStyle Hidden -Wait
Write-Host 'Waiting for CleanMgr and DismHost processes. Second wait neccesary as CleanMgr.exe spins off separate processes.'
Get-Process -Name cleanmgr,dismhost -ErrorAction SilentlyContinue | Wait-Process
$UpdateCleanupSuccessful = $false
if (Test-Path $env:SystemRoot\Logs\CBS\DeepClean.log) {
$UpdateCleanupSuccessful = Select-String -Path $env:SystemRoot\Logs\CBS\DeepClean.log -Pattern 'Total size of superseded packages:' -Quiet
}
if ($UpdateCleanupSuccessful) {
Write-Host 'Rebooting to complete CleanMgr.exe Update Cleanup....'
SHUTDOWN.EXE /r /f /t 0 /c 'Rebooting to complete CleanMgr.exe Update Cleanup....'
}
The PowerShell logic provided below is dynamic and ready for use or automation with the sageset options all being selected and no user interaction being required. This was inspired by multiple answers and comments from this post.
Note: I've adjusted for my needs and used successfully without any issues on multiple remote and local Windows 10 systems in particular.
Run on Local System
Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\*' | % {
New-ItemProperty -Path $_.PSPath -Name StateFlags0001 -Value 2 -PropertyType DWord -Force
};
Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:1' ##-WindowStyle Hidden
Run on Remote System
$cred = Get-Credential "domain\administrator";
Invoke-Command -ComputerName "computer004" {
Process {
Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\*' | % {
New-ItemProperty -Path $_.PSPath -Name StateFlags0001 -Value 2 -PropertyType DWord -Force
};
Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:1' -WindowStyle Hidden
}
} -AsJob -Credential $cred
Supporting Resources
cleanmgr
Invoke-Command
-AsJob
Run the command as a background job on a remote computer.
Use this parameter to run commands that take an extensive time to complete.
Get-Credential
Automate process of Disk Cleanup cleanmgr.exe without user intervention
Creating a Disk Cleanup Handler
You can use cleanmgr /verylowdisk to silently automate all the cleanup steps.
The only solution I found is to manually set the registry values like this:
...
#Set StateFlags0012 setting for each item in Windows 8.1 disk cleanup utility
if (-not (get-itemproperty -path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders' -name StateFlags0012 -ErrorAction SilentlyContinue)) {
set-itemproperty -path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders' -name StateFlags0012 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\BranchCache' -name StateFlags0012 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Downloaded Program Files' -name StateFlags0012 -type DWORD -Value 2
...
see full example
I ran into the same issue. Researching the possible ways, I have found the following:
http://stealthpuppy.com/cleaning-up-and-reducing-the-size-of-your-master-image/
It shows how to create the sageset registry settings via cmd. You can then use the sagerun:# cmd. I have not tried it via script yet, but have validated that it works...
This script will get all the Volume Caches from the Registry, enable them to be cleaned and run the CLEANMGR.EXE for all caches.
$VolumeCachesRegDir = "hklm:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches"
$CacheDirItemNames = Get-ItemProperty "$VolumeCachesRegDir\*" | select -ExpandProperty PSChildName
$CacheDirItemNames |
%{
$exists = Get-ItemProperty -Path "$VolumeCachesRegDir\$_" -Name "StateFlags6553" -ErrorAction SilentlyContinue
If (($exists -ne $null) -and ($exists.Length -ne 0))
{
Set-ItemProperty -Path "$VolumeCachesRegDir\$_" -Name StateFlags6553 -Value 2
}
else
{
New-ItemProperty -Path "$VolumeCachesRegDir\$_" -Name StateFlags6553 -Value 0 -PropertyType DWord
}
}
Start-Sleep -Seconds 3
Write-Host 'Running CleanMgr.exe...'
Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:65535' -WindowStyle Hidden -PassThru
cls
Running CleanMgr.exe in a powershell script or by itself seems to work fine as long as you run it locally with an account that has local admin rights. But try running it remotely via any remote management tool or remote scripting command (Invoke-Command) and it does not run. You might see the process running on the remote system but it doesn't seem to cleanup anything and the process never ends. I would be interested if anyone has been able to get cleanmgr.exe to run remotely without any user interaction. E.G. ConfigMgr Right Click Tools, ConfigMgr App or PKG, Task Scheduler.