Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 554 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check if a command has run successfully

#1
I've tried enclosing the following in an if statement so I can execute another command if this succeeds:

Get-WmiObject -Class Win32_Share -ComputerName $Server.name -Credential $credentials -Filter "Description='Default share'" | Foreach-Object {
$Localdrives += $_.Path

but I can't figure out how to do it. I even tried creating a function but I couldn't figure out how to check if the function had successfully completed either.
Reply

#2
you can try :

$res = get-WmiObject -Class Win32_Share -Filter "Description='Default share'"
if ($res -ne $null)
{
foreach ($drv in $res)
{
$Localdrives += $drv.Path
}
}
else
{
# your error
}
Reply

#3
There are instances where any of the options are best suitable. Here is another method:

try {
Add-AzureADGroupMember -ObjectId XXXXXXXXXXXXXXXXXXX -RefObjectId (Get-AzureADUser -ObjectID "XXXXXXXXXXXXXX").ObjectId -ErrorAction Stop
Write-Host "Added successfully" -ForegroundColor Green
$Count = $Null
$Count = 1
}
catch {
$Count = $Null
$Count = 0
Write-Host "Failed to add: $($error[0])" -ForegroundColor Red
}

With try and catch, you don't only get the error message returned when it fails, you also have the $count variable assigned the number 0. When the command is successful, your $count value returns 1. At this point, you use this variable value to determine what happens next.
Reply

#4
You can use “$error” automatic variable. This will check if the first command throws error or if it completes successfully.

“execute first command”
if($error.count -eq 0)
{
“execute second command”
}


if you want to clear the errors afterwards you can use: `$error.clear()`. This will set the counter of errors to 0.
Reply

#5
Or if a failure returns no standard output, that would work for an if statement (*assuming an exception doesn't terminate the pipeline*):

```
if (! (Get-CimInstance Win32_Share -ComputerName $Server.name -Credential $credentials -Filter "Description='Default share'")) {
'command failed'
}
```

Also there's now the or symbol "||" in powershell 7 (works with terminating exceptions):

```
Get-CimInstance Win32_Share -ComputerName $Server.name -Credential $credentials -Filter "Description='Default share'" || 'command failed'
```
Reply

#6
Try the `$?` automatic variable:

$share = Get-WmiObject -Class Win32_Share -ComputerName $Server.name -Credential $credentials -Filter "Description='Default share'"

if($?)
{
"command succeeded"
$share | Foreach-Object {...}
}
else
{
"command failed"
}



From [`about_Automatic_Variables`](

[To see links please register here]

):

$?
Contains the execution status of the last command.
It contains True if the last command succeeded and False if it failed.
...

$LastExitCode
Contains the exit code of the last native program or PowerShell script that ran.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through