by jmp8600 at 2013-03-21 18:48:39
Hello There,by mikefrobbins at 2013-03-21 20:08:49
in the book Learn Windows PowerShell In the month of lunches. on Page 250. Don showed us example to capture machine names and exceptions in two different files.
below is the example
function Get-Stuff {
BEGIN {
del c:\retry.txt -ea SilentlyContinue
del c:\errors.txt -ea SilentlyContinue
}
PROCESS {
Try {
Get-WmiObject Win32_BIOS -comp $_ -ea Stop -ev WmiError
} Catch {
$_ | Out-File c:\retry.txt -append
$WmiError | Out-File c:\errors.txt -append
}
}
}
‘Server-R2’,‘Notonline’,‘Localhost’ | Get-Stuff
The issue is, i do not see server names in retry.txt. I see below in retry.txt
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\Users\administrator\Documents\Ch22_ErrVariable.ps1:8 char:14
+ Get-WmiObject <<<< Win32_BIOS -comp $_ -ea Stop -ev WmiError
+ CategoryInfo : InvalidOperation: ([Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
The errors.txt has below
Command execution stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: The RPC server i
s unavailable. (Exception from HRESULT: 0x800706BA)
I tried write-host $_ and $WiError in Catch but it does not show machine name it shows the whole error i paste above. I am not sure which variable will contain machine name.
Do you have machines with the following names on your network?by jmp8600 at 2013-03-22 10:35:23
Server-R2
Notonline
Localhost
I receive the same error with those names since they don’t exist on my network.
The command runs successfully when I target my local machine. I changed the last line to the following:"$env:computername", 'localhost', '.' | Get-Stuff
If you’ve changed the names to remote machines on you network and you’re receiving that error, it’s probably due to the firewall on the remote machine blocking it.
i have used two machine names which i have in my network and one which i did not. I have copy/paste example from book because as per the book retry.txt should contain machine names which are failing but instead i am seeing the whole error. How co capture a failed machine name in retry.txt?by mikefrobbins at 2013-03-22 14:50:18
I see what you mean. Looks like it’s an error in the book due to the scope of the current object variable (the current object is different in the try block versus the catch block). I’ve modified the function to accomplish what it’s suppose to:by jmp8600 at 2013-03-22 15:23:23function Get-Stuff {
[CmdletBinding()]
param(
[Parameter(Mandatory=$True,
ValueFromPipeline=$True)]
[string]$ComputerName
)
BEGIN {
del c:\retry.txt -ea SilentlyContinue
del c:\errors.txt -ea SilentlyContinue
}
PROCESS {
foreach ($computer in $computername) {
Try {
Get-WmiObject Win32_BIOS -comp $computer -ea Stop -ev WmiError
} Catch {
$computer | Out-File c:\retry.txt -append
$WmiError | Out-File c:\errors.txt -append
}
}
}
}
'Server-R2','Notonline','Localhost' | Get-Stuff
Thanks You.