Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 130
  • Last Modified:

Powershell GWMI RPC help

Hi

We use the following code below supplied by Qlemo to search a list of PC's to see who is logged on using RPC and then output to a .csv

#Create Blade Output
gwmi -computer (gc c:\blades\blades.txt) Win32_ComputerSystem |
  select @{n='Blade'; e={$_.Name}},
             @{n='User'; e={ if ($_.UserName) { $_.Username } else { "No user" }} } |
             
 Export-CSV c:\blades\output.csv

Open in new window


Evening works great until a PC in the list is switched off

When using the code via Task Scheduler we see that the output file is not being created when a PC has been switched off by a User.

Is there away to deal with the below issue by adding the "PC name" and "Switched off" to the output file but also clearing the message so that it continues to check all other PC's in the list.


gwmi : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\xampp\htdocs\blades\DE_Blades.ps1:71 char:1
+ gwmi -computer (gc c:\xampp\htdocs\blades\dk\dk_blades_list.txt) Win32_ComputerS ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Thank you in advance

Si
0
Si Walton
Asked:
Si Walton
1 Solution
 
oBdACommented:
Get-Content C:\blades\blades.txt | % {
	$Blade = $_
	Try {
		Get-WmiObject -Computer $Blade Win32_ComputerSystem -ErrorAction Stop | Select-Object -Property `
			@{n='Blade'; e={$Blade}},
			@{n='User'; e={If ($_.UserName) {$_.Username} Else {"No user"}}},
			@{n='Exception'; e={}}
	} Catch {
		$_.Exception | Select-Object -Property `
			@{n='Blade'; e={$Blade}},
			@{n='User'; e={"n/a"}},
			@{n='Exception'; e={$_.Message}}
	}
} | Export-CSV c:\blades\output.csv

Open in new window

0
 
Si WaltonAuthor Commented:
Thank you oBdA

Works as you said :)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now