Powershell可以同时唤醒多个媒体驱动器 [英] Powershell to wake up multiple media drives simultaneously

查看:106
本文介绍了Powershell可以同时唤醒多个媒体驱动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台服务器,其中有许多43TB的媒体驱动器.由于大部分时间甚至都没有使用单个驱动器,因此将Areca 1882ix-16设置为在闲置30分钟后将驱动器旋转下来.这样可以很好地防止不必要的电源和热量.在这种情况下,驱动器仍会显示在Windows资源管理器中,但是当您单击访问它们时,大约需要10秒钟才能显示文件夹列表,因为它必须等待驱动器旋转.

I have a server with lots of media drives ~43TB. An areca 1882ix-16 is set to spin the drives down after 30 minutes of inactivity since most days an individual drive is not even used. This works nicely to prevent unnecessary power and heat. In this case the drives still show up in windows explorer but when you click to access them it takes about 10 seconds for the folder list to show up since it has to wait for the drive to spin up.

对于管理工作,我需要启动所有驱动器,以便能够在其中搜索.单击Windows资源管理器中的每个驱动器,然后等待其旋转,然后再单击下一个驱动器,这非常繁琐.显然,多个资源管理器窗口使它运行起来更快,但仍然很乏味.我以为Powershell脚本可以减轻痛苦.

For administrative work I have a need to spin up all the drives to be able to search among them. Clicking on each drive in windows explorer and then waiting for it to spin up before clicking the next drive is very tedious. Obviously multiple explorer windows makes it faster but it is still tedious. I thought a powershell script may ease the pain.

所以我从以下内容开始:

So I started with the following:

$mediaDrives = @('E:', 'F:', 'G:', 'H:', 'I:', 'J:', 'K:', 'L:',
    'M:','N:', 'O:', 'P:', 'Q:', 'R:', 'S:')
get-childitem $mediaDrives  | foreach-object -process { $_.Name }

这只是要求阵列中的每个驱动器都列出其根文件夹名称.可以唤醒驱动器,但是它还是一个线性函数.在打印之前,脚本会为每个驱动器暂停.寻找有关如何同时唤醒每个驱动器的解决方案.有没有一种多线程或其他方法?

This is just requesting that each drive in the array have its root folder name listed. That works to wake the drive but it is again a linear function. The script pauses for each drive before printing. Looking for a solution as to how to wake each drive simultaneously. Is there a way to multi-thread or something else?

推荐答案

下面是一个脚本,可以执行您想要的操作,但是必须使用MTA线程模式在powershell下运行(这是powershell.exe 2.0的默认设置,但是powershell.exe 3.0必须使用-MTA开关启动.)

Here's a script that will do what you want, but it must be run under powershell using the MTA threading mode (which is the default for powershell.exe 2.0, but powershell.exe 3.0 must be launched with the -MTA switch.)

#require -version 2.0

# if running in ISE or in STA console, abort
if (($host.runspace.apartmentstate -eq "STA") -or $psise) {
    write-warning "This script must be run under powershell -MTA"
    exit
}

$mediaDrives = @('E:', 'F:', 'G:', 'H:', 'I:', 'J:', 'K:', 'L:',
    'M:','N:', 'O:', 'P:', 'Q:', 'R:', 'S:')

# create a pool of 8 runspaces   
$pool = [runspacefactory]::CreateRunspacePool(1, 8)
$pool.Open()

$jobs = @()
$ps = @()
$wait = @()     

$count = $mediaDrives.Length

for ($i = 0; $i -lt $count; $i++) {

    # create a "powershell pipeline runner"
    $ps += [powershell]::create()  

    # assign our pool of 8 runspaces to use
    $ps[$i].runspacepool = $pool   

    # add wake drive command
    [void]$ps[$i].AddScript(
        "dir $($mediaDrives[$i]) > `$null")

    # start script asynchronously    
    $jobs += $ps[$i].BeginInvoke();         

    # store wait handles for WaitForAll call
    $wait += $jobs[$i].AsyncWaitHandle
}

# wait 5 minutes for all jobs to finish (configurable)
$success = [System.Threading.WaitHandle]::WaitAll($wait,
    (new-timespan -Minutes 5))     
write-host "All completed? $success"

# end async call   
for ($i = 0; $i -lt $count; $i++) {     
    write-host "Completing async pipeline job $i"    

    try {       
        # complete async job           
        $ps[$i].EndInvoke($jobs[$i])     
    } catch {   
        # oops-ee!          
        write-warning "error: $_"      
    }   

    # dump info about completed pipelines       
    $info = $ps[$i].InvocationStateInfo

    write-host "State: $($info.state) ; Reason: $($info.reason)"  
}

例如,另存为warmup.ps1并运行如下:powershell -mta c:\scripts\warmup.ps1

So, for example, save as warmup.ps1 and run like: powershell -mta c:\scripts\warmup.ps1

要了解有关运行空间池和上述常规技术的更多信息,请查看我关于运行空间池的博客条目:

To read more about runspace pools and the general technique above, take a look at my blog entry about runspacepools:

http://nivot.org/blog/post/2009/01/22/CTP3TheRunspaceFactoryAndPowerShellAccelerators

对于并行度因素,我几乎任意选择了8个-尝试使用较低或较高的数字.

I chose 8 pretty much arbitrarily for the parallelism factor - experiment yourself with lower or higher numbers.

这篇关于Powershell可以同时唤醒多个媒体驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