1 Replies - 849 Views - Last Post: 12 March 2012 - 02:10 PM Rate Topic: -----

#1 codecsmash49  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 09-December 08

Powershell

Posted 11 March 2012 - 02:07 PM

Im working on a powershell script to implements a directory size analyzer. I have having a hard time with the user input if its null to run the size annalists on the current directory

Clear-Host

if ($args = " ")
    {
    $location = Get-Location
    get-childitem $location | Format-Table Length, Name -auto
    Write-Host "The subdirectory with the maximum disk space use:" 
    Dir $location -ea SilentlyContinue|
        Sort-Object Length -Descending |
        Select-Object -first 1|
        Select-Object Length, Name
        exit
        }

if (Test-Path $args) {
    get-childitem $args | Format-Table Length, Name -auto
    Write-Host "The subdirectory with the maximum disk space use:" 
    Dir $args -ea SilentlyContinue|
        Sort-Object Length -Descending |
        Select-Object -first 1|
        Select-Object Length, Name
    exit
    }
    elseif (!(Test-Path $args)) {
    Write-Host "No subdirectory is found inside" $args
    exit 
    }



I can get it to read and print out correctly if the search directory is there or if it is not there. But if the user has no input it complains about string being empty

Is This A Good Question/Topic? 0
  • +

Replies To: Powershell

#2 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 356
  • View blog
  • Posts: 785
  • Joined: 27-June 09

Re: Powershell

Posted 12 March 2012 - 02:10 PM

I think the error is in your condition.

if ($args = " ")


Should instead be

if ($args.count -eq 0)


You will also need to add else logic so that the code never even tries the "Test-Path $args"

This post has been edited by mojo666: 12 March 2012 - 02:13 PM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1