Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 8583

IF Else Statement

$
0
0

Hello,

I am running several scripts similar to the one below. While the script is running, it gives me a list of computers not responding along with a few other errors that I would like to capture on a report to keep track of which computers failed to run the script. Below the script are some of the errors I am getting. Is this report possible?

$password = "P@ssw0rd12345!"
$computers = Get-Content -path "C:\enumerated\Backup Admin Accounts.txt"
foreach ($computer in $computers)
{
   If (Test-Connection -ComputerName $computer -Count 1 -Quiet)
    {
        $userName = [ADSI]"WinNT://$computer,computer"
        $LocalAdmin = $userName.create("User", "Backup Admin")
        $LocalAdmin.SetPassword($Password)
        $LocalAdmin.SetInfo()
    }
    else
    {
        Write-Host "$computer is not responding"
    }
    }
   

 

Test-Connection : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Supply an argument that is not n
ull or empty and then try the command again.
At line:5 char:37
+    If (Test-Connection -ComputerName <<<<  $computer -Count 1 -Quiet)
    + CategoryInfo          : InvalidData: (:) [Test-Connection], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand

 

The following exception occurred while retrieving member "create": "Access is denied.
"
At line:8 char:22
+         $LocalAdmin = <<<<  $userName.create("User", "Backup Admin")
    + CategoryInfo          : NotSpecified: (:) [], ExtendedTypeSystemException
    + FullyQualifiedErrorId : CatchFromBaseGetMember

 

$computer is not responding
$computer is not responding


Viewing all articles
Browse latest Browse all 8583

Trending Articles