在powershell脚本中查找多个域上的驱动器大小

时间:2014-04-14 21:26:55

标签: powershell

这是我的第一篇文章,所以希望它有道理。

我尝试浏览多个域并查看每台服务器上每个驱动器的数据使用情况。然后我获取信息,将其添加到数组,然后将数组写入csv。

>DOMAIN-ONE 
>first drive: \\domain-one\e$
>second drive: \\domain-one\f$
>third drive: \\domain-one\k$

问题不在于登录,而是找不到所有存在的驱动器和它找到的驱动器,它没有返回大小。这个相同的脚本可以在我的本地机器上运行,为我提供所有驱动器,而不是域。我认为它们是虚拟驱动器,它不能与

一起使用

当前脚本

$serverlist=@("domain-one","domain-two")


$username = "username"
$password = "pass123"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr


#set up array to 
$csvarray= @()

#go through each server
foreach ($servername in $serverlist)
{
    #check all drives a to z on server
    foreach ($drive in 97..122) {


        #set drive name
        $workingdir = ([char]$drive) + ":"



        #check to see if temp directory file path exists
        if (test-path $workingdir) {

            #get drive size
            $disk = Get-WmiObject Win32_LogicalDisk -computername $servername -Credential $cred -Filter "DeviceID='$workingdir'" |
            Select-Object DeviceID,Volumename,Size,FreeSpace

            $disksize = $disk.Size/1gb
            $diskfree = $disk.FreeSpace/1gb
            $disk.DeviceID
            $disk.Volumename
            $usedspace = ($disk.Size-$disk.FreeSpace)/1gb


            #create row of data
            $rowobject = New-Object psobject 

            $rowobject | Add-Member -MemberType NoteProperty -Name "Server" -Value $servername 
            $rowobject | Add-Member -MemberType NoteProperty -Name "Drive" -Value $disk.deviceid 
            $rowobject | Add-Member -MemberType NoteProperty -Name "Drive Name" -Value $disk.volumename 
            $rowobject | Add-Member -MemberType NoteProperty -Name "Used Space" -Value $usedspace 
            $rowobject | Add-Member -MemberType NoteProperty -Name "Free Space" -Value $diskfree
            $rowobject | Add-Member -MemberType NoteProperty -Name "Drive Size" -Value $disksize

            $csvarray += $rowobject


        }
    }
}
next

$csvarray| export-csv "F:\datausage3.csv" -NoTypeInformation

1 个答案:

答案 0 :(得分:0)

如果我正在读这个权利会要求你为它检查的每一个驱动器提供凭据,你应该使用$creds = Get-Credential "$env:username",然后简单地在Get-WMIObject调用中返回$ creds,如下所示: / p>

$disk = Get-WmiObject Win32_LogicalDisk -computername $servername -Credential $cred -Filter "DeviceID='$workingdir'" | Select-Object DeviceID,Volumename,Size,FreeSpace

到这个

$disk = Get-WmiObject Win32_LogicalDisk -computername $servername -Credential $creds -Filter "DeviceID='$workingdir'" | Select-Object DeviceID,Volumename,Size,FreeSpace

此外,您在foreach之后列出了“下一个”,这不是继续循环所必需的,它将自动执行此操作。 :)