Most Popular Posts

Showing posts with label Powershell. Show all posts
Showing posts with label Powershell. Show all posts

Saturday, 12 October 2013

Powershell - Enable incremental updates on collections

I am busy at the moment migrating a customer from SCCM 2007 to 2012, as part of the migration I disabled incremental updates on all collections so as not to hurt performance. The customer has a folder of collections that they use to deploy software via active directory groups, all collections are prefixed "AD Deploy". They wanted to enable scheduled & incremental updates on these collections only so I set to do this with powershell, disappointed that there was no cmdlet for this I turned to WMI.

Using the SMS_Collection class from the SMS namespace I was able to filter on collections begining with "AD Deploy" and set the property (RefreshType in this case) to a value of 6, which would enable incremental and scheduled updates. Below is the script I came up with.


#======Start Script==============
$Collections = (Get-WmiObject -Class SMS_Collection -Namespace "root\SMS\site_007" -ComputerName . -Filter "Name Like 'AD Deploy%'")

$Count = 0
foreach ($Col in $Collections){
$Col.RefreshType = 6
$Col.put() | Out-Null
Write-Host $Col
write-host $Col.Name
$Count ++
}
write-host $Count "collections in total"

#=========End Script===========

Here is a link to the script

You can change the filter for collections in your environment if you wish to use it, I would also recommend commenting out the "$Col.put() | Out-Null" line for the first run and do a manual check/count of the collections that this might actually affect ;)

Hope it helps someone out there :)

Wayne

Monday, 9 July 2012

Regular Expressions Helper

Yesterday I had the need to create a regular expression in Powershell and stumbled across an amazing website that allows you to:

1) Paste in the text you are working with
2) Adjust your Regex on the fly and have it highlight text that matches that expression

How cool is this!?

I take no credit for the site whatsoever, I am just posting here for future reference

http://gskinner.com/RegExr/

Cheers

Tuesday, 1 March 2011

Powershell script to monitor for idle CPU then put computer to sleep

Good evening, I have been working on a little powershell script and thought I would post it as it might come in handy for others.
Basically my laptop stays on 24/7 (its tucked away in a corner and runs DNS, DHCP,Downloads and that kind of thing). What I want to do is if nothing is downloading on it overnight then I want it to sleep. I use JDownloader to auto download files for me overnight which has the auto extract (unrar.exe) feature built in too. Take a look at the script you will see it looks for unrar.exe and if that isnt running checks the cpu usage of jdownloader (javaw.exe) if it is below a certain value 5 times then the powershell script calls a batch file and the laptop sleeps.
When it hits 7am a scheduled task (to ping localhost) wakes it up again, nice huh? :)

Anyway enough babbling heres the script:

####### SCRIPT START #######
[int]$ProcessorInactive = $null

do {if(($ProcessorInactive) -le 5)
{
Write-host "ProcessorInactive is less than 5"
do {if(($ProcessActive = Get-Process unrar -ErrorAction SilentlyContinue) -ne $null)
{
Write-host "unrar is active"
$ProcessAlive = "SomeValue"
Start-Sleep -Seconds 900
}
else
{
Write-host "unrar isnt active"
$Process = Get-Counter -Counter "\Process(javaw)\% Processor Time"
$Sample = $process.Countersamples[0]
$CpuTime = $Sample.CookedValue
$CpuTime = ($CpuTime/2)

do {if(($CpuTime) -gt 3)
{
Write-host "CpuTime is higher than 3"
Start-Sleep -Seconds 10
$Process = Get-Counter -Counter "\Process(javaw)\% Processor Time"
$Sample = $process.Countersamples[0]
$CpuTime = $Sample.CookedValue
$CpuTime = ($CpuTime/2)
}
else
{
Write-host "CpuTime is less than 3 now"

}
}
while ($CpuTime -gt 3)
#clear this variable else we will loop forever
$ProcessAlive = $null
$ProcessorInactive++
Start-Sleep -Seconds 30
}}
while (($ProcessAlive) -ne $null)
}
}
while ($ProcessorInactive -le 5)
echo "********VERY END OF SCRIPT NOW DO WHAT YOU WANT HERE*******"
cmd /c """H:\powershell\Sleep.bat"""
####### SCRIPT END #######

And if anyone wants to know the contents of "sleep.bat" are:
rundll32.exe powrprof.dll,SetSuspendState

Note: Aside from the obvious you may have to change this part:
$CpuTime = ($CpuTime/2)
to the number of cores you have (my laptop has 2) as the cpu usage is a total of all cores
Thanks for reading and hope it helps someone! :)

EDIT: I have since changed the cputime percentage for javaw.exe to 1 to see if this improves the script. I think that with it set to 3 the script is sometimes catching the usage below 3% 5 times and sleeping even though there are downloads left so altering this value should help. The 2 parts you need to change are line 21 & line 36