Darksoth Posted March 19, 2018 Posted March 19, 2018 (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 March 19, 2018 by Darksoth
David44 Posted March 19, 2018 Posted March 19, 2018 (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 March 19, 2018 by David44 1
Darksoth Posted March 20, 2018 Author Posted March 20, 2018 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!
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now