Solved

error with powershell script Out-File : Cannot bind argument to parameter 'FilePath' because it is null.

Posted on 2016-11-22
3
159 Views
Last Modified: 2016-11-26
Hello,
I just started learning powershell and doing training. This is the script that I am practicing. This is exactly how they show in th video from Lynda.com but I am getting this error. Please help. thank you. I am posting the script that I wrote below

Also, I am getting this "message" prompt. See the screenshot. Where is this coming from? This script should not be prompting for "message"

and here is the error
Out-File : Cannot bind argument to parameter 'FilePath' because it is null.
At C:\cfscripts\function1.ps1:45 char:26
+     $computer | out-file $logFile -Append
+                          ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Out-File], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.OutFileCommand

Open in new window

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
    }
        $os = Get-WmiObject -ComputerName $computer -class Win32_OperatingSystem |
            Select Caption,BuildNumber,OSArchitecture,ServicePackMajorVersion
        $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

#write-host $computername   
}

}

End {}


}


#Get-DJOSInfo -computername server01

Open in new window

error message
0
Comment
Question by:creative555
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 41898327
Lines 33 and 34 belong together into a single line - this is the "message" thingy you get.
The out-file error results from - no clue. The script should search for existing log files and create a new one (by incrementing the index number).
You can help yourself by adding -verbose when calling of the procedure; that activates the write-verbose messages, which otherwise are suppressed. They will print some debugging info.
0
 

Author Closing Comment

by:creative555
ID: 41902683
thanks! tested and it worked!!!
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A procedure for exporting installed hotfix details of remote computers using powershell
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question