I am trying to get computer information from remote computers and then put it in excel. For the most part its working but I'm trying to include the info in the else scriptblock to but I can't seem to make that happen here is the script.
Function Get-Compinfo {
[CmdletBinding()]
Param(
[Parameter (ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[Alias("HostName","CN")]
[String[]]$ComputerName= 'LocalHost',
)
Begin{}
Process {
ForEach ($Client in $ComputerName) {
if (Test-Connection -Computername $client -BufferSize 16 -Count 1 -Quiet) {
$System = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $Client
$Bios = Get-WmiObject -Class Win32_Bios -ComputerName $Client
$os=Get-Wmiobject -ComputerName $Client -Class Win32_OperatingSystem
$Disk=Get-WmiObject -ComputerName $Client -class Win32_LogicalDisk -filter "DeviceID='c:'"
$Prop=[Ordered]@{
'ComputerName'=$System.Name;
'Manufacturer'=$System.Manufacturer;
'Model'=$System.Model;
'Username'=$System.Username;
'OS Name'=$os.caption;
'OSBit'=$OS.OSArchitecture;
'Service Pack'=$os.Servicepackmajorversion;
'Serial Number'=$Bios.SerialNumber
'DiskSize(c:)'=$Disk.Size / 1gb -as [int];
'FreeSpace'=$Disk.Freespace / 1gb -as [int]
}
$Obj=New-Object -TypeName PSObject -Property $Prop
Write-Output $Obj
}
Else {"$Client is notonline"}
}
}
End {}
}
Here is the Info I get by just running it in the shell. It looks fine, but when I export it is when I get the issue. where it says tms-601-ws01 is notonline will not show up in excel just a blank shell. I'm sure I'm not doing something right I pretty new to powershell.
PS D:\Documents\Powershell> Get-Compinfo -ComputerName (Get-Content D:\Documents\Powershell\computers.txt)
ComputerName : THS-AD2
Manufacturer : Dell Inc.
Model : PowerEdge R320
Username :
OS Name : Microsoft Windows Server 2012 R2 Standard
OSBit : 64-bit
Service Pack : 0
Serial Number :
DiskSize(c:) : 195
FreeSpace : 183
tms-601-ws01 is notonline
ComputerName : TMS2
Manufacturer : Dell Computer Corporation
Model : PowerEdge 1800
Username :
OS Name : Microsoft(R) Windows(R) Server 2003, Standard Edition
OSBit :
Service Pack : 2
Serial Number :
DiskSize(c:) : 12
FreeSpace : 1
notonline is notonline
ComputerName :
Manufacturer : Hewlett-Packard
Model : HP Compaq Pro 6300 All-in-One PC
Username :
OS Name : Microsoft Windows 8.1 Pro
OSBit : 64-bit
Service Pack : 0
Serial Number :
DiskSize(c:) : 232
FreeSpace : 160
| ComputerName | Manufacturer | Model | Username | OS Name | OSBit | Service Pack | Serial Number | DiskSize(c:) | FreeSpace |
| THS-AD2 | |||||||||
| TMS2 | Dell Computer Corporation | PowerEdge 1800 | Microsoft(R) Windows(R) Server 2003, Standard Edition | 12 | 1 | ||||
| test | Hewlett-Packard | HP Compaq Pro 6300 All-in-One PC | Microsoft Windows 8.1 Pro | 64-bit | 232 | 160 | |||
Any help would be appreciated
Sorry Kinda long