Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
429 views
in Technique[技术] by (71.8m points)

Need to output the results of a PowerShell script

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 rootwmi
$LogFile = "C:estmonitors.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
}
question from:https://stackoverflow.com/questions/65852834/need-to-output-the-results-of-a-powershell-script

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

why not export to CSV?(:

$Monitors = Get-WmiObject WmiMonitorID -Namespace rootwmi
$LogFile = "C:estmonitors.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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...