Jump to content

Recommended Posts

Posted (edited)

Hi All,

Firstly; Not sure if this is the correct place for this so feel free to move if not.

 

Secondly; I am, as the script will no doubt prove, an absolute novice with Powershell & have been cobbling this together over a few weeks when I have spare time from half baked ideas & google-fu.

 

Right, with that out of the way. The problem.

 

I have been writing a script to show me who is logged on to any computer in our domain, either individually or by AD group. What I have so far works if all computers are powered on & connected, however If one is offline, it throws an error & cuts out.

 

In the version of the script below, I am using validatescript to check a computer is online but would ideally like to skip a computer if it's offline rather than just throw the error.

 

Any pointers would be great.

 

Cheers,

AJ

 

function Get-LoggedOnUser
   {
       [CmdletBinding()]
       param
       (
           [Parameter()]
           [ValidateScript({If(Test-Connection -ComputerName $_ -Quiet -Count 1) {$true} else{throw "$_ offline"}})]    #If no response from computer, throw error message 'Failed'
           [ValidateNotNullOrEmpty()]
           [string[]]$ComputerName = $env:ComputerName
       )
       foreach ($comp in $ComputerName)
       {
           $output = @{ 'ComputerName' = $comp}
           $output.UserName = (Get-WmiObject -Class win32_computersystem -ComputerName $comp).UserName
           [PSCustomObject]$output
       }
   }


$Choice = Read-Host -Prompt 'To query an Active Directory Group, Enter 1. To query individual machines, Enter 2'
If ($Choice -eq '1')
   {
   $ADGroup = Read-Host "Which AD Group would you like to query?"                                                #output text to console
   $ADCOMPNAMES = Get-ADGroupMember -Identity $ADGroup | select name                                             #Gets members of specified AD group. Selects only name
   $ADCOMPNAMES = $ADCOMPNAMES -replace '@{name=',''                                                             #remove preceeding characters from variable
   $ADCOMPNAMES = $ADCOMPNAMES -replace '}',''                                                                   #remove traling brace from variable
   Get-LoggedOnUser -ComputerName $ADCOMPNAMES
   }
If ($Choice -eq '2')
   {    
   $CompNames = Read-Host -Prompt 'Input Computer Names here separated by commas'
   $CompNames = $CompNames -replace ',', ' '
   Get-LoggedOnUser -ComputerName $CompNames
   }
Else 
   {
   Write-Host "Done"
   }

Edited by Darksoth
Posted (edited)

Is it the line with

[color=#333333]#If no response from computer, throw error message 'Failed'[/color]

that is causing it to stop?

 

If you don't want it to throw an error, do something else instead. Maybe change

 

[ValidateScript({If(Test-Connection -ComputerName $_ -Quiet -Count 1) {$true} else{throw "$_ offline"}})]    #If no response from computer, throw error message 'Failed'

to

[ValidateScript({If(Test-Connection -ComputerName $_ -Quiet -Count 1) {$true} else{write-host ("$_ offline")}})]    #If no response from computer, throw error message 'Failed'

 

So instead of throwing the error

throw "$_ offline"

you are just writing an error message to the output instead

write-host ("$_ offline")

 

There are probably better ways to do this. You may still want to throw an error but use TRY and CATCH to handle it instead of just letting it terminate your script.

Edited by David44
  • Thanks 1
Posted
Is it the line with
[color=#333333]#If no response from computer, throw error message 'Failed'[/color]

that is causing it to stop?

 

If you don't want it to throw an error, do something else instead. Maybe change

 

Thanks David, I'll give this a go & see what we get!:)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...