SS-WQY
SS-WQY

Reputation: 59

ASCII conversion, format list and output to txt file

I'm currently studying Powershell and working on a script that grabs Display configuration from windows system.

Here is the script I am using to get the data of monitors' manufacturer and model and convert into alphanumeric, and then save to system1.txt:

Get-WmiObject WmiMonitorID -Namespace root\wmi | ForEach-Object 
{($_.ManufacturerName | foreach {[char]$_}) -join "";($_.UserFriendlyName | 
foreach {[char]$_}) -join ""}  | out-file -append system1.txt

The output looks like:

ACI
ASUS PB287Q
SAM
SMS27A850

And then, I also create a script which is for adding ManufacturerName and UserFriendlyName in front of the above result:

Get-WmiObject WmiMonitorID -Namespace root\wmi | Format-List 
ManufacturerName,UserFriendlyName

The output is:

ManufacturerName : {65, 67, 73, 0...}
UserFriendlyName : {65, 83, 85, 83...}

ManufacturerName : {83, 65, 77, 0...}
UserFriendlyName : {83, 77, 83, 50...}

My question is: how to combine the two script together to make the output looks like and save to system1.txt (and also, how can I replace 'UserFriendlyName' by 'Model' in the result in system1.txt):

  Manufacturer: ACI
  Model: ASUS PB287Q

  Manufacturer: SAM
  Model: SMS27A850

Besides, someone suggested me to use:

$ByteArray = @(65, 65, 61, 64, 64, 65)
$String = [System.Text.Encoding]::ASCII.GetString($ByteArray)

But I dont get it, could you guys also please show me how to realize by using this?

Thank advance for all replies :)

Upvotes: 1

Views: 251

Answers (1)

user6811411
user6811411

Reputation:

Create a PSCustomObject:

Get-WmiObject WmiMonitorID -Namespace root\wmi | ForEach-Object {
     [PSCustomObject]@{
         Manufacturer = ($_.ManufacturerName | ForEach {[char]$_}) -join ""
         Model        = ($_.UserFriendlyName | ForEach {[char]$_}) -join ""
     }
 } | Format-List | Out-File .\system1.txt -Append -Encoding ascii

Sample output (having two Samsung Syncmaster):

> gc .\system1.txt

Manufacturer : SAM
Model        : SyncMaster

Manufacturer : SAM
Model        : SyncMaster

Upvotes: 1

Related Questions