Posh script help

Hi guys I am new hire . Trying to get some help on this script . Running the script from a Windows Server 2012 R2 running Powershell v4 . Trying to grab a remote machine’s release Id which is a Windows 10 machine . It grabs the OS.caption part it doesn’t grab the ReleaseId.

$computers = REMOTE PC Name
foreach ($computer in $computers) {
    $Bios = Get-WmiObject win32_bios -Computername $Computer
    $Hardware = Get-WmiObject Win32_computerSystem -Computername $Computer
    $Sysbuild = Get-WmiObject Win32_WmiSetting -Computername $Computer
    $OS = Get-WmiObject Win32_OperatingSystem -Computername $Computer
    $OSBuild = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ReleaseId'
    $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | Where-Object {$_.IPEnabled}
    $driveSpace = Get-WmiObject win32_volume -computername $Computer -Filter 'drivetype = 3' |
    Select-Object PScomputerName, driveletter, label, @{LABEL='GBfreespace';EXPRESSION={'{0:N2}' -f($_.freespace/1GB)} } |
    Where-Object { $_.driveletter -match 'C:' }
    $cpu = Get-WmiObject Win32_Processor  -computername $computer
    $username = Get-ChildItem "\\$computer\c$\Users" | Sort-Object LastWriteTime -Descending | Select-Object Name, LastWriteTime -first 1
    $totalMemory = [math]::round($Hardware.TotalPhysicalMemory/1024/1024/1024, 2)
    $lastBoot = $OS.ConvertToDateTime($OS.LastBootUpTime)
 
    $IPAddress  = $Networks.IpAddress[0]
    $MACAddress  = $Networks.MACAddress
    $systemBios = $Bios.serialnumber
    $OutputObj  = New-Object -Type PSObject
    $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
    $OutputObj | Add-Member -MemberType NoteProperty -Name Manufacturer -Value $Hardware.Manufacturer
    $OutputObj | Add-Member -MemberType NoteProperty -Name Model -Value $Hardware.Model
    $OutputObj | Add-Member -MemberType NoteProperty -Name Processor_Type -Value $cpu.Name
    $OutputObj | Add-Member -MemberType NoteProperty -Name System_Type -Value $Hardware.SystemType
    $OutputObj | Add-Member -MemberType NoteProperty -Name Operating_System -Value “$($OS.caption) $($OSBuild.ReleaseId)"

   
    #$OutputObj | Add-Member -MemberType NoteProperty -Name Operating_System_Version -Value $OS.version
    $OutputObj | Add-Member -MemberType NoteProperty -Name Operating_System_BuildVersion -Value $SysBuild.BuildVersion
    $OutputObj | Add-Member -MemberType NoteProperty -Name Serial_Number -Value $systemBios
    $OutputObj | Add-Member -MemberType NoteProperty -Name IP_Address -Value $IPAddress
    $OutputObj | Add-Member -MemberType NoteProperty -Name MAC_Address -Value $MACAddress
    $OutputObj | Add-Member -MemberType NoteProperty -Name Last_User -Value $username.Name
    $OutputObj | Add-Member -MemberType NoteProperty -Name User_Last_Login -Value $username.LastWriteTime
    $OutputObj | Add-Member -MemberType NoteProperty -Name C:_FreeSpace_GB -Value $driveSpace.GBfreespace
    $OutputObj | Add-Member -MemberType NoteProperty -Name Total_Memory_GB -Value $totalMemory
    $OutputObj | Add-Member -MemberType NoteProperty -Name Last_ReBoot -Value $lastboot
   #$OutputObj | Export-Csv $exportLocation -Append -NoTypeInformation
  }
 Write-Output $OutputObj

I don’t believe the Win32_OperatingSystem class includes ReleaseId. You’d need to find somewhere else to get that.

For example, How to find the build / version of Windows 10 - Super User suggests a registry location. However, querying the registry remotely is a bit tricky, so you’ll need to get that working first.

Note that your script is already grabbing reg info, but it’s doing so from the local computer. So you’re not getting correct results.

The line you need is:

$OSBuild = Get-ItemProperty 'HKLM:\software\Microsoft\Windows NT\CurrentVersion\' |
    Select-Object -ExpandProperty ReleaseId

Also, you don’t need to use massive reams of Add-Member here. Just use this to construct your objects:

[PsCustomObject]@{
    Property = Value
    Property2 = Value
    Etc = Etc
}

Thank You Don and Joel . I tried both of your suggestions in this script here . I am still not getting the Windows 10 release Id remotely. I am using Windows Server 2012 R2 to grab info on a Windows 10 machine remotely .

$Computers = "RemotePCName01"

foreach ($Computer in $Computers) 
{
    $computerSystem =   get-wmiobject Win32_ComputerSystem -Computer $Computer
    $ComputerModel  =   get-WmiObject Win32_ComputerSystemProduct -ComputerName $Computer
    $computerBIOS   =   get-wmiobject Win32_BIOS -Computer $Computer
    $computerOS     =   get-wmiobject Win32_OperatingSystem -Computer $Computer
    $OSBuild = Invoke-command -computer $Computer {Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\WINDOWS NT\CurrentVersion\' | Select-Object -ExpandProperty ReleaseId}
    $computerCPU    =   get-wmiobject Win32_Processor -Computer $Computer
    $computerHDD    =   get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter drivetype=3
       #Write-Host "Computer Name: " $computerSystem.Name -BackgroundColor Black -ForegroundColor Red
       # "-------------------------------------------------------"
        Write-Host "Computer Name: " $computerSystem.Name -BackgroundColor Black -ForegroundColor Red
        "Manufacturer: " + $computerSystem.Manufacturer
        "MachineType: " + $computerSystem.Model.Substring(0,4)
        "Model: " + $ComputerModel.Version
        "Serial Number: " + $computerBIOS.SerialNumber
        #"CPU: " + $computerCPU.Name
        #"HDD Capacity: "  + "{0:N2}" -f ($computerHDD.Size/1GB) + "GB"
        #"HDD Space: " + "{0:P2}" -f ($computerHDD.FreeSpace/$computerHDD.Size) + " Free (" + "{0:N2}" -f ($computerHDD.FreeSpace/1GB) + "GB)"
        #"RAM: " + "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
        "Operating System: " + $computerOS.caption + ", BuildNumber: " + $OSBuild.ReleaseId
        "User logged In: " + $computerSystem.UserName
        #"Last Reboot: " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
        ""
        #"-------------------------------------------------------"
}

This is what I get when I run the code

Computer Name: RemotePCName01
Manufacturer: LENOVO
MachineType: 20AR
Model: Thinkpad T440S
Serial Number: PC01XX84
Operating System: Microsoft Windows 10 Enterprise, BuildNumber:

$OSBuild.ReleaseId

This value doesn’t exist. Since you’ve used Select-Object -ExpandProperty ReleaseId in the initial invocation to populate this variable, the $OSBuild variable itself just contains the release ID, it’s just the integer or string value (not sure which it’d come back as, to be honest, but probably string) and nothing else.

In other words, you should just use:

"Operating System: " + $computerOS.caption + ", BuildNumber: " + $OSBuild

Thank You , Joel that worked .

“Operating System: " + $computerOS.caption + ", BuildNumber: " + $OSBuild

Tried using my other script with the Add-members . It doesn’t pull the $OSBUILD.

Using this

$OSBuild = Get-ItemProperty 'HKLM:\software\Microsoft\Windows NT\CurrentVersion' | Select-Object -ExpandProperty ReleaseId 

with this

$OutputObj | Add-Member -MemberType NoteProperty -Name Operating_System -Value $OS.Caption.Substring(10) + " " + $OSBuild
it doesn't give me the Operating system or the $osbuild.

But if I do this

$OS.Caption.Substring(10) + " " + $OSBuild
I get Windows 10 Pro 1803

You probably need to either enclose that value parameter in parentheses, or insert them into a complete string:

# Option 1
$OutputObj | Add-Member -MemberType NoteProperty -Name Operating_System -Value ($OS.Caption.Substring(10) + " " + $OSBuild)
# Option 2
$OutputObj | Add-Member -MemberType NoteProperty -Name Operating_System -Value "$($OS.Caption.Substring(10)) $OSBuild"

Although it should be noted that neither of those are necessary if you construct the object with a hashtable instead of Add-Member lists, as it evaluates the entire expression before putting it into the hashtable instead of mistakenly assuming the parts after the space are meant to be passed to different parameters.

Joel

Looks like this two options worked

# Option 1
$OutputObj | Add-Member -MemberType NoteProperty -Name Operating_System -Value ($OS.Caption.Substring(10) + " " + $OSBuild)

Results :
ComputerName : PC01
Operating_System : Windows 10 Pro 1803
Operating_System_Version : 10.0.17133
Operating_System_BuildVersion : 17133.1
<prep

Option 2
OutputObj | Add-Member -MemberType NoteProperty -Name Operating_System -Value "$($OS.Caption.Substring(10)) $OSBuild"

Results :
ComputerName : PC01
Manufacturer : Microsoft Corporation
Model : Surface Book
Operating_System : Windows 10 Pro 1803

Is there a way to remove the huge white space between Manufacturer and Model ?