Need PowerShell script to check the variable, if the variable is undefined or Null or Empty

Swathi Reddy 186 Reputation points
2020-12-17T10:42:26.86+00:00

But I am getting the error.

Need PowerShell script to check the variable, if the variable is undefined or Null

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,363 questions
{count} votes

Accepted answer
  1. MotoX80 31,571 Reputation points
    2020-12-17T15:39:31.637+00:00

    You might want to ask your teacher for some 1 on 1 tutoring. Or review these sites.

    https://learn.microsoft.com/en-us/powershell/scripting/learn/more-powershell-learning?view=powershell-5.1
    https://www.guru99.com/powershell-tutorial.html

    But I am getting the error.

    What error are you getting? You misspelled "write-host", you have "wite".

    Need PowerShell script to check the variable

    Which variable? Demostring? Value? Desc? It is not clear what your script is doing with these variables.

    Let's start with the first few lines of your script. .

    param(
    [string]$DemoString,
    [string]$value
    )

    That code says that your script accepts 2 positional input parameters. Save this as demo.ps1 and test it.

    param(  
    [string]$DemoString,  
    [string]$value  
    )  
      
    "Here are the parameters that this script was invoked with."  
    "Demostring: {0}"  -f $DemoString | Write-Host  
    "value     : {0}"  -f $value | Write-Host  
    

    Test it like this.

    PS C:\Temp> .\demo.ps1 xxx aaa
    Here are the parameters that this script was invoked with.
    Demostring: xxx
    value : aaa

    But in your code you immediately prompt the console user and overwrite the passed values. That doesn't make sense.

    $DemoString = Read-Host "Enter:"
    $value = Read-host "Enter value :"

    From a learning point of view, it would make more sense to teach how to validate input data. like this.

    param(  
    [string]$DemoString,  
    [string]$value  
    )  
      
    $ValidData = $true        # the tests will set this to false if we find bad data.  
      
    if($DemoString -eq $null)  
    {  
        Write-Host "Demostring was null. "  
        $ValidData = $false  
    }  
    if($DemoString -eq '')  
    {  
        Write-Host "Demostring was blank. "  
        $ValidData = $false  
    }  
      
    if($value -eq $null)  
    {  
        Write-Host "Value was null. "  
        $ValidData = $false  
    }  
      
    if($value -eq '')  
    {  
        Write-Host "Value was blank. "  
        $ValidData = $false  
    }  
      
    if ($ValidData)   
    {  
        Write-Host "Input data is valid."  
    }   
    else   
    {  
        Write-Host "Please rerun this script and pass 2 valid parameters."                   
        return                    
    }   
    Write-Host "Now process the data."  
    "Demostring: {0}"  -f $DemoString | Write-Host  
    "value     : {0}"  -f $value | Write-Host  
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful