Link to home
Start Free TrialLog in
Avatar of creative555
creative555

asked on

issue with beginner powershell script.

Hello,
I am new to powershell. this is my learning script. I created try and catch and in the catch it suppose to create file with failed computers.
Below is the line. It created failedcomp.txt but it is empty. "Notonline" computer name should be there because this computer doesn't exist.
 $computer = out-file failedcomp.txt
please help
Avatar of creative555
creative555

ASKER

Function Get-DJOSInfo {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true,
        ValueFromPipeline=$true,
        ValueFromPipelineByPropertyName=$true)]
    [Alias('hostname')]
    [string[]]$ComputerName,
    [switch]$nameLog

)
Begin {
   if ($nameLog)
   {
       Write-Verbose "Finding name log file"
       $i = 0
    do {
    $logFile = "names-$i.txt"
    $i++
    } while (Test-Path $logFile)
    Write-verbose "Log file name will be stored $logfile"

    }
else {
write-verbose "Name logging off"
   } 


}
Process {
    if ($nameLog)
    {
        Write-Verbose "Name log on"
    }
    else
    {
       Write-Verbose "Name log off" 
    }

    foreach ($computer in $computername) {
    Write-Verbose "Now connecting to $computer"
    if ($nameLog){
    $computer | out-file $logFile -Append
    }

    try {
            $continue = $true
            $os = Get-WmiObject -ErrorAction 'Stop' -ComputerName $computer -class Win32_OperatingSystem |
            Select Caption,BuildNumber,OSArchitecture,ServicePackMajorVersion

        } catch {
            Write-Verbose "Connection to $computer failed"
            $continue = $false
            $computer = out-file failedcomp.txt
        }

        if ($continue) {
            Write-Verbose "Connection to $computer succeeded"
            $bios = Get-WmiObject -ComputerName $computer -Class Win32_BIOS |
                Select SerialNumber
            $processor = Get-WmiObject -ComputerName $computer -Class Win32_Processor |
                Select AddressWidth -first 1
            $osarchitecture = $os.osarchitecture -replace '-bit',''
            $properties = @{'ComputerName'=$computer;
                            'OSVersion'=$os.Caption;
                            'OSBuild'=$os.BuildNumber;
                            'OSArchitecture'=$osarchitecture;
                            'OSSPVersion'=$os.servicepackmajorversion;
                            'BIOSSerial'=$bios.SerialNumber;
                            'ProcArchitecture'=$processor.addresswidth}
            $obj = New-Object -TypeName PSObject -Property $properties
            Write-Output $obj
            }
 
}

}

End {}


}



Get-DJOSInfo -computername server01, notonline, server02.testtarget.local -Verbose -nameLog

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You also can use Export-CSV filename.csv to export if you have data you want to get in Excel.
I recommend to see i.e.
    https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/out-file 
for the syntax of out-file.

Your command writes an empty string (because no string is given) to the log file and stores the resultcode in $computer.

I think the command should read

out-file -FilePath failedcomp.txt -InputObject $computer

Open in new window


By the way:
Get-WmiObject has a hell of timeout if the queried node is down, which results in an enormous long time to scan a long list with many down nodes. It would be better to first try a single ping on that node prior to query it with Get-WmiObject ... if the ping fails, the Get-WmiObject query would be senseless.
thank you! This worked!!!