1

I have a powershell function that I inherited. it works fine when using no input or providing a single server name as input.

Function works when running like this

Get-DiskUtil or

Get-DiskUtil computername

But if I try

Get-Content c:\psfiles\servers.txt | Foreach-Object {Get-DiskUtil} (this give the output of the local machine three times if I have three server listed in servers.txt).

or

Get-Content C:\psfiles\servers.txt | Get-DiskUtil

The result only gives the output of the local machine name and this error three times.

Can someone tell me why Get-Content does not work and how I might go about fixing this?

computername=$_ : The term 'computername=$_' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a 
path was included, verify that the path is correct and try again.
At D:\Data\WindowsPowerShell\Modules\MyFunctions\Get-DiskUtil.ps1:4 char:10
+ if ($_) {computername=$_}
+          ~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (computername=$_:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Get-DiskUtil:

Function Get-DiskUtil {
Param([string] $computername=$env:computername)
Process {
if ($_) {computername=$_}
gwmi win32_logicaldisk -fi "drivetype=3" -comp $computername |
Select @{Name="Computername";Expression={$_.systemName}},
DevicedID,
@{Name="SizeGB";Expression={"{0:N2}" -f ($_.Size/1GB)}},
@{Name="FreeGB";Expression={"{0:N2}" -f ($_.Freespace/1GB)}},
@{Name="UsedGB";Expression={"{0:N2}" -f (($_.Size-$_.FreeSpace)/1GB)}},
@{Name="PerFreeGB";Expression={"{0:P2}" -f ($_.Freespace/$_.size)}}
}
}

3 Answers 3

4

Looks like a simple typo to me. This line:

if ($_) {computername=$_}

should be:

if ($_) {$computername=$_}

There are many advantages to using advanced functions, but it's not required to have a function that accepts input from the pipeline.

Sign up to request clarification or add additional context in comments.

Comments

3

The issue is that you have created a function. Functions do not accept input from the pipeline you need to create an advanced function.

Function Get-DiskUtil {
     [CmdLetBinding]
     Param([parameter(Mandatory=$true,
        ValueFromPipeline=$true)]
          [string] $computername=$env:computername)
     )

This will allow that paramater to be taken in on the pipe line. Not you could also solve this by rearaging your calling code

Get-Content c:\psfiles\servers.txt | % {Get-DiskUtil $_}

Comments

1

All you need to do is add the [Parameter(ValueFromPipeline = $true)] attribute to your parameter declaration. You also need to declare the $ComputerName parameter as an array, since it will accept either a single value, or an array of values.

Here is the modified function.

function Get-DiskUtil {
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline = $true)]
        [string[]] $ComputerName = $env:COMPUTERNAME
    )

    process {
        Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType = 3" -ComputerName $ComputerName |
        Select-Object -Property @{Name="Computername"; Expression={$_.systemName} },
        DevicedID,
        @{ Name="SizeGB"; Expression={"{0:N2}" -f ($_.Size/1GB)}},
        @{ Name="FreeGB"; Expression={"{0:N2}" -f ($_.Freespace/1GB)}},
        @{ Name="UsedGB"; Expression={"{0:N2}" -f (($_.Size-$_.FreeSpace)/1GB)}},
        @{ Name="PerFreeGB"; Expression={"{0:P2}" -f ($_.Freespace/$_.size)}}
    }
}

# Call the function, passing in an array of values
'localhost','localhost' | Get-DiskUtil

2 Comments

This works as posted. Not declaring the $ComputerName is why I was getting the output error. Thank you very much and Happy New Year.
Great! Glad to hear you're up and running! Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.