Powershell脚本设置ms访问的属性PriorityClass

时间:2018-10-01 02:10:23

标签: powershell ms-access automation

我有以下PS脚本,可以自动运行25个单独的Access db。每小时运行一次。我有一个长期的数据库“冻结”的常见问题。如果我正在观看实时节目并打开了任务管理器,则可以提高MSAccess的优先级,并且数据库可以快速运行。我想添加一行代码,以提高每次调用它时MS Access的优先级。 (为简便起见,变量$ Owner被截断为3)

# Run Merge Database on all Owner groups
$Owner = "Beach", "Bennett", "Burger"
foreach ($element in $Owner) 
{ # BEGIN "foreach" LOOP
# Create variable with directory of Target file
$OwnerPath = "E:\ServerFolders\Ops\$element\$element-Merge.accdb"

# Remove # below to see Directory variables
Write-Output $OwnerPath

# start Access
$access = New-Object -comobject Access.Application

#make it visible (just to check what is happening)
$access.Visible = $true

#access the Application object and run a macro
$access.OpenCurrentDatabase($OwnerPath)


} # END "foreach"

2 个答案:

答案 0 :(得分:0)

这很丑陋,但是您可以执行以下操作

$PIDS_Before = Get-Process | ?{ $_.Name -like "iexplore*"} | select id, Name
$IE = New-Object -com internetexplorer.application
sleep -Seconds 5
$Pid_After = Get-Process | ?{$_.Name -like "iexplore*"} | select id, Name
Compare-Object $PIDS_Before $Pid_After | select -ExpandProperty InputObject | %{
    $process = Get-Process -Id $_.Id
    $process.PriorityClass = "High"
}

您需要到达的实际区域是Process对象中的 PriorityClass 问题是从COM对象获取PID。如果可以得到,则可以使用get-process -id $ PIDHERE

睡眠的原因(这真的很讨厌)是您需要等待对象加载。有时5秒还不够,您可能需要更长的时间。

优先级类别的可接受值为 空闲,低于正常,正常,高于正常,高,实时

答案 1 :(得分:0)

您可以使用类似的内容:

$job = "msaccess.exe"
Get-WmiObject Win32_Process -filter "CommandLine LIKE '%$job%'" | select ProcessId | %{ $process = Get-Process -Id $_.ProcessId; $process.PriorityClass = "High" }

此代码段对于在批量模式下设置优先级非常有用:

$job = "scheduled.bat"
while (1) {
  Get-WmiObject Win32_Process -filter "CommandLine LIKE '%$job%'" | select ProcessId | %{ $process = Get-Process -Id $_.ProcessId; $process.PriorityClass = "High" }
  "sleeping"
  sleep (30)
}