简体   繁体   English

从任务计划程序执行时,Powershell WMI查询失败

[英]Powershell WMI Query failing when executed from Task Scheduler

i have a strange problem... 我有一个奇怪的问题...

i have the following code, which takes the output from Sysinternals Disk Usage tool (link below) 我有以下代码,它采用Sysinternals磁盘使用工具的输出(下面的链接)
Disk Usage - Sys Internals 磁盘使用情况-Sys Internals

so first i get the physical drives into array $Disks, then i enumerate these through the foreach and mess about with them. 因此,首先我将物理驱动器放入阵列$ Disks中,然后通过foreach枚举它们并将它们弄乱。

my problem lies in this line $Dir = du.exe -q -v $d.DeviceID 我的问题在于此行$ Dir = du.exe -q -v $ d.DeviceID

$PC = get-content env:COMPUTERNAME
$Disk = gwmi win32_logicaldisk -filter "drivetype=3"

    foreach ($d in $Disk)
    {
    $Dir =  du.exe -q -v $d.DeviceID
    $Dir[8..($Dir.length-8)] | foreach {
    $Size = $_.substring(0,10).replace(",","")/1024
    $Path = $_.substring(10)
    }
}

$d.DeviceID should be the drive letter (ie C:) $ d.DeviceID应该是驱动器号(即C :)

then i populate $Dir with the output from DU.exe, but $d.DeviceID is not acting how it is supposed to, running this from a task has this following result (added a line that says $d.DeviceID, to show the output): 然后我用DU.exe的输出填充$ Dir,但是$ d.DeviceID并未按照预期的方式运行,从任务运行此命令具有以下结果(添加了一行显示$ d.DeviceID的行,以显示输出):

B: B:
Cannot index into a null array. 无法索引为空数组。
At C:\\DU.ps1:25 char:6 在C:\\ DU.ps1:25 char:6
+ $Dir[ <<<< 8..($Dir.length-8)] | + $ Dir [<<<< 8 ..($ Dir.length-8)] | foreach { foreach {
+ CategoryInfo : InvalidOperation: (System.Object[]:Object[]) [], + CategoryInfo:InvalidOperation:(System.Object []:Object [])[],
RuntimeException RuntimeException
+ FullyQualifiedErrorId : NullArray + FullyQualifiedErrorId:NullArray

C: C:
Cannot index into a null array. 无法索引为空数组。
At C:\\DU.ps1:25 char:6 在C:\\ DU.ps1:25 char:6
+ $Dir[ <<<< 8..($Dir.length-8)] | + $ Dir [<<<< 8 ..($ Dir.length-8)] | foreach { foreach {
+ CategoryInfo : InvalidOperation: (System.Object[]:Object[]) [], + CategoryInfo:InvalidOperation:(System.Object []:Object [])[],
RuntimeException RuntimeException
+ FullyQualifiedErrorId : NullArray + FullyQualifiedErrorId:NullArray

D: D:
Cannot index into a null array. 无法索引为空数组。
At C:\\DU.ps1:25 char:6 在C:\\ DU.ps1:25 char:6
+ $Dir[ <<<< 8..($Dir.length-8)] | + $ Dir [<<<< 8 ..($ Dir.length-8)] | foreach { foreach {
+ CategoryInfo : InvalidOperation: (System.Object[]:Object[]) [], + CategoryInfo:InvalidOperation:(System.Object []:Object [])[],
RuntimeException RuntimeException
+ FullyQualifiedErrorId : NullArray + FullyQualifiedErrorId:NullArray

running it from the ISE or just from the Shell has no issues, running it on other servers from all methods works. 从ISE或仅从Shell运行它都没有问题,通过所有方法在其他服务器上运行都可以。

i do believe the population of the $Dir vairable is the problem, as the du.exe has trouble with the $d.DeviceID 我确实相信$ Dir vairable的问题是问题所在,因为du.exe遇到了$ d.DeviceID的问题

i dont understand why it is just this server/task sheduler that has the issue. 我不明白为什么只有这个服务器/任务管理器才有问题。 i have tried the following: 我尝试了以下方法:

redefined the array element to $i = $d.deviceID to fix it down - nothing 将数组元素重新定义为$ i = $ d.deviceID以将其修复-没有
exported the job from other server (both DCs) an imported - nothing 从导入的其他服务器(两个DC)导出了作业-没有
restarted the winmgmt service - nothing 重新启动winmgmt服务-什么都没有

i think its a permissions issue, but im running this on an AD as THE Dom Admin with top privilages. 我认为这是一个权限问题,但是我在AD上以具有最高特权的THE DOM管理员身份运行了它。

please can you guys help on this one, really am stuck... 你们可以帮忙吗,真的卡住了...

cheers 干杯

Lee 背风处

Yet another update based on comment below: 根据下面的评论还有另一个更新:

Try: 尝试:

$cmd = "du.exe `-q `-v $($d.DeviceID)"
$dir = Invoke-Expression $cmd

Updating as per the comment below. 根据以下评论进行更新。

Take this example. 举这个例子。 This can get the size of every folder and display size and full path to the folder. 这样可以获取每个文件夹的大小,并显示该文件夹的大小和完整路径。

Function Get-FolderSize {
    Param ($folderPath)
    $colItems = (Get-ChildItem $folderPath -recurse | Measure-Object -property length -sum)
    return $colItems.sum/1MB
}

$folders = Get-ChildItem -Recurse C:\Scripts
$folders | % {
    if ($_.PSIsContainer) {
        $size = Get-FolderSize $_.FullName
        Write-Host $size
        Write-Host $_.FullName
    }
}

You can use WMI to get the drive letter and pass it to the script. 您可以使用WMI获取驱动器号并将其传递给脚本。 For example: 例如:

$disks = gwmi win32_logicaldisk -filter "drivetype=3"
$disks | % {
    $items = Get-ChildItem -Recurse $_.DeviceID -Force
    $items | % {
        if ($_.PSIsContainer) {
            $size = Get-FolderSize $_.FullName
            Write-Host $size
            Write-Host $_.FullName
        }
    }
}

So, with this, you dont need DU.exe. 因此,有了此,您不需要DU.exe。 You can run this as a script. 您可以将其作为脚本运行。

--------------OLD ANSWER _------------------- --------------旧答案_-------------------

First thing I would suspect is the path to DU.exe. 我首先怀疑的是DU.exe的路径。 What is the working directory set on the scheduled task? 计划任务上设置的工作目录是什么? Is it the place where DU.exe is available? 是DU.exe可用的地方吗?

BTW, what is the goal of this script? 顺便说一句,这个脚本的目标是什么? Are you just looking at the disk drive size? 您只是在查看磁盘驱动器的大小吗? What are you capturing into $path? 您在$ path中捕获了什么? I did not have the patience to test your code. 我没有耐心来测试您的代码。 But, I feel that this can be easily achieved using just WMI and no other external tools. 但是,我认为仅使用WMI而不使用其他外部工具即可轻松实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM