Custom Variable - Current Logged On User (for Dynamic Collections)

Comments

6 comments

  • Michael Muni

    %username% should give you the current user.

    0
    Comment actions Permalink
  • Craig Whetton

    $env:username if using PowerShell

    0
    Comment actions Permalink
  • Marcel Büttinghaus

    Sorry I think I described my problem completly wrong. I would like to create a "Dynamic collection" and use the current logged on user of the computer as a variable.

    The reason is that I want to create e.g. a Dynamic Collection that gives me all the computers where the logged on user is part of the local Administrators group.

    0
    Comment actions Permalink
  • Marcel Büttinghaus

    Sorry I think I described my problem completly wrong. I would like to create a "Dynamic collection" and use the current logged on user of the computer as a variable.

    The reason is that I want to create e.g. a Dynamic Collection that gives me all the computers where the logged on user is part of the local Administrators group.

    0
    Comment actions Permalink
  • Craig Whetton

    The best way I can think of doing this is having a login script GPO, that runs a PowerShell script. This PowerShell script checks the current user against the members of the local admins and then imports that data into a custom field for that PC in Inventory.

    If I get some time spare today, I'll have a play with PowerShell and see if I can get something working.

    0
    Comment actions Permalink
  • Craig Whetton

    This works. It's probably not the best way of doing this.

    $user = "$env:USERNAME";
    $group = "Administrators";
    $groupObj =[ADSI]"WinNT://./$group,group" 
    $membersObj = @($groupObj.psbase.Invoke("Members")) 
    
    $members = ($membersObj | foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)})
    
    $ErrorActionPreference = 'Continue'
    
    If ($members -contains $user) {
    $exists = $true
    } Else {
    $exists = $false
    }
    
    Write-Host "$exists"
    
    $computer = $env:COMPUTERNAME
    
    $customfieldname = "Admin?"
    $Customfieldtype = "String"
    
     $computerlist = @("ComputerName,$customfieldname")
    
     $computer | ForEach-Object {
    
    $computerlist += "$_,$exists"
    
    }
    
    pdqinventory createcustomfield -name $customfieldname -type $customfieldtype
    $tempfile = New-TemporaryFile
    $computerlist | out-file $tempfile
    
    pdqinventory importcustomfields -filename $tempfile -computercolumn "ComputerName" -customfields "$Customfieldname=$customfieldname" -allowoverwrite
    
    0
    Comment actions Permalink

Please sign in to leave a comment.