Home > Back-end >  PowerShell error "The argument is null or empty."
PowerShell error "The argument is null or empty."

Time:01-05

I am trying to create a PowerShell script that uses the below code. I believe that the projects folder having spaces is causing my error but I am unable to come up with a solution. I have tried numerous things from different stackoverflow solutions with no success. I am not a PowerShell guy so I know it is something simple (maybe). Anyone help me out there...

Script code:

Write-Host ""
$projectsFolder = Read-Host **'E:\Work - VS.NET 2022'**

Write-Host ""
Write-Host "Adding Path Exclusion: " $projectsFolder
Add-MpPreference -ExclusionPath $projectsFolder

foreach ($exclusion in $pathExclusions) 
{
    Write-Host "Adding Path Exclusion: " $exclusion
    Add-MpPreference -ExclusionPath $exclusion
}

Error:

 Adding Path Exclusion:   Add-MpPreference : Cannot validate argument
 on parameter 'ExclusionPath'. The argument is null or empty. Provide
 an argument that is not null or  empty, and then try the command
 again. At E:\Work - VS.NET 2022\Tools\Visual
 Studio\Windows_Defender_Exclusions_VS_2022.ps1:54 char:33
   Add-MpPreference -ExclusionPath $projectsFolder
                                   ~~~~~~~~~~~~~~~
       CategoryInfo          : InvalidData: (:) [Add-MpPreference], ParameterBindingValidationException
       FullyQualifiedErrorId : ParameterArgumentValidationError,Add-MpPreference

CodePudding user response:

I do not think you want to read host. I think you want to get the content of a file. Your code will be like the bottom but I also added a simple read-host for you to review.

==========read-host================

$YesNoResponse = $null
while ($YesNoResponse -ne "y" -and $YesNoResponse -ne "n") {
    Clear-Host
    write-host “Yes or No?”
    Write-Host "Please confirm."
    $YesNoResponse = Read-Host " [Y/N] "
    If ($YesNoResponse -eq "") {
        Write-Host "Enter was inputted"
        Return $null
    }
}

I think you are trying to do something like this. If you need something else please expand.

Write-Host "" 
$projectsFolder = Import-Csv 'E:\Work - VS.NET 2022'
Write-Host "" 
Write-Host "Adding Path Exclusion: " 
$projectsFolder = Add-MpPreference -ExclusionPath $projectsFolder

foreach ($exclusion in $pathExclusions) { 
    Write-Host "Adding Path Exclusion: " 
    $exclusion = Add-MpPreference -ExclusionPath $exclusion }

CodePudding user response:

The problem in your script is that the array you use for your loop ($pathExclusions) is not created and thus is empty, resulting in $exclusion variable being always empty/null... which explain your error message.

If the goal is to prompt the user to enter manually a list of directories to be processed, then you can do something like that:

# Create the exclusions array (of strings)
$pathExclusions = @()

Write-Host ""
$projectsFolder = Read-Host **'E:\Work - VS.NET 2022'**

# Press Enter at read-host prompt to exit the loop
while ($projectsFolder -ne "") {
    $pathExclusions  = $projectsFolder
    $projectsFolder = Read-Host **'E:\Work - VS.NET 2022'**
}

Write-Host ""

# Process each item in the exclusions array
# Loop not executed if $pathExclusions is empty so no error message
foreach ($exclusion in $pathExclusions) {
    Write-Host "Adding Path Exclusion: " $exclusion
    Add-MpPreference -ExclusionPath $exclusion
}

If the goal is to get the list of directories from a file, then just populate the exclusions array by getting the file content instead of the Read-Host loop:

$pathExclusions = Get-Content -Path "c:\temp\exclusionsfile.txt"
  •  Tags:  
  • Related