Will This PowerShell Script Produce Consistent Output on a Raspberry Pi?

0
0
Asked By VelvetMango42 On

I'm writing a PowerShell script that displays the current date, hostname, username, operating system, CPU information, total memory, and boot information. It uses Windows CIM queries when running on Windows and reads files or invokes Linux commands such as /proc/cpuinfo, free, and uptime on other systems. PowerShell is installed on the Raspberry Pi, but I can't access it right now to test the script. Will it run there, and will the output have the same format as it does on Windows?

4 Answers

Answered By QuietHarbor7 On

The overall approach should work, and the platform-specific branch is the right idea. The Windows CIM commands won’t provide useful results on Linux, so keeping them inside the Windows condition is important. On the Pi, make sure commands such as free and uptime are available through PATH; if needed, invoke native commands with the call operator. Also convert the uptime result to a string before calling Trim(), rather than relying on implicit conversion.

Answered By CopperLynx88 On

A few improvements would make this more reliable: call Get-Date once and format that value, then reuse it; likewise, query each CIM class only once and read the properties you need. For the Linux CPU lookup, matching Model name or Model more precisely is safer than matching any line containing Model. I’d also return a PSCustomObject with fields such as HostName, User, OS, CPU, RamMB, and LastBoot, then format it afterward. That keeps the data consistent and lets you use Format-Table or another output format later.

MistyCedar31 -

The built-in $IsWindows, $IsLinux, and $IsMacOS variables are available in modern PowerShell, but not in older Windows PowerShell. A condition such as if ($IsMacOS) {} elseif ($IsLinux) {} else {} is clearer if you need to support multiple versions.

Answered By AmberKite56 On

It should run on a Raspberry Pi with PowerShell installed, but identical-looking output is not automatic. The underlying values and native command output differ between Windows and Linux, especially memory and boot time, so you need to normalize them yourself. For example, parse the CPU model from /proc/cpuinfo, extract the memory value from free, and turn uptime into a DateTime. Then place the normalized values in one object and format that object consistently.

Answered By BlueOrchid19 On

The Linux parsing can be made fairly direct. Read the CPU model line from /proc/cpuinfo, split it at the colon, and trim the result. For memory, use a predictable form such as free --mega and select the memory row instead of storing the entire command output. For boot time, uptime -s is easier to convert into a date than the human-readable uptime display. The script can work on the Pi, but test it on the actual PowerShell version and operating system because native command output can vary slightly.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.