Issue manipulating parameter - SOLVED

0

I am new to powershell, and with the help of google and trial and error i have come to below code.

This function is rather simple it should receive a list of workstation names and then return the asked results.

The problem lies is that on the domain i am working ping commands to workstation names do not work, they do work if i add ".domain.com" to the workstation.

Now i could do this manually when i enter the parameter to run, but it wouldn't be much of a well written script if you have to do repeated manual labor.

The results from this script will then be used as input for different other scripts.

I have tried:

On the first actual line after the foreach I changed

ComputerName = $Comp to = ($Comp + ".domain.com")

This resulted in one of my output lines to show this .domain.com but the Test-Connection still fails.

function Get-LoggedOnUser
{
    [OutputType([pscustomobject])]
    [CmdletBinding()]
    param
    (
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string[]]$ComputerName = $env:COMPUTERNAME
    )
    begin
    {
        $ErrorActionPreference = 'Stop'
    }
    process
    {
        try
    {
            foreach ($Comp in $ComputerName)
            {
                $Output = @{ 
                    ComputerName = $Comp
                    Domain= 'Unknown' 
                    UserName = 'Unknown'
                    ComputerStatus = 'Offline'
                }
                if (Test-Connection -ComputerName $Comp -Count 1 -Quiet) {
                    $Output.Domain = (Get-WmiObject -Class win32_computersystem -ComputerName $Comp).UserName.split('\')[0]
                    $Output.UserName = (Get-WmiObject -Class win32_computersystem -ComputerName $Comp).UserName.split('\')[1]
                    $Output.ComputerStatus = 'Online'
                }
                Format-List -InputObject $Output
            }
        }
        catch
        {
             $PSCmdlet.ThrowTerminatingError($_)
        }
    }
}

The expected results would be :

Username

Workstation.domain.com (optionally the .domain.com should be removed again for the output, but i would be happy already if it just gives me the expected results)

Domain

Computer status

current results are

username: unknown
workstation: as inputted
domain: unknown
computer status: offline*

These results are standard when the test connection fails.

EDIT: with below current code the Test-Connection is now behaving as expected

                    Foreach ($Comp in $ComputerName)
                    {
                              $Output = @{
                              ComputerName = $Comp
                              Domain= 'Unknown' 
                              UserName = 'Unknown'
                              ComputerStatus = 'Offline'
                           }
                    $CompTest = ("$Comp" + ".domain.com")
                    echo $CompTest

                    <#Test-Connection -ComputerName $Comp -count 1#>
                    <#Test-Connection -ComputerName $CompTest -count 1#>                

                if (Test-Connection -ComputerName $CompTest -Count 1 -Quiet)

                    {
                    $Output.Domain = (Get-WmiObject -Class win32_computersystem -ComputerName $Comp).UserName.split('\')[0]
                                  $Output.UserName = (Get-WmiObject -Class win32_computersystem -ComputerName $Comp).UserName.split('\')[1]
                                  $Output.ComputerStatus = 'Online'
                               }

however when testing with a workstation that is offline now instead of showing the standard output declared in the initial part of the foreach it is now throwing the error:

Get-LoggedOnUser : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

InvalidOperation: (:) [Get-loggedonUser], COMException

EDIT2: seems the workstation i was testing and that was throwing the RPC server is unavailable had some local setting blocking the ping requests.

my question here has hereby been answered thank you all for the help, updating title to 'solved'

powershell
asked on Stack Overflow Jan 31, 2019 by krieksken • edited Feb 1, 2019 by krieksken

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0