locked
Variable $computername that can be defined through 2 different options? RRS feed

  • Question

  • Hi,

    I got this function which checks for a remote program on a computer (get-program).
    Then I also have a function which checks to which pc a user was last logged on to (get-LastLoggedonPCName).

    I' d like to combine both function so that I can execute

    => get-program -computername OR get-program -username

    So in short, the variables should be something like
    $comutername,
    $computername = Get-LastLoggedOnPCName $user

    in that way that both
    get-program mypc as well as
    get-program myuser can be used

    Please advise.
    J.


    Jan Hoedt

    Monday, October 5, 2015 10:13 AM

Answers

  • Sorry but I don't think anyone can decode what you are asking.

    A function cannot detect a user or computer. In AD a computer and a user can have the same name. They cannot have the same samaccountname. YOu could poll AD by samaccountaname and get the class of the object.

    An easier method is a parameter name.

    Get-Program -user joe
    Get-Program -computer PC002


    \_(ツ)_/

    • Marked as answer by janhoedt Monday, October 5, 2015 1:23 PM
    Monday, October 5, 2015 11:13 AM
  • I was making it way to complicated, this did the job:

    	[Parameter(Position=0)]
            [string[]]$User,
            [Parameter(Position=1)]
            [string[]]$Computers = (Get-LoggedOnPcname $User),
    


    Jan Hoedt

    • Marked as answer by janhoedt Monday, October 5, 2015 1:23 PM
    Monday, October 5, 2015 1:23 PM

All replies

  • Just use the computer name in the next command. Why is that a problem?

    Are you asking for someone to write a function that can tell a user from a computer? Why?


    \_(ツ)_/


    • Edited by jrv Monday, October 5, 2015 10:18 AM
    Monday, October 5, 2015 10:17 AM
  • I'm not sure what you mean. Probably my explanation wasn't clear.
    The

    get-program mycomputer
    get-program myuser

    should both list all programs on my computer. I'd like to have both options in 1 command.

    This is the current config

     [Parameter(Mandatory, Position=0)]
            [string[]]$ComputerName,
            [Parameter(Position=1)]
            [string[]]$Property = ('DisplayVersion','UninstallString'),
            [Parameter(Position=2)]
            [string[]]$ProgramName = "*"

    Where to add the $computername = Get-LastLoggedOnPCName $user?
    Can't add the variable again since it will be a duplicate.

    J.


    Jan Hoedt

    Monday, October 5, 2015 11:03 AM
  • Sorry but I don't think anyone can decode what you are asking.

    A function cannot detect a user or computer. In AD a computer and a user can have the same name. They cannot have the same samaccountname. YOu could poll AD by samaccountaname and get the class of the object.

    An easier method is a parameter name.

    Get-Program -user joe
    Get-Program -computer PC002


    \_(ツ)_/

    • Marked as answer by janhoedt Monday, October 5, 2015 1:23 PM
    Monday, October 5, 2015 11:13 AM
  • Sorry for that. In my head it's all pretty clear :-).
    Get-Program -user joe
    Get-Program -computer PC002

    is exactly what I'm looking for.


    Jan Hoedt

    Monday, October 5, 2015 11:28 AM
  • Sometimes I guess right.


    \_(ツ)_/

    Monday, October 5, 2015 11:32 AM
  • Thanks but that was actually my initial question. Next time I'll try to formulate it clearer.
    The rest of my script uses the variable $computer, so maybe I should use an if construction?

     [Parameter(Position=0)]
            [string[]]$ComputerName,
            [Parameter(Position=1)]
            [string[]]$User = "Get-LoggedOnPCName $user",
            [Parameter(Position=2)]
            [string[]]$Property = ('DisplayVersion','UninstallString'),
            [Parameter(Position=3)]
            [string[]]$ProgramName = "*"

    ([Validateset('User','Computer'')]
     [Parameter(Position=2,Mandatory=$True)][String]$Type

    The rest of my script:
    if $type = user
    foreach ($user in $Users) {

    .. if $type = computer
    foreach ($computer in $computers) {


    Jan Hoedt

    Monday, October 5, 2015 11:47 AM
  • Or I could set $computername to $computername = $username of course.

    Jan Hoedt

    Monday, October 5, 2015 11:53 AM
  • I was making it way to complicated, this did the job:

    	[Parameter(Position=0)]
            [string[]]$User,
            [Parameter(Position=1)]
            [string[]]$Computers = (Get-LoggedOnPcname $User),
    


    Jan Hoedt

    • Marked as answer by janhoedt Monday, October 5, 2015 1:23 PM
    Monday, October 5, 2015 1:23 PM
  • Note:

    get-program myuser works fine
    get-program mycomputer works fine

    get-program myuser programname
    does not if I don't enter the -programname
    f.e. get-program myuser *office* does not work, get-program myuser -programname *office* does work

    I know I have to play with the positions but computername and user have already first and second and can't put both on 0.
    Any idea how I could make that work?


    Jan Hoedt

    Monday, October 5, 2015 1:43 PM
  • We use parameter sets for that.  This is one on one thousand reasons why I repeat that anyone seriously considering PowerShell use professionally must either get a book or take a formal course.

    search for how to use parameter sets or

     help parameters


    \_(ツ)_/

    Monday, October 5, 2015 2:04 PM
  • As I mentioned, I know that it is in the positions part (of the parameter set) but I cannot set 2 parameters with the same position.

    This is a group in which questions can be asked by any level of expertise I thought. Have made note of your remark a dozen times and the course is on my wishlist. Several answers in this group learned me a lot and pointed me in the right direction. Although I do appreciate your answers, don't feel obliged to answer.


    Jan Hoedt

    Monday, October 5, 2015 2:28 PM
  • As has been noted numerous times: Asking incremental questions in a forum is an extremely inefficient way to learn any kind of programming. For best results, ask one single, very specific question, and then tell also what you were expecting to happen vs. what actually happened.


    -- Bill Stewart [Bill_Stewart]

    Monday, October 5, 2015 3:04 PM
  • Thanks for your input, I'll keep that in mind.

    Jan Hoedt

    Monday, October 5, 2015 3:18 PM