Solved

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

Posted on 2016-11-22
3
45 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
3 Comments
 
LVL 69

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This article will help you understand what HashTables are and how to use them in PowerShell.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now