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)}}
}
}