PARAM issue

Joseph Patrick 641 Reputation points
2020-10-05T18:10:38.21+00:00

I am trying to fix a script that was working below is the piece of code that is causing the problem:

param ([Switch]$detailed)  
Clear-Host  
###########################################################################################################################################################  
Function VALIDATEDCONLINE   
{  
	param ($server)  
	$Global:DCOnline = $Null  
	""  
	"------------------------------------------------------------------------------------"  
	"VALIDATING $SERVER IS ONLINE"  
	"------------------------------------------------------------------------------------"  
	""  
	$result = test-connection -computername $Server -quiet  
	if (!$result)  
	{  
		Write-host -ForegroundColor Red "Error, $server appears to be offline. Please check it manually"  
		write-host -ForegroundColor Red "Moving to next server..."  
		$Global:DCOnline = $false  
	}  
	else  
	{  
		Write-Host -ForegroundColor Green "$server is online. Beginning DCHealthCheck"  
	}  
}  

The Error I am getting is:

30147-capture.png

How can I fix this PARM issue?

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Joseph Patrick 641 Reputation points
    2020-10-05T19:54:59.423+00:00

    The piece of script that should be passing the server name is below, the Item listed ($Servers = $input) seemed to be the culprit, I removed the S and it fixed the script:

    Prompt user for dc or txt file with list of DCs
    $cwd = Get-Location
    $TextFileInput = "*.txt"
    $ServerInput = "*0*"
    $matchFound = $null
    ###Validate input
    while($validParamPassed -ne $true)
    {
    $input = (Read-Host "Please enter the name of the DC, a txt file, or a domain. Ex. DC_Name, servers.txt, ED.  Note: The txt file must be in the same directory as the script."`n)
        switch($input)
        {
            {$_ -like $TextFileInput}
                {   
                    $servers = Get-Content "$cwd\$input"
                    $validParamPassed = $true
                    break
                }
            {$_ -like $ServerInput}
                {
                    $Servers = $input
                    $validParamPassed = $true
                    break
                }
            #Validate that the DC is a DC in the forest that the healthcheck script is being executed in.
            {$_ -like "*"}
                {
                    $domains = ([system.directoryservices.activedirectory.forest]::getcurrentforest().Domains).name
                    foreach($domain in $domains)
                    {
                        if($domain -match $input)
                        {
                            $matchFound = $true
                        }
                    }
                    if($matchFound)
                    {
                        $DCs = [system.directoryservices.activedirectory.forest]::getcurrentforest().Domains | ForEach-Object {$_.DomainControllers} | Select Name -ExpandProperty Name
                        $Servers = $DCs -match $input
                        $validParamPassed = $true
                        break
                    }
                }
            default {Write-Host -ForegroundColor Red "Please enter valid input!";$validParamPassed = $false}
        }
    }
    Clear-Host  
    
    $now = Get-Date
    $string_now = $now.tostring("MM-dd-yyyy_HHmmss")
    $admin = whoami
    $logpath = "$CWD\DCHealthCheckLogs\"
    $pathtest = Test-Path $logpath 
    if ($pathtest -eq $false)
    {
        Write-Host "Creating $logpath Directory" 
        New-Item -Path $logpath -type directory | Out-Null
    }
    $logfile = "$logpath" + "DCHealthCheck" + "$string_now" + ".html"
    "`nStarting the Domain Controller Heath Check Script"
    "Initiated by: $admin"
    "Current Date/Time: $now"
    
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2020-10-05T18:49:58.11+00:00

    You fix it by passing the name of a computer when you call the function.

    VALIDATEDCONLINE computer-name
    

  2. Rich Matheisen 47,901 Reputation points
    2020-10-05T19:24:23.583+00:00

    I did give you an example!

    How about this:

    VALIDATEDCONLINE  REPLACE-THIS-STRING-WITH-THE-NAME-OF-A-COMPUTER
    
    0 comments No comments

  3. Rich Matheisen 47,901 Reputation points
    2020-10-05T19:42:24.6+00:00

    Your function is really isn't necessary. But if you want to use it you might consider using in like this, after making sure the parameter's been suppled and eliminating the unnecessary use of a global variable.

    Function VALIDATEDCONLINE 
    {
        param (
            [Parameter(Mandatory=$true)]
            [String]$server
        )
        if (test-connection -computername $Server -quiet){
            Write-Host -ForegroundColor Green "$server is online. Beginning DCHealthCheck"
            $true
        }
        else{
            Write-host -ForegroundColor Red "Error, $server appears to be offline. Please check it manually"
            write-host -ForegroundColor Red "Moving to next server..."
            $false
        }
    }
    
    Get-Content c:\junk\serverlist.txt |
        Foreach-Object{
            If (VALIDATEDCONLINE $_){
                # server's online
            }
            else{
                # server's offline
            }
        }
    

    Or just don't use a function to perform the test-connection and use something like this:

    Get-Content c:\junk\serverlist.txt |
        Foreach-Object{
            If (test-connection -computername $_ -quiet){
                Write-Host -ForegroundColor Green "$server is online. Beginning DCHealthCheck"
                # server's online, place your code here
            }
            else{
                # server's offline
                Write-host -ForegroundColor Red "Error, $server appears to be offline. Please check it manually"
                write-host -ForegroundColor Red "Moving to next server..."
            }
        }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.