invoke-command cannot validate argument...

This topic contains 4 replies, has 3 voices, and was last updated by  Richard Burrs 4 months ago.

  • Author
    Posts
  • #70486

    Richard Burrs
    Participant

    Hello,

    I'm writing a script that will loop through all Exchange Mailbox servers to determine if a particular set of services related to backups are not running. If the services are not running, the script will attempt to start the services and send an email indicating if the services were started or not.

    I am getting the following error: "Invoke-Command: Cannot validate argument on parameter 'Computername'. The argument is null or empty. Provide and argument that is not null or empty, and then try the command again."

    Here's the script that I created:

    Add-PSSnapin  Microsoft.Exchange.Management.PowerShell.E2010
    
    $servers = Get-MailboxServer | sort Name
    $servicearray = 'sdmgmtsvc','snapdriveservice','snapmanagerservice','swsvc';
    
    foreach ($server in $servers)
    {
        $services = Get-Service -ComputerName $servers -Include $servicearray |
                    Format-Table Status,Name,DisplayName,MachineName -AutoSize
        
            foreach ($service in $services)
            {
                    if ($service.status -ne 'Running')
                    {
                        $servername = $service.MachineName
                        $servicename = $service.Name
                        $result = Invoke-Command -ComputerName $servername -ScriptBlock {Start-Service -Include $servicename} -ArgumentList $servicename                                          
    
                        if ($result -match "Service Started")
                        {   $body = "Started $servicename on $servername"
                            $from = "noreply@domain.com"
                            $to = "username@domain.com"
                            $subject = "SnapDrive and SnapManger Service Alert"
                            $smtpserver = "servername@domain.com"}
    
                        if ($result -match "Service Failed to Start")
                        {   $body = "Failed to Start $servicename on $servername.  Please start $servicename to ensure backup jobs execute."
                            $from = "noreply@domain.com"
                            $to = "username@domain.com"
                            $subject = "SnapDrive and SnapManger Service Alert"
                            $smtpserver = "servername@domain.com"}
    
                        if ($body)
                        {   Send-MailMessage -From $from -To $to -Subject $subject -Body $body -SmtpServer $smtpserver -BodyAsHtml}
                       
                    }        
                    
            }    
            
    }    
    

    Thanks,

    Richard

  • #70513

    James Bernie
    Participant

    1st thing I see and probably the issue is that your foreach statement says ($server in $servers) but then use $servers for the computername so you send the entire array as opposed to just a single computer name & that will blow up.

    foreach ($server in $servers)
    {
    $services = Get-Service -ComputerName $servers

    It's been a few years since I did PowerShell with Exchange, but from other stuff I have been working on you may want to add a $result option if the service is maybe stuck in the starting state or similar. There is success, failure & something in the middle which if you don't cover you might miss an issue.

  • #71023

    Richard Burrs
    Participant

    James Bernie,

    Thanks for the suggestions. Although, changing the ComputerName value did not resolve the problem. I'm still getting the "argument is Null or empty" error message regarding the ComputerName parameter.

    Richard

  • #71075

    Max Kozlov
    Participant

    Your problem not computername, but $services variable
    $services = Get-Service -ComputerName $servers -Include $servicearray |
    Format-Table Status,Name,DisplayName,MachineName -AutoSize

    Format-Table used only to display information on screen(host) but should never be assigned to variable
    but after it you try to use it as object
    foreach ($service in $services) and so on

  • #72973

    Richard Burrs
    Participant

    Max Kozlov,

    Sorry I had to step away from this for a minute. But your suggestion worked. Piping to FT in the services variable was the problem.

    Thanks,

    Richard

You must be logged in to reply to this topic.