Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Move pagefile from one partition to another

Posted on 2008-06-20
10
Medium Priority
?
2,493 Views
Last Modified: 2012-08-13
I've been working on getting a script created that will set the pagefile settings on all our workstations.  Workstations are Win 2K SP4 and Win XP SP2.  Current company policy:  no pagefile on C: and a pagefile set on D: (1.5 * physical memory); min and max are the same.  We have successfully set the C: (boot) pagefile to zero manually, so I don't see a reason why we can't do this via a script.  I have a script that will find the physical memory and set the page file size accordingly, but I need it to also do the following:

If the pagefile is found on the C: partition, remove from C: and create on D: partition
Max out at 4GB for the pagefile size

I work weekdays, so if I don't get the answer today, I'll be back Monday.

This is what I have so far:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
Set colCSItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
 
Set colPageFiles = objWMIService.ExecQuery _
    ("Select * from Win32_PageFileSetting")
 
For Each objPageFile in colPageFiles
    For Each objCSItem In colCSItems
       objPageFile.InitialSize = objCSItem.TotalPhysicalMemory * 1.5 / 1048576
       objPageFile.MaximumSize = objCSItem.TotalPhysicalMemory * 1.5 / 1048576
       objPageFile.Put_
    Next
Next

Open in new window

0
Comment
Question by:atomite
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 24

Accepted Solution

by:
purplepomegranite earned 1000 total points
ID: 21835736
The attached code will do what you ask.

Note that the minimum possible pagefile size on the C (system) drive is 2Mb - it is impossible to set it below this, at least by script.  I think it is impossible to set it below this anyway.
option explicit
dim strComputer
dim blnPageFileOnD
dim lngTotalPhysicalMemory, lngPageFileSize
dim objWMI
dim colCS, objCS
dim colPF, objPF
 
strComputer = "."
blnPageFileOnD=false
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
' First of all work out the desired size for the pagefile
Set colCS = objWMI.ExecQuery("SELECT * FROM Win32_ComputerSystem")
For Each objCS In colCS
	lngTotalPhysicalMemory=objCS.TotalPhysicalMemory
next
lngPageFileSize=lngTotalPhysicalMemory * 1.5 / 1048576
if lngPageFileSize>4096 then lngPageFileSize=4096 ' Limit to 4Gb
wscript.echo "Pagefile size will be: " & lngPageFileSize
	
' Now process the pagefiles
set colPF=GetObject("winmgmts:{(CreatePageFile)}").instancesof("Win32_PageFileSetting")
For Each objPF in colPF
	if lcase(left(objPF.Name, 1))="c" then
		' This pagefile is on the c: drive, so set it's size to 2, which is the minimum supported size for the c drive
		objPF.InitialSize = 2
		objPF.MaximumSize = 2
		objPF.Put_
		wscript.echo "Pagefile on C set to minimum (2Mb)"
	elseif lcase(left(objPF.Name, 1))="d" then
		objPF.InitialSize = lngPageFileSize
		objPF.MaximumSize = lngPageFileSize
		objPF.Put_
		wscript.echo "Pagefile on D set to " & lngPageFileSize
		blnPageFileOnD=true
	end if
Next
 
if not blnPageFileOnD then
	' Need to create pagefile on d drive
	wscript.echo "Creating pagefile on D drive"
	Set objWMI = GetObject("winmgmts:{(CreatePageFile)}!\\.\root\cimv2")  
	Set colPF = objWMI.Get("Win32_PageFileSetting")  
	Set objPF = colPF.SpawnInstance_(0)    
	objPF.Name = "D:\\pagefile.sys"  
	objPF.InitialSize = lngPageFileSize  
	objPF.MaximumSize = lngPageFileSize
	objPF.Put_  
end if

Open in new window

0
 

Author Comment

by:atomite
ID: 21836076
Thanks for the reply.  I'm checking this at home, so I can't test this till I'm back in the office on Monday.

We disable the pagefile on the C: through the GUI (system properties) by selecting the C:, then selecting the No Paging File radio button.  This removes the pagefile from the C: drive as far as I know.  It does warn you about the fact that it is needed, but lets you continue without it.  Is it possible to instead of setting the size to zero (or 2), to disable the pagefile on the c:?  Is there a command to disable the pagefile on a particular drive?
0
 
LVL 24

Expert Comment

by:purplepomegranite
ID: 21836940
It is not possible to set the pagefile to 0 via WMI.  The properties are simply not available - see here http://msdn.microsoft.com/en-us/library/aa394245(VS.85).aspx.  I am unaware of any way around this except possibly delving into the registry.  If you change the code to anything less than 2, the settings will simply be ignored as invalid.

As a point to note, though... not having at least a small pagefile on your c: drive means that in the event of a crash, Windows cannot write a crash dump file (see here http://support.microsoft.com/kb/197379).
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:atomite
ID: 21847578
It went great until "Creating pagefile on D drive", then:

Line:49
Char:9
Error: Value out of Range
Code: 8004102B
Source: SWbemObjectEx

This was on a text Win XP box that had a pagefile on the C: but not on the D:
0
 

Author Comment

by:atomite
ID: 21847620
Sorry, my mistake.  I didn't have my test* box set up right.  I'll re-test in a minute.
0
 
LVL 24

Expert Comment

by:purplepomegranite
ID: 21847636
Hmm... what was the value of lngPageFileSize as calculated by the script?
0
 
LVL 24

Expert Comment

by:purplepomegranite
ID: 21847643
Ah, ok!  I tested on a WinXP virtual machine with a d drive, it worked on that ok.
0
 

Author Comment

by:atomite
ID: 21848122
Alright, the script worked exactly as you stated.  In terms of disabling the C: drive pagefile, since you don't know of a workaround, should I maybe post another question regarding that?
0
 
LVL 24

Expert Comment

by:purplepomegranite
ID: 21848230
That would be best, I think, as then other people are likely to look at it and hopefully someone will have a solution.  It must be said, I do seem to remember seeing another thread on this site regarding the same issue - but I have done a couple of searches, and I can't find it at all.

My thoughts on a workaround are that it may be possible to be set to zero by modifying the registry.  I can have a look at that later, won't have time for a while though.
0
 

Author Closing Comment

by:atomite
ID: 31469353
Thanks for your help.  :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question