how to amend formatting of output - powershell

when I do the below I get the format as "#{xxx}"...how do I just get the computer description and none of the the other formatting around it?
$CompDes = Get-WmiObject -Class Win32_OperatingSystem |Select
Description
Write-Host "Computer Description $CompDes"
Output:
Computer Description #{Description=TESING_SERVER}
Thanks,

$CompDes = Get-WmiObject -Class Win32_OperatingSystem | Select -exp Description
Write-Host "Computer Description $CompDes"
-exp is short for -ExpandProperty. It gets the value of the specified property.

Another way:
$CompDes = Get-WmiObject -Class Win32_OperatingSystem | Foreach-Object {$_.Description}

Related

Not able to format output as CSV [duplicate]

This question already has an answer here:
Get WMI Data From Multiple Computers and Export to CSV
(1 answer)
Closed 3 years ago.
Am not able to convert PS output to CSV format using echo function. I need to collect hardware information about multiple servers and got this script from internet. I modified it to collect only the necessary information such as Computername,HDD space, CPU details and RAM.
Below is my code:
$ArrComputers = "PC17"
Clear-Host
foreach ($Computer in $ArrComputers) {
$computerSystemRam = Get-WmiObject Win32_ComputerSystem -Computer $Computer |
select #{n="Ram";e={[math]::Round($_.TotalPhysicalMemory/1GB,2)}} |
FT -HideTableHeaders -AutoSize
$computerCPU = Get-WmiObject Win32_Processor -Computer $Computer |
select Name |
FT -HideTableHeaders
$computerCPUCores = Get-WmiObject Win32_Processor -Computer $Computer |
select NumberOfLogicalProcessors |
FT -HideTableHeaders -AutoSize
$computerC = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID= 'C:'" -ComputerName $Computer |
select #{n="Size";e={[math]::Round($_.Size/1GB,2)}} |
FT -HideTableHeaders -AutoSize
$computerD = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID= 'D:'" -ComputerName $Computer |
select #{n="Size";e={[math]::Round($_.Size/1GB,2)}} |
FT -HideTableHeaders -AutoSize
$computerE = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID= 'E:'" -ComputerName $Computer |
select #{n="Size";e={[math]::Round($_.Size/1GB,2)}} |
FT -HideTableHeaders -AutoSize
echo $computer,$computerC,$computerD,$computerE,$computerSystemRam,$computerCPU,$computerCPUCores
}
and my output is coming as
PC17
99.9
12
537.11
15.98
Intel(R) Xeon(R) CPU E5-2630 0 # 2.30GHz
12
What I need is to get this outputs as a comma separated value like below
PC17,99.9,12,537.11,15.98,Intel(R) Xeon(R) CPU E5-2630 0 # 2.30GHz,12
so that I can open it in Excel. Please let me know what the problem here is? Or any other alternative solution to so as to get the output as .csv.
Remove the Format-Table, use ExpandProperty and choose the right property from the array,
Also, I used -f to format the csv, see the differences:
foreach ($Computer in $ArrComputers)
{
$computerSystemRam = get-wmiobject Win32_ComputerSystem -Computer $Computer | select #{n="Ram";e={[math]::Round($_.TotalPhysicalMemory/1GB,2)}}
$computerCPU = get-wmiobject Win32_Processor -Computer $Computer | select -ExpandProperty Name
$computerCPUCores = get-wmiobject Win32_Processor -Computer $Computer | select -ExpandProperty NumberOfLogicalProcessors
$computerC = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID= 'C:'" -ComputerName $Computer | select #{n="Size";e={[math]::Round($_.Size/1GB,2)}}
$computerD = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID= 'D:'" -ComputerName $Computer | select #{n="Size";e={[math]::Round($_.Size/1GB,2)}}
$computerE = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID= 'E:'" -ComputerName $Computer | select #{n="Size";e={[math]::Round($_.Size/1GB,2)}}
"{0},{1},{2},{3},{4},{5},{6}" -f $computer,$computerC.Size,$computerD.Size,$computerE.Size,$computerSystemRam.Ram,$computerCPU,$computerCPUCores
}

script execution omits commands

