How to confirm that an action has been completed

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Barry Thomson Barry Thomson 3 years, 8 months ago.

  • Author
    Posts
  • #10553
    Profile photo of Barry Thomson
    Barry Thomson
    Participant

    Hi,

    I've written a small script to delete machine that are supplied by a text file.

    All seems to work okay in a test environment, but I don't know how to give any conformation of what's been done.

    What I'd like this to do is, output some sort of "successful deleted MACHINE1" message.

    Could someone point us in the right direction?

    Cheers

    function delete-computers
    {

    Add-PSSnapin Quest.ActiveRoles.ADManagement

    $inputfile = Read-Host "Enter input file"
    $computers = Get-Content $inputfile

    foreach ($computer in $computers) {

    Get-QADComputer -Name $computer | Remove-QADObject -Force

    }
    }

    delete-computers

  • #10554
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    There are several variations of how to do this, but they all boil down to checking whether the Get-QADComputer or Remove-QADObject commands produced any errors. When processing one object at a time like this, I prefer to use try/catch blocks in conjunction with -ErrorAction Stop:

    function delete-computers
    {
    
        Add-PSSnapin Quest.ActiveRoles.ADManagement
    
        $inputfile = Read-Host “Enter input file”
        $computers = Get-Content $inputfile
    
        foreach ($computer in $computers)
        {
            try
            {
                Get-QADComputer -Name $computer -ErrorAction Stop | Remove-QADObject -Force -ErrorAction Stop
                Write-Host "Successfully deleted $computer."
            }
            catch
            {
                Write-Error -ErrorRecord $_
            }
        }
    }
    
  • #10555
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Your function would be better named "Remove-Computer," as "Delete" is not a standard PowerShell verb, and the standard is to use singular nouns. Also, it's more usual to let the shell prompt for input as part of a parameter, than to manually use Read-Host.

    function Remove-Computer {
      [CmdletBinding()]
      param(
        [Parameter(Mandatory=$True)][string]$InputFile
      )
      Add-PSSnapin Quest.ActiveRoles.ADManagement
      $computers = Get-Content $inputfile
      foreach ($computer in $computers) {
        Write-Verbose "Removing $computer"
        Get-QADComputer -Name $computer | Remove-QADObject -Force
      }
    }
    

    If you run your function using the -Verbose parameter, you'll see the "Remove " output. Using Write-Verbose is the standardized way of displaying "status" messages from within a script; those messages are suppressed by default, but shown when you add -Verbose.

    By declaring $InputFile as a parameter, you can specify it when you run the command: "Remove-Computer -InputFile whatever.txt". But, if you forget, the shell will prompt you automatically. It's generally preferred to let that happen, so that all input comes in the form of a parameter value, rather than prompting for input within the function. When you use Read-Host, you can't ever automate the use of your function, because that interactive prompt will always be there. When you use a parameter, you have the option of doing it either way – providing it in advance, or being prompted. More flexible.

  • #10556
    Profile photo of Barry Thomson
    Barry Thomson
    Participant

    Don, Dave, thank you very much.

You must be logged in to reply to this topic.