Author Posts

March 16, 2018 at 3:02 pm

All

I am trying to call a function from my ps1 file but its not calling. This is what I get. Any help is appreciated

PS C:\Work\PowerShell\Scripts> .\Get-ComputerInfo.ps1 compname1, compname2
Computer count & Details for: 2 compname1
Computer count & Details for: 2 compname2

#######################################################################

[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true)]
[String[]]$ComputerName
)
Clear-Host

#declaring variables
$CompInfo = @()
$isPingable = "No"
$IP = "N/A"
$OS = "N/A"
$ServicePack = "N/A"
$Uptime = "N/A"
$LastBoot = "N/A"

#loop through each computer name and call the function
Foreach($CN in $ComputerName)
{
Write-Host ("Computer count & Details for: " + $ComputerName.Count + " " + $CN)

#call function
Get-ComputerInfo($CN)
}

#display the final result in table form
$CompInfo | Format-Table -auto

#function body
function Get-ComputerInfo($ComputerName)
{
#check if computer is up and running
$isPingable = Test-Connection -ComputerName $ComputerName -Count 1 -Quiet
# if we can ping the computer get details
if ($isPingable)
{
$IPAddress = Test-Connection -ComputerName $ComputerName -Count 1
$compInfo = Get-WmiObject -Class Win32_operatingsystem -ComputerName $ComputerName
$TotalSeconds = (get-date).Subtract( [System.Management.ManagementDateTimeConverter]::ToDateTime($compInfo.LastBootUpTime)).TotalSeconds
$TSeconds = [timespan]::fromseconds($TotalSeconds)

#put the requested info in an object
$obj = [PSCustomObject]@{Computername = $compInfo.PSComputerName ;
Pinged = $isPingable ;
IP = $IPAddress.IPV4Address ;
OS1= $compInfo.Caption ;
ServicePack = $compInfo.CSDVersion ;
Uptime = $TSeconds.ToString("hh\:mm\:ss\,fff") ;
LastBoot = [System.Management.ManagementDateTimeConverter]::ToDateTime($compInfo.LastBootUpTime ) }
}
else
{
Write-warning ("Sorry cant connect to the requested computer $CN... Please check and try again")

$obj = [PSCustomObject]@{Computername = $CN ;
Pinged = $isPingable ;
IP = $IP ;
OS1= $OS ;
ServicePack = $ServicePack ;
Uptime = $Uptime;
LastBoot = $LastBoot }
}
$CompInfo += $obj
}

#######################################################################

March 16, 2018 at 3:07 pm

If you could in the future, please take a moment to format your code as indicated in the bullet list above the posting textbox. It makes helping you a lot easier and more accurate! Longer code like yours would also be better as a Gist, since the formatting is even clearer.

PowerShell executes scripts from top-to-bottom. That means functions need to be defined BEFORE they can be called. As-is, you are calling your function before PowerShell has seen it, and so PowerShell doesn't think your function exists.

You're also calling the function incorrectly.

Get-ComputerInfo($CN)

Is a common mistake if you've come from another programming language. The correct pattern would be:

Get-ComputerInfo -ComputerName $CN

Parentheses can get you into a situation where PowerShell creates an array, which isn't what you want. You've made the same mistake in several locations, such as with your Write-Warning. In your current context it will still work, but it's a habit you will want to break, since in other contexts it will cause difficult-to-diagnose problems.