I have a small script to get information about a network computer in my domain but during the execution the script omits two lines.
$HostName = Read-Host 'Veuillez donner le nom du poste que vous voulez verifier?'
If (Test-Connection $HostName -count 4 -quiet)
{
Write 'The host responded';
gwmi win32_volume -ComputerName $HostName -Filter 'drivetype = 3' | select driveletter, label, #{LABEL='GBfreespace';EXPRESSION={"{0:N2}" -f ($_.freespace/1GB)} };
Get-WmiObject Win32_OperatingSystem -ComputerName $HostName | Select-Object PSComputerName, Description, OSArchitecture;
Get-ADComputer $HostName | select DistinguishedName;
Get-WmiObject -Class Win32_Product -Computer $HostName | Format-Table -Property Name, Version, InstallDate, Vendor;
}
else
{
Write 'The host doesn''t responded';
}
The problem does not seems to be in the commands (I get the desired result by typing them directly in the console), but in the position of the command.
I tried to invert the order of the commands and the result was always like this:
After the if statement:
the two first commands are displayed
the 3rd and the 4th are omitted
the last one is again displayed
I also tried it like this:
If (Test-Connection $HostName -count 4 -quiet)
{
Get-WmiObject Win32_OperatingSystem -ComputerName $HostName | Select-Object PSComputerName, Description, OSArchitecture;
Get-ADComputer $HostName | select DistinguishedName;
Get-ADComputer $HostName | select DistinguishedName;
Get-WmiObject Win32_OperatingSystem -ComputerName $HostName | Select-Object PSComputerName, Description, OSArchitecture;
Get-ADComputer $HostName | select DistinguishedName;
Write 'The host responded';
gwmi win32_volume -ComputerName $HostName -Filter 'drivetype = 3' | select driveletter, label, #{LABEL='GBfreespace';EXPRESSION={"{0:N2}" -f ($_.freespace/1GB)} };
Get-WmiObject -Class Win32_Product -Computer $HostName | Format-Table -Property Name, Version, InstallDate, Vendor;
}
After the if statement:
the command 1 is displayed
the commands 2 and 3 are omitted
the 4th command is displayed
the 5th command is again omitted
the 6th is displayed
the 7th is omitted and finally the 8th is displayed

Pull NT user ID from powershell

get-wmiobject -class win32_computersystem -computername c73118 | format-table username
Will output something similar to:
username
--------
GHS_NTDOMAIN\amacor
Is it possible to only output the amacor part only?
first, you don't really want FT for this I don't think. Use Select -Expand instead. So doing that we get back the string GHS_NTDOMAIN\amacor. Once you have that, you can do .Split("\") to split it into an array of strings, and [-1] to specify the last string in the array. So it would look like:
(get-wmiobject -class win32_computersystem -computername c73118 | Select -ExpandProperty username).Split("\")[-1]
That will result in:
amacor
Or if you wanted to be a bit more verbose about it, you can do:
$Data = get-wmiobject -class win32_computersystem -computername c73118
$DomainUser = $Data.Username
$UserName = $DomainUser.Split("\")[-1]
Then $UserName = "amacor"
Edit: Updated per Andy Arismendi's excellent suggestion.

How to format 1-liner for printing an object property

How can I write a quick 1-liner for write-host'ing an object property (let's say Name)? Here is the object I want to print the Name of ...
Get-WmiObject -Class win32_ComputerSystem -namespace "root\CIMV2"
I tried ...
write-host $_.name | Get-WmiObject -Class win32_ComputerSystem -namespace "root\CIMV2"
But this seems to still print all object properties. What can I do to fix this command?
You can use the -ExpandProperty parameter of the Select-Object cmdlet to retrieve just the computer name, then pipe that to Write-Host (formatted as multiple lines for readability):
Get-WmiObject -Class win32_ComputerSystem -namespace "root\CIMV2" `
| Select-Object -ExpandProperty 'Name' `
| Write-Host;
Alternatively, use the ForEach-Object cmdlet to get the Name property:
Get-WmiObject -Class win32_ComputerSystem -namespace "root\CIMV2" `
| ForEach-Object { $_.Name; } `
| Write-Host;
This is not a one-liner, but another approach similar to what you tried:
$computer = Get-WmiObject -Class win32_ComputerSystem -namespace "root\CIMV2";
Write-Host $computer.Name;
Note that since you only care about the Name property of Win32_ComputerSystem, it's a good idea to communicate that to Get-WmiObject using the -Property parameter so it doesn't bother returning information that will be discarded anyways:
Get-WmiObject -Class win32_ComputerSystem -namespace "root\CIMV2" -Property 'Name'
In addition to the answer from BACON, another option is this (needs PowerShell v3 or higher):
Write-Host (Get-WmiObject -Class win32_ComputerSystem -namespace "root\CIMV2").Name

How do I include computername in output

PS newbe here...
How do I get the remote computer name to appear in the output?
$computer = "PC3090-121","APCD02"
Get-WmiObject Win32_Printer -ComputerName $computer |
Select-Object SystemName,Name,Local |
Format-Table -AutoSize
I've tried including -computername, computername, %computername% in the Select and format-table -properties - no joy...
My searches have come up empty, or I couldn't understand them.
------------------------------ answer:
$computer = "PC3090-121","APCD02"
Get-WmiObject Win32_Printer -ComputerName $computer |
Select-Object __Server, Name, Local |
Format-Table -AutoSize
How about simply
Get-WmiObject Win32_Printer -ComputerName $computer |
Select-Object SystemName,Name,Local |
Format-Table -AutoSize
There is no computername property on the resulting object, nor is there a %computername% property. What exists is SystemName.