question

Ashwa avatar image
0 Votes"
Ashwa asked Ashwa edited

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

But I am getting the error.

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

windows-server-powershell
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You'd probably benefit from reading this: Windows-PowerShell-4


0 Votes 0 ·
Ashwa avatar image Ashwa RichMatheisen-8856 ·

Thank you. I will go through the material.

0 Votes 0 ·

1 Answer

MotoX80 avatar image
1 Vote"
MotoX80 answered Ashwa commented

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

https://docs.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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you so much

0 Votes 0 ·