1

I am trying to make a script that takes input of a hostname and tells me the current logged on user. It works when I run the command one by one on the powershell prompt (as an admin), but doesn't produce the expected output when I run the actual script itself.

Here is the relevant code:

$Row.Hostname = $Hostname
$getwmiobject = Get-WmiObject -class Win32_computersystem -computername $Hostname
$Username = $Getwmiobject.username

    if($UserName -eq $NULL) {
    $Row.Username = "No Current Logged on User"
    }
        else {$Row.Username = $Username
    }

$csvfile += $Row

$csvfile |sort-object Hostname | Export-Csv "foo.csv

When ran in the command line, I get the correct user. When ran as a script, I get "No Current logged on user". I have no clue why this is happening.

EDIT: I suppose it's important to mention that I have the output writing out to a file, made the changes in the code above.

  • 1
    Please provide full script, at least the `param()` part, and code around your sample. Is the sample identical to the script, or have you copy/pasted from mulitple places to give us a sample(may be a variable scope problem). At last, did you the script in a console with admin-rights? – Frode F. Feb 08 '13 at 21:03
  • 1
    Sorry, I understand more clearly now. This is an excerpt from the end of the script. The only thing I didn't include was how it got the $Hostname variable, but that really shouldn't matter – user2055535 Feb 08 '13 at 21:43
  • 1
    okey. Can you insert `Write-Host "Hostname is: $Hostname"; Write-Host "Hostname is of type: $($Hostname.gettype().name)` before the `get-wmiobject...` part and see if it outputs a valid hostname and "String" as type? :) – Frode F. Feb 08 '13 at 21:46
  • 1
    All of the hostnames that it pulls are being outputted as valid and as strings. I have a check that simply says `"No Hostname to lookup user"` if the `$hostnames` length is less than 1 as well. – user2055535 Feb 08 '13 at 21:51
  • 1
    what about the rights ? did you run the script as admin too ? – Frode F. Feb 08 '13 at 22:15
  • 1
    Yes, both the script and the line by line were run as admin – user2055535 Feb 08 '13 at 22:36
  • 1
    do you run the script from a terminal server session? In a terminal services session, UserName returns the name of the user that is logged on to the console—not the user logged on during the terminal service session. – Loïc MICHEL Feb 09 '13 at 07:21
  • 1
    Right, but the computer's queried are in fact logged onto the console. The script is ran in a terminal service session, but that doesn't explain why it works when done line-by-line rather than by the script. – user2055535 Feb 11 '13 at 16:27
  • 1
    So, apparently when I run the script, it comes back with `"Get-WmiObject : The RPC server is unavailable`. When run line by line, I do not get this issue and get the proper info back. – user2055535 Feb 11 '13 at 18:51
  • 1
    are you sure you are running a console session (mstsc.exe /admin) ? – Loïc MICHEL Feb 11 '13 at 19:53

1 Answers1

0

Check whatever input you have for the $Hostname variable for extra characters, such as a newline `n. When you type it out, it may not give you an error and work because it doesn't have to process things like that.

Ondaje
  • 74
  • 1
  • 2
  • 10
  • 1
    That was it. There was a `n at the end of each hostname so it could add a new one to the array. Took that line out and it worked. Thanks! – user2055535 Feb 11 '13 at 19:58