user9426157
user9426157

Reputation: 41

Powershell scripting help needed

I am very new to powershell scripting. Trying to learn it from web. Now I am trying to do a script and facing some problem, so need some help ans suggestions from you people. I am giving description what I tried to do:

first of all I have declared 2 variables, then I used if statement to see if the variables are empty then it will show a warning message and it will ask for the inputs from the user and after that it will show the value of the variables. but it is giving some errors.

$workingdirectory = args[0]

$directoryname = args[1]

if ("$WorkingDirectory" -eq "")

    { 

      Write-Warning "Parameter Required"

     $WorkingDirectory = Read-Host "Enter the absolute path to working directory "

     }


if ("$DirectoryName" -eq "")

    {

     Write-Warning "Paramater Required"

      $DirectoryName = Read-Host "Enter a directory name to search for in $WorkingDirectory "

    }

Write-Host "$WorkingDIrectory"


write-host "$DirectoryName"

When I run it, it is showing the following errors:

ARGS[0] : The term 'ARGS[0]' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\LAB_5-submission\mmbillah1_Lab_testdir.ps1:24 char:21 + $WorkingDirectory = ARGS[0] + ~~~~~~~ + CategoryInfo : ObjectNotFound: (ARGS[0]:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

ARGS[1] : The term 'ARGS[1]' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\LAB_5-submission\mmbillah1_Lab_testdir.ps1:25 char:18 + $DirectoryName = ARGS[1] + ~~~~~~~ + CategoryInfo : ObjectNotFound: (ARGS[1]:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

I want to run like this: .\scriptname.ps1, if I use this then it will show the warning and ask for the two variables input. and if I run this .\scriptname.ps1 C:\users\masum then it will ask for the second variable value only.

Upvotes: 3

Views: 926

Answers (1)

TheMadTechnician
TheMadTechnician

Reputation: 36322

The root of your problem is simply syntax it should be $args[0] and $args[1] if you intend to use what you have now. What I would strongly suggest instead is to create parameters for your script, and then test if those parameters are valid, and if they aren't to prompt for them.

Parameters can be defined in a Param() block as such:

Param(
    $workingdirectory,
    $directoryname
)

That is very simple, but for your needs it works. You can add types to make sure the right kinds of things are passed as the parameter, and add tests to make sure the parameters are valid, but that goes beyond what we're doing here.

Then you would check to make sure that there is a value for each, and I would recommend making sure that the path is valid. Something like:

While([string]::IsNullOrEmpty($workingdirectory) -or !(Test-Path $workingdirectory)){
    $workingdirectory = Read-Host "Enter a valid working directory"
}

That checks if the $workingdirectory variable is empty or just blank spaces, and if it actually has a value it will check to make sure it's a valid directory. If it is blank or the path isn't valid it prompts the user to enter a valid path. You would need to repeat that for the $directoryname variable.

So you would end up with something like:

Param(
    $workingdirectory,
    $directoryname
)

While([string]::IsNullOrEmpty($workingdirectory) -or !(Test-Path $workingdirectory)){
    $workingdirectory = Read-Host "Enter a valid working directory"
}

While([string]::IsNullOrEmpty($directoryname) -or !(Test-Path $directoryname)){
    $directoryname= Read-Host "Enter a valid target directory"
}

Write-Host $workingdirectory
Write-Host $directoryname

Upvotes: 4

Related Questions