1

Hi i made a script that checks disk space and transfers it on to a log and i want to put the error that pops on the prompt inside the log heres my code:

$equipos = "D:\SCRIPTSM\AAAA\equipos.txt"
$listaEquipos = Get-Content $equipos
$fecha = (Get-Date).ToString('dd.MM.yyyy')
$espaciotxt = foreach ($equipo in $listaEquipos){
    Get-WmiObject -Class Win32_LogicalDisk -ComputerName $equipo -Filter "drivetype=3" | Sort-Object -Property DeviceID | Format-Table -Property DeviceID,
    @{name='EspacioLIBRE(MB)';expression={$_.Freespace / 1MB -as [int]}},
    @{name='TamañoDISCO(GB)';expression={$_.Size / 1GB -as [int]}},
    @{name='%Libre';expression={$_.FreeSpace / $_.Size * 100 -as [int]}}
    Write-Output "Ip del equipo : $equipo fecha del test: $fecha"
    }
$espaciotxt | Out-File C:\Users\AAAAA\Desktop\PRU.txt

Heres the exit log and where do i want to place the error prompt inside of it.

Error prompt: Get-WmiObject : RPC server is not available...

log img disk

AALV8
  • 35
  • 5
  • 1
    As an aside: The CIM cmdlets (e.g., `Get-CimInstance`) superseded the WMI cmdlets (e.g., `Get-WmiObject`) in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell (Core) v6+, where all future effort will go, doesn't even _have_ them anymore. Note that WMI still _underlies_ the CIM cmdlets, however. For more information, see [this answer](https://stackoverflow.com/a/54508009/45375). – mklement0 Aug 30 '23 at 14:36

1 Answers1

2

I've added a Try-Catch loop & set the error preference to "Stop" on the Get-WMIObject command.

Not sure if you want "Error Prompt:" or your own custom string, so I've added both to the catch statement.

$Error[0] is the last Error in the Powershell session, which will be what triggered the catch statement.

$equipos = "D:\SCRIPTSM\AAAA\equipos.txt"
$listaEquipos = Get-Content $equipos
$fecha = (Get-Date).ToString('dd.MM.yyyy')
$espaciotxt = foreach ($equipo in $listaEquipos){
    Try{
        Get-WmiObject -Class Win32_LogicalDisk -ComputerName $equipo -Filter "drivetype=3" -ErrorAction Stop | Sort-Object -Property DeviceID | Format-Table -Property DeviceID,
        @{name='EspacioLIBRE(MB)';expression={$_.Freespace / 1MB -as [int]}},
        @{name='TamañoDISCO(GB)';expression={$_.Size / 1GB -as [int]}},
        @{name='%Libre';expression={$_.FreeSpace / $_.Size * 100 -as [int]}}
        Write-Output "Ip del equipo : $equipo fecha del test: $fecha"
    }
    catch{
        "Error prompt: "+[string]$Error[0].Exception.Message
        $Error[0].Exception.Message
        Write-Output "Ip del equipo : $equipo fecha del test: $fecha"
    }
}
$espaciotxt | Out-File C:\Users\AAAAA\Desktop\PRU.txt
  • 2
    Nice. Note that it's simpler (and more robust) to refer to the error at hand via `$_` rather than via `$Error[0]` in the `catch` block. (Also: `$Error[0].Exception.Message` already is a string - no need for the `[string]` cast; more broadly, the fact that the LHS of the `+` operation is a `[string]` alone would coerce even non-string LHS operands to strings). – mklement0 Aug 30 '23 at 14:36