Hey guys,<\/p>\n
Ive been working on a little something to check if laptops are being charged at night using the get-cimInstance cmdlet to get the battery status whether it’s discharging the battery or on AC power.<\/p>\n
This is what I put together and in the curly brackets gets exactly what I need and writes it to the csv:<\/p>\n
$computers = import-csv ‘filepath’<\/p>\n
foreach ($computername in $computers)<\/p>\n
{get-ciminstance win32_battery -computername $computers -Property Description, BatteryStatus, SystemName |Select systemname, description, batterystatus | Export-CSV ‘filepath’}<\/p>\n
I’m still relatively new to Powershell and may be delving too deep too quick, still im curious. Now, i’m trying to import computer names from a CSV, and thats where I hit my roadblock. I’m not asking for someone to write this for me, but some ideas/info I can play with to get this to work.<\/p>","upvoteCount":4,"answerCount":7,"datePublished":"2019-02-04T22:24:01.000Z","author":{"@type":"Person","name":"itf0r3all","url":"https://community.spiceworks.com/u/itf0r3all"},"acceptedAnswer":{"@type":"Answer","text":"
you must have a header in csv as computername<\/p>\n
$computers = import-csv c:\\computers.csv\nForEach ($computer in $computers){\ngwmi win32_battery -ComputerName $computer|Select @{n='ComputerName';e={$computer}}, @{n='BatteryType';e={$_.description}},@{N= \"BatteryStatus\";Expression = {if ($_.batterystatus -eq '2') {\"Charging\"} Elseif ($_.batterystatus -eq '1') {\"NotCharging\"}}}|\nExport-CSV 'filepath'\n}\n<\/code><\/pre>\nCiminstance requires Winrm enabled so used WMI<\/p>\n
If there is no header in CSV use get-content instead of import-CSV<\/p>","upvoteCount":1,"datePublished":"2019-02-05T06:39:46.000Z","url":"https://community.spiceworks.com/t/get-ciminstance-on-remote-computers-from-csv/695951/4","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},"suggestedAnswer":[{"@type":"Answer","text":"
Hey guys,<\/p>\n
Ive been working on a little something to check if laptops are being charged at night using the get-cimInstance cmdlet to get the battery status whether it’s discharging the battery or on AC power.<\/p>\n
This is what I put together and in the curly brackets gets exactly what I need and writes it to the csv:<\/p>\n
$computers = import-csv ‘filepath’<\/p>\n
foreach ($computername in $computers)<\/p>\n
{get-ciminstance win32_battery -computername $computers -Property Description, BatteryStatus, SystemName |Select systemname, description, batterystatus | Export-CSV ‘filepath’}<\/p>\n
I’m still relatively new to Powershell and may be delving too deep too quick, still im curious. Now, i’m trying to import computer names from a CSV, and thats where I hit my roadblock. I’m not asking for someone to write this for me, but some ideas/info I can play with to get this to work.<\/p>","upvoteCount":4,"datePublished":"2019-02-04T22:24:01.000Z","url":"https://community.spiceworks.com/t/get-ciminstance-on-remote-computers-from-csv/695951/1","author":{"@type":"Person","name":"itf0r3all","url":"https://community.spiceworks.com/u/itf0r3all"}},{"@type":"Answer","text":"
To start with why not pull directly from AD instead of a text file. No maintenance, just point at the proper OU(s). I believe you would just use Get-ADComputer for this.<\/p>","upvoteCount":0,"datePublished":"2019-02-05T00:49:59.000Z","url":"https://community.spiceworks.com/t/get-ciminstance-on-remote-computers-from-csv/695951/2","author":{"@type":"Person","name":"jcLAMBERT","url":"https://community.spiceworks.com/u/jcLAMBERT"}},{"@type":"Answer","text":"
If you import-csv, you access the data via column name. So if you have one column named machines in machines.csv:<\/p>\n
$machines = import.csv “\\path\\machines.csv”
\n$computers = $computers.machines<\/p>\n
<\/a>Now, let’s fix your loop error:<\/h1>\nforeach ($computer in $computers) {
\nget-ciminstance win32_battery -computername $computer …
\n}<\/p>\n
when you created $computers, you instantiated an array.
\ninside your loop, you use the first variable which you’re creating in the iteration of your array.<\/p>","upvoteCount":0,"datePublished":"2019-02-05T02:09:24.000Z","url":"https://community.spiceworks.com/t/get-ciminstance-on-remote-computers-from-csv/695951/3","author":{"@type":"Person","name":"gary-m-g","url":"https://community.spiceworks.com/u/gary-m-g"}},{"@type":"Answer","text":"
Hello and welcome to the PowerShell forum.<\/p>\n
When posting code here in this form, PLEASE use the </> tool bar tool to enter the code. It makes it easier to view and that means probably better answers.<\/p>","upvoteCount":0,"datePublished":"2019-02-05T09:08:34.000Z","url":"https://community.spiceworks.com/t/get-ciminstance-on-remote-computers-from-csv/695951/5","author":{"@type":"Person","name":"DoctorDNS","url":"https://community.spiceworks.com/u/DoctorDNS"}},{"@type":"Answer","text":"
We do intend on using a separate OU for these computers we want to track so that would be a pretty good idea.<\/p>","upvoteCount":0,"datePublished":"2019-02-05T14:23:27.000Z","url":"https://community.spiceworks.com/t/get-ciminstance-on-remote-computers-from-csv/695951/6","author":{"@type":"Person","name":"itf0r3all","url":"https://community.spiceworks.com/u/itf0r3all"}},{"@type":"Answer","text":"