PS script for checking disk space

PashPash Member Posts: 1,600 ■■■■■□□□□□
Any idea why the below snippet of script isn't writing any variables to my text file?
{

            echo "$computer        $ID            $size        $free        $b" >> C:\Scripts\lowdisk.txt

            $i++

        }

I am using powergui to debug the script and all of the variables are registered and have values. I am lost icon_sad.gif

Anyone got a helping hand for Pash? :D

Thanks,
DevOps Engineer and Security Champion. https://blog.pash.by - I am trying to find my writing style, so please bear with me.

Comments

  • dynamikdynamik Banned Posts: 12,312 ■■■■■■■■■□
    If you don't send the output to a file, does it display as you intended?
  • HeroPsychoHeroPsycho Inactive Imported Users Posts: 1,940
    1. Don't use aliases in your scripts. Use the actual full cmdlet name. Makes it more readable and easier for others to help you. Ex. Echo = write-output
    2. I can't understand what the need would be for write-output anyway.

    How about posting the entire script, dude. I have a feeling you've made this script a lot more complex than necessary.

    Edit: Do you understand that echo in posh is absolutely NOT what it was in a batch script or other languages?

    Also, I just noticed ">>". That looks like it's not gonna work in PoSh at all.

    Again, paste the script in full here, dude.
    Good luck to all!
  • Hyper-MeHyper-Me Banned Posts: 2,059
    append carrots have worked for me in posh before but never tried in in the brackets.
  • HeroPsychoHeroPsycho Inactive Imported Users Posts: 1,940
    I was referring to how he was trying to extract this data into the textfile.

    I personally would structure the script to simply get WMI objects for the disks running out of space, and export that to CSV. That way it's sortable, etc. Not to mention I bet it's a heck of a lot easier than what he's doing.
    Good luck to all!
  • PashPash Member Posts: 1,600 ■■■■■□□□□□
    ##########################################
    
    ###        Gather Disk Info            ###
    
    ##########################################
    
    
    Clear-Content "C:\Scripts\lowdisk.txt"
    
    $computers = Get-Content "C:\scripts\computers.txt"
    
    echo "ServerName        Drive Letter    Drive Size    Free Space    Percent Free" >> "C:\Scripts\lowdisk.txt"
    
    echo "———-        ————    ———-    ———-    ————" >> "C:\Scripts\lowdisk.txt"
    
    foreach ($computer in $computers)
    
    {
    
        $drives = Get-WmiObject -ComputerName $computer Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}
    
        foreach($drive in $drives)
    
    {
    
            $size1 = $drive.size / 1GB
    
            $size = "{0:N2}" -f $size1
    
            $free1 = $drive.freespace / 1GB
    
            $free = "{0:N2}" -f $free1
    
            $ID = $drive.DeviceID
    
            $a = $free1 / $size1 * 100
    
            $b = "{0:N2}" -f $a
    ##############################################
    
    ##    Determine if any disks low    ##
    
    ##############################################
    
            if (($ID -eq "D:") -or ($ID -eq "C:") -and ($b -lt 10.0))
    
            {
    
                echo "$computer        $ID            $size        $free        $b" >> C:\Scripts\lowdisk.txt
    
                $i++
    
            }
    
        }
    
    }
    
    I can see that all the variables have values as I debug them. And the diskspace on the pc I am running it on has less than 10%. I don't have to write it to text file, csv would be fine. Interestingly enough aslong as the lowdisk file excists the first part of writing text to the file works ok. This isnt usually a method I use for piping info to a text file either.....normally I pipe it into Out-File. I am just curious above all as why I dont see this '>>' method more. There are very good Posh'ers on here so I thought id ask you genius's to shed some light :D

    I seem to remember sourcing a bloated version of this script from a blog post somwhere that I thought I had bookmarked and snapshotted but I cannot find.

    Thanks,

    Pash
    DevOps Engineer and Security Champion. https://blog.pash.by - I am trying to find my writing style, so please bear with me.
  • HeroPsychoHeroPsycho Inactive Imported Users Posts: 1,940
    You can edit this a little to make it work for you. Since you are altering significantly the default properties of the WMI object you're collecting, it might be easier to just store the results in Excel instead of doing a bunch of add-member lines.

    $erroractionpreference = "SilentlyContinue"
    $a = New-Object -comobject Excel.Application
    $a.visible = $True

    $b = $a.Workbooks.Add()
    $c = $b.Worksheets.Item(1)

    $c.Cells.Item(1,1) = "Machine Name"
    $c.Cells.Item(1,2) = "Drive"
    $c.Cells.Item(1,3) = "Total size (GB)"
    $c.Cells.Item(1,4) = "Free Space (GB)"
    $c.Cells.Item(1,5) = "Free Space (%)"

    $d = $c.UsedRange
    $d.Interior.ColorIndex = 19
    $d.Font.ColorIndex = 11
    $d.Font.Bold = $True
    $d.EntireColumn.AutoFit($True)

    $intRow = 2

    $colComputers = get-content C:\scripts\listfiles\Servers.txt
    foreach ($strComputer in $colComputers)
    {
    $colDisks = get-wmiobject Win32_LogicalDisk -computername $strComputer -Filter "DriveType = 3"
    foreach ($objdisk in $colDisks)
    {
    $c.Cells.Item($intRow, 1) = $strComputer.ToUpper()
    $c.Cells.Item($intRow, 2) = $objDisk.DeviceID
    $c.Cells.Item($intRow, 3) = "{0:N0}" -f ($objDisk.Size/1GB)
    $c.Cells.Item($intRow, 4) = "{0:N0}" -f ($objDisk.FreeSpace/1GB)
    $c.Cells.Item($intRow, 5) = "{0:P0}" -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size)
    $intRow = $intRow + 1
    }
    }

    Some changes you might include would be to filter with the WMI query to only get the disks that are low on space. Relatively easy to do.
    Good luck to all!
  • PashPash Member Posts: 1,600 ■■■■■□□□□□
    Thanks Hero. Seems like I cant get this to work either though!

    I modified the script a bit to include a test-path option and save. BUT it just opens excel 2007 and it sits there doing nothing. Script debugs fine, variables appear to have values again but nothing is shown.

    Any ideas?
    $strPath = "c:\scripts\output.xls"
    $objExcel=New-Object -ComObject Excel.Application
    $objExcel.Visible=-1
    $WorkBook = $objExcel.Workbooks.Add()
    $sheet = $workbook.worksheets.item(1)
    
    $sheet.Cells.item(1,1) = "Machine Name"
    $sheet.Cells.item(1,2) = "Drive"
    $sheet.Cells.item(1,3) = "Total size (GB)"
    $sheet.Cells.item(1,4) = "Free Space (GB)"
    $sheet.Cells.item(1,5) = "Free Space (%)"
    
    $d = $sheet.UsedRange
    $d.Interior.ColorIndex = 19
    $d.Font.ColorIndex = 11
    $d.Font.Bold = $True
    $d.EntireColumn.AutoFit($True)
    
    $intRow = 2
    
    $colComputers = get-content C:\scripts\computers.txt
    foreach ($strComputer in $colComputers)
    {
    $colDisks = get-wmiobject Win32_LogicalDisk -computername $strComputer -Filter "DriveType = 3"
    foreach ($objdisk in $colDisks)
    {
    $c.Cells.Item($intRow, 1) = $strComputer.ToUpper()
    $c.Cells.Item($intRow, 2) = $objDisk.DeviceID
    $c.Cells.Item($intRow, 3) = "{0:N0}" -f ($objDisk.Size/1GB)
    $c.Cells.Item($intRow, 4) = "{0:N0}" -f ($objDisk.FreeSpace/1GB)
    $c.Cells.Item($intRow, 5) = "{0:P0}" -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size)
    $intRow = $intRow + 1
    }
    }
    IF(Test-Path $strPath)
    {
    Remove-Item $strPath
    $objExcel.ActiveWorkbook.SaveAs($strPath)
    }
    ELSE
    {
    $objExcel.ActiveWorkbook.SaveAs($strPath)
    }
    
    
    

    Thanks,

    Pash
    DevOps Engineer and Security Champion. https://blog.pash.by - I am trying to find my writing style, so please bear with me.
  • HeroPsychoHeroPsycho Inactive Imported Users Posts: 1,940
    Launching with right creds?
    Good luck to all!
  • PashPash Member Posts: 1,600 ■■■■■□□□□□
    HeroPsycho wrote: »
    Launching with right creds?

    good point dude! I will try this on Monday morning when back in the office. Cheers.
    DevOps Engineer and Security Champion. https://blog.pash.by - I am trying to find my writing style, so please bear with me.
  • HeroPsychoHeroPsycho Inactive Imported Users Posts: 1,940
    Pash wrote: »
    good point dude! I will try this on Monday morning when back in the office. Cheers.

    Remember, get-wmiobject supports the -credential parameter.

    You don't have to use a Run As. Set a variable to get-credential ""
    Good luck to all!
  • PashPash Member Posts: 1,600 ■■■■■□□□□□
    I get the following in the debug window now:-

    Exception calling "Add" with "0" argument(s): "Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))"
    At :line:4 char:35
    + $WorkBook = $objExcel.Workbooks.Add <<<< ()

    I have used get-member to find our the current properties of that newobject, add isnt in there, so seemingly thats why its not liking it.

    Its annoying because even the dev reference says that my method is ok:-

    How to: Create a Workbook [Excel 2007 Developer Reference]

    Any other ideas guys? :)
    DevOps Engineer and Security Champion. https://blog.pash.by - I am trying to find my writing style, so please bear with me.
  • PashPash Member Posts: 1,600 ■■■■■□□□□□
    haha ok, ive found why i cant get the workbook to open:-

    COM. Can't open an Excel workbook - Vista Forums

    Work around is in that link, one other method is to change all my locale settings to English US. Now I have a new debugging problem :D

    Cannot find an overload for "AutoFit" and the argument count: "1".
    At :line:18 char:23
    + $d.EntireColumn.AutoFit <<<< ($True)

    I guess ill have to have a search a bit later for the reason for this one.
    DevOps Engineer and Security Champion. https://blog.pash.by - I am trying to find my writing style, so please bear with me.
  • PashPash Member Posts: 1,600 ■■■■■□□□□□
    Ok lol, i got tired of working out that Excel issue for now and I don't mean to bring up a dead horse. But for all intent and purpose the below should work, 100% sure of that:-
    ##########################################
    
    ###        Gather Disk Info            ###
    
    ##########################################
    
    
    Clear-Content "C:\Scripts\lowdisk.txt"
    
    $computers = Get-Content "C:\scripts\computers.txt"
    
    Write-Output "ServerName        Drive Letter    Drive Size    Free Space    Percent Free" | Out-File C:\Scripts\lowdisk.txt
    
    Write-Output "———-        ————    ———-    ———-    ————" | Out-File C:\Scripts\lowdisk.txt
    
    foreach ($computer in $computers)
    
    {
    
        $drives = Get-WmiObject -ComputerName $computer Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}
    
        foreach($drive in $drives)
    
    {
    
            $size1 = $drive.size / 1GB
    
            $size = "{0:N2}" -f $size1
    
            $free1 = $drive.freespace / 1GB
    
            $free = "{0:N2}" -f $free1
    
            $ID = $drive.DeviceID
    
            $a = $free1 / $size1 * 100
    
            $b = "{0:N2}" -f $a
    
    Write-Host $size1 $size $free1 $free $ID $a $b ## this shows variables
    
    ##############################################
    
    ##    Determine if any disks low    ##
    
    ##############################################
    
            if (($ID -eq "D:") -or ($ID -eq "C:") -and ($b -lt 10.0))
    
            {
                Write-Output $computer        $ID            $size        $free        $b | Out-File C:\Scripts\lowdisk.txt
    
                $i++
    
            }
    
        }
    
    }
    

    I have made it more readable by replacing echo with "write-output" and then piped it to "Out-file".

    The first part of the script works fine and the headers are placed in the file. The script debugs and the variable values are visible in powergui script editor and I included a write-host to confirm that from the debugging window

    Any clues? :D
    DevOps Engineer and Security Champion. https://blog.pash.by - I am trying to find my writing style, so please bear with me.
  • PashPash Member Posts: 1,600 ■■■■■□□□□□
    Ok, its working now to some extent!

    ServerName Drive Letter Drive Size Free Space Percent Free
    ———- ———— ———- ———- ————
    127.0.0.1 C: 19.53 0.45 2.29
    127.0.0.1 D: 54.99 2.46 4.48

    It is something to do with my if statement. Need to check it.

    Ohh and I am a wally, -append is needed on out file unless you wanna overwrite previous entered text.
    DevOps Engineer and Security Champion. https://blog.pash.by - I am trying to find my writing style, so please bear with me.
Sign In or Register to comment.