0

I am currently using a script to pull information from monitors such as Manufacturer, Model, and Serial Number. At the moment the script exports the file to a folder that has already been created. I am needing to modify the script so it can automatically create the folder and file.

Script:

$Monitors = Get-WmiObject WmiMonitorID -Namespace root\wmi
$LogFile = "C:\test\monitors.txt"

function Decode {
    if ($args[0] -is [System.Array]){
        [System.Text.Encoding]::ASCII.GetString($args[0])
    }
    Else {
        "Not Found"
    }
}

echo "Manufacturer", "Name", "Serial Number"

ForEach ($Monitor in $Monitors) {
    $Manufacturer = Decode $Monitor.ManufacturerName -nomatch 0
    $Name = Decode $Monitor.UserFriendlyName -nomatch 0
    $Serial = Decode $Monitor.SerialNumberID -nomatch 0

    echo "$Manufacturer, $Name, $Serial" >> $LogFile
}
2
  • What is the problem? What have you tried? Commented Jan 22, 2021 at 21:28
  • The problem is that the script currently needs me to manually create the folder. I have tried using Out-File -FilePath "C:\test\monitors.txt" but it runs into an error saying that the path doesn't exist. I am needing help figuring out where to put the: Out-File -FilePath "C:\test\monitors.txt" command instead of using $LogFile = "C:\test\monitors.txt" Commented Jan 22, 2021 at 21:36

1 Answer 1

2

why not export to CSV?(:

$Monitors = Get-WmiObject WmiMonitorID -Namespace root\wmi
$LogFile = "C:\test\monitors.csv"

function Decode {
    if ($args[0] -is [System.Array]){
        [System.Text.Encoding]::ASCII.GetString($args[0])
    }
    Else {
        "Not Found"
    }
}

$Manufacturer= @()
$Name = @()
$Serial = @()

ForEach ($Monitor in $Monitors) {
    $Manufacturer += Decode $Monitor.ManufacturerName -nomatch 0
    $Name += Decode $Monitor.UserFriendlyName -nomatch 0 
    $Serial += Decode $Monitor.SerialNumberID -nomatch 0 
}

$obj = for($i=0; $i -lt $Monitors.Count;$i++){
    [pscustomobject]@{
        Manufacturer = $($Manufacturer[$i])
        Name = $($Name[$i])
        Serial = $($Serial[$i])
    } 
} 
$obj
$obj | Export-Csv -Path $LogFile -NoTypeInformation
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.