A PowerShell Carol in which Ebenezer Script Learns WMI

ScriptingGuy1

   Summary: A Windows PowerShell Carol in which Ebenezer Script learns how easy it to use WMI to check disk space usage.   Hey, Scripting Guy! QuestionHey, Scripting Guy! I am wondering if Ebenezer Script will learn how easy it is to work with WMI from inside Windows PowerShell. — CS   Hey, Scripting Guy! AnswerHello CS, Microsoft Scripting Guy Ed Wilson here. It is almost the end of the year and we have decided to devote some posts to the holiday season. We even have guest bloggers from around the world to share some holiday spirit. This week we have Sean Kearney. You can learn more about Sean in yesterday’s blog introduction. Be sure to check out Sean’s theme song for this week’s PowerShell Carol posts.   And so it was on that cold winter evening, Ebenezer Script headed home to his hovel whilst hauling his old 286 with CGA monitor in a rusty Red Flyer wagon to his house. We did mention Ebenezer Script was not fond of anything new. Even a suitcase computer was sickening to his eyes. He trotted along up the stairs of his abode. He stared at the doorknocker. It shimmered. His eyes blinked as he reached for the doorknocker and it took on the shape of his former assistant Jacob Clippy. Big round eyes blinked away at him.

“Don’t run with scissors,” he cheerfully suggested and shimmered away into nothingness. Old Ebenezer shook his head. Was he losing his mind? He opened the door up with the old ratty skeleton key, stepping into the hall. Staring at his floor littered with CP/M 2.2 8” floppies. He let out a sigh shaking his head. He grabbed a bottle from the shelf and poured himself a strong stiff drink for this warm night, a hot can of soda pop. (*BLEeAaaaCCccchHhHH!!*) Ebenezer’s body responded cheerfully to his always excellent choice in pre-nighttime drinks. The boiled syrup reacted equally well with his lunch of “Mr. Noodles.” (*Urrrrrrggggllllll*) His old tummy rumbled. Ebenezer’s habits came from a long time ago when he was once happy. Yes, there was a day when old Ebenezer Script was the best of the best, and could out manage anybody with VBScript and run circles with Perl. He was the king with his old Office Assistant and sometimes business partner, the ever-irritating Jacob Clippy. Happy were they coding and scripting, scripting and coding; and sometimes dancing, especially with the pretty data entry clerks. But, he was always happy. Nevertheless, those days were gone. Off to bed Ebenezer went. The thought about “Whatever happe….” quickly floated away. He was a miserable grouchy, grimy grump. Nothing could ever be perceived to change that. “Bah! Codebug!” Ebenezer turned on his old computer and fired up his script to check free drive space. He looked at once to remind himself about the hard work he had put in to create this work of art.

Dim Result

 

Set ConnectoWMI = GetObject(“winmgmts:\localhostrootcimv2”)    

Set Disks = objWMIService.ExecQuery(“Select * from Win32_LogicalDisk Where DriveType = 3”)

For each Drive in Disks

      Result = “Drive ” & Drive.DeviceID & ” – Free Space ” & Drive.FreeSpace

      wscript.echo Result

Next   He ran the script and proudly noted plenty of free space on his old computer. “Good thing I ran DriveSpace on it,” he chortled to himself. “Let’s see Windows PowerShell do anything like that,” cackled old Ebenezer sitting back on his haunches. He reached into his pocket to pull out his keys to lock the computer when he found a scrap of paper. Glancing down, he realized that it was a piece of paper he had grabbed off Bob Snipit’s desk to write down a telephone number. He turned it over and noted it was a script Bob had written. Free Drive Space in Windows PowerShell – One liner, it was titled.

GET-WMIOBJECT win32_logicaldisk | where { $_.DriveType -eq 3} | format-list   “BAH! There is no way it is that easy. What a fool Bob is! Codebug! Baaaaaaah!” It was at that time that a foul breeze blew out of the power supply of his old 286. Capacitors popped and the room filled with smoke. Coughing and hacking, Ebenezer looked up to put out the smoke only to see a shape. “AIGHAIHGHAGHH!!!!” Ebenezer Script leapt out of his old socks, “It can’t be you!” Sure enough, it was his old assistant / partner / iconic personality, Jacob Clippy. Thin as always with that same slick smug smile.

“Well Ebenezer,” the giant Bent Blinking Behemoth looked on, “have you missed me?” *KLUNK!*, the old Admin dropped dead in a faint. Will Ebenezer recover from his fainting spell? Is Jacob Clippy compatible with Windows PowerShell? Will he irritate the readers endlessly with inane suggestions? Will we get hot hot chocolate after reading this? This and many more questions (some pointlessly odd) answered next time!   CS, that is all there is to using Windows PowerShell and WMI. Holiday guest blogger week will continue tomorrow when Sean will continue “A Windows PowerShell Carol.” I invite you to follow me on Twitter or Facebook. If you have any questions, send email to me at scripter@microsoft.com or post them on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.   Ed Wilson, Microsoft Scripting Guy 

0 comments

Discussion is closed.

Feedback usabilla icon