PowerShell Script: How to query a current logon user in part of E.G AD group domain1\Group1 ?

Hi Scripts guys,
I am new to powershell . I would like to achieve the following :
1. to check e.g if current logon user is member of .e.g. to domain1\group1
if yes then execute another
a. do a subst of a: to d:
b. follow by executing the 2nd cmd e.g execute.ps1  
else exit and
proceed with execute.ps1
please advice how best to achieve this ? Thanks in advance for your help :)

When testing the "current user" for group memberships, I prefer to do it locally using the user's logon token rather than having to query the directory. This has the advantage of automatically dealing with nested group memberships, and working
with local / builtin groups as well. Something along these lines:
function Test-CurrentUserIsMember
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
$Group
$sid = $Group -as [System.Security.Principal.SecurityIdentifier]
if ($null -eq $sid)
$account = $Group
if ($account -isnot [System.Security.Principal.IdentityReference])
$account = [System.Security.Principal.NTAccount][string]$Group
try
$sid = $account.Translate([System.Security.Principal.SecurityIdentifier])
catch
throw "Group '$Group' could not be resolved to a Security Identifier"
return ([System.Security.Principal.WindowsIdentity]::GetCurrent().Groups -contains $sid)
if (Test-CurrentUserIsMember -Group 'DOMAIN1\Group1')
# User is a member of the group (directly, or via nested group memberships.)
Write-Host "Is Member"
else
# User is not a member of the group.
Write-Host "Is Not Member"

Similar Messages

  • Can I query to PowerShell scripts from Power Query ?

    Can I query to PowerShell scripts from Power Query ?
    I want to use PowerShell result for Data Visualization in Excel.
    Like importing from web, odata, hdinsight, etc,
    I want to import result of PowerShell script.
    PowerShell can do a lot of system management.
    Regards,
    Yoshihiro Kawabata 

    This is not possible today and is not something that's likely to be implemented. The combination of being able to easily share queries and being able to easily launch external scripts that can do anything supported by the current user's permissions is
    something of a security nightmare.

  • How to get current logon user list of biee?

    All,
    I used BIEE 10.1.3.3. I want to export current logon user list to OS file. How to do it? I know BIEE session manager can do it. But I must export session information by java or other program.

    Hi,
    You could create a procedure which stores this information at logon and updates this information at log off.
    On the 'Connection Scripts' tab of a Connection Pool you can define two sorts of scripts; 'Execute on connect' and 'Execute on disconnect'. This way you can store the user_name and the connect and disconnect in a table.
    Good Luck,
    Daan Bakboord

  • How to query which Tcodes specified user used with one day?

    Hi guys:
      How to query which Tcodes specified user used with one day?

    Hi
    You can use transaction code ST03N.
    1. Go to tx code - ST03N
    2. Under "Workload" you can select your "Instance or Total" so that it expands and shows you Days, Week, Month.
    3. If you want to look at the transactions executed for a particular day, lets say, then double click on any day.
    4. At the bottom left you get "Analysis Views"
    5. Select & expand "User and Settlement Statistics"
    6. Double click on "User Profile"
    7. On the right side of the window you get a list of all the users
    8. Double click on a particular user you want to view the details of.
    9. The new screen will display the "Transactions/Reports Utilized by User XXX"
    If you wanna track which users executed a particular transacation then follow this:
    10. In "Analysis Views" expand "Transaction Profile"
    11. Double click on "Standard"
    You can view the list of Transactions and Reports.
    12. Double click on the Tx Code or Report you wanna check and it will show the Use of it.
    This will help you.
    Regards
    Sumit Jain
    [reward with points if the answer is useful]

  • Using Powershell Script Run simple query in MS Access 2007 and export the results of query to Excel

    Hi Experts,
    I have a Access 2007 DB file and 2 Big tables inside that (bigger than the size that can be easily handled by MS Excel 2007).
    My requirement is automate using powershell scripts the below things.
    1. Create a SQL query in Access DB and save that in access DB
    2. Run the saved query and export the result in excel sheet where I can create the charts and Pivots. Thanks in advance
    Prajesh

    Do you have to use the Access query, couldn't you just recreate the query in Powershell?  Here's a link with good info that references an existing script for querying an Access database:
    http://blogs.technet.com/b/heyscriptingguy/archive/2009/08/13/hey-scripting-guy-can-i-query-a-microsoft-access-database-with-a-windows-powershell-script.aspx
    Once you have your dataset you can pipe it to
    Export-Csv -NoType c:\pathtofile\output.csv

  • Powershell script - how to read a registry hive and store the value in text file and then again read the text file to write the values back in registry

    Hi All,
    powershell script Method required to read a value from registry and then taking the backup of that values in some text file.
    For example the hive is
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path
    and under path i need to take back up  of values in some text file and then put some value in the registry after back is taken in text file.
    Also how to read the text file values so that we can again write to registry hive  back from the back up text file.
    Your help is much appreciated.
    Umeed4u

    I think you need to read this first:
    http://social.technet.microsoft.com/Forums/scriptcenter/en-US/a0def745-4831-4de0-a040-63b63e7be7ae/posting-guidelines?forum=ITCG
    Don't retire TechNet! -
    (Don't give up yet - 12,830+ strong and growing)

  • Powershell script to Scan Active Directory Attributes for Country and Department ,Then add to Sales Group then add to Distribution list based on Region

    Hey Scripting Guys,
    I have been in and out of Powershell last few years, not that great at it tbh !!! I'm looking for advice on how I can as in Title, Create a Powershell script to Scan Active Directory Attributes for Country and Department ,Then add to Group then add to Distribution
    list based on Region/Country
    I was thinking along the lines of get-aduser -LDAPFilter "(department=SALES France) and adding a where clause for country.
    Any help would be great.
    Dec

    So I have tried a few variations but get errors on both 
    get-aduser -LDAPFilter "(&(department=SALES)(c=us))" | Add-ADPrincipalGroupMembership -MemberOf "testgroup"
    get-aduser -LDAPFilter "(&(department=SALES)(c=fr))" | Add-ADGroupMember -identity "testgroup"
    Add-ADPrincipalGroupMembership : Object reference not set to an instance of an
    object.
    At line:1 char:86
    + get-aduser -LDAPFilter "(&(department=SALES)(c=fr))" | Add-ADPrincipalGroupMe
    mbership <<<< -MemberOf "testgroup"
    + CategoryInfo : NotSpecified: (:) [Add-ADPrincipalGroupMembershi
    p], NullReferenceException
    + FullyQualifiedErrorId : Object reference not set to an instance of an ob
    ject.,Microsoft.ActiveDirectory.Management.Commands.AddADPrincipalGroupMem
    bership

  • How to Get the Current Logged user location ID in SharePoint online

    Hi All ,
    Please suggest me how we can get the SharePoint online current Login user
    Location ID
    Actually i want to show the weather report in my SharePoint online site (App Part in SharePoint Online Site)
    Suppose SPO user Login From India(Delhi) so that i want to show the India(Delhi) weather report
    If Login user from US(dallas) so that US user see the US(dallas) weather report 
    Thanks
    Deepak Chauhan
    SharePoint 2010 & 2013 and Office-365 Branding and Front End Customization, UI Design

    @Mohamed
    Derhalli ,
    Suppose SPO user Login From India(Delhi) so that i want to show the India(Delhi) weather report
    If Login user from US(dallas) so that US user see the US(dallas) weather report
    so without using how we can show the weather report can you please suggest me how we can show the weather report
    Reply ..
    Thanks
    Deepak Chauhan 
    SharePoint 2010 & 2013 and Office-365 Branding and Front End Customization, UI Design

  • How to capture the Windows logon user

    Hi. How I can to capture the Windows logon user for the browser.
    I have a procedure on webtoolkit, and when I call the proc I pass the user of application, I need check if the net user is the same.
    Thanks

    if you have to take the user name of the server machine it is just
    System.getProperty("user.name");if you want to retrive the username from the client machine
    then look at this thread
    http://forum.java.sun.com/thread.jspa?threadID=766416&messageID=4370490#4370490

  • How to fetch the current login user name at the BI system

    Hi,
    In our scenerio we need a query to fetch the current login user name at the BI system. Would you please suggest the possibilities?
    Thanks and regards,
    Pradip

    Hi,
    The current login id will be available in text elements of query.
    Regards
    Akhan

  • How to query the current instance size of an instance of a process

    Hello guys,
    I'm currently doing a Capacity Plan for my BPM enviroment and I would like to know the current instance size of different kind of processes deployed in my environment. I'll need this to calculate an average instance size per process. The main reason for this capacity plan is that our environment instance size is configured to more then 1000kb(don't know what kind of issues occurred in the past that led to this exceeded value configured) and I don't want to leave this configuration but I can't be safe decreasing it without know if our production environment will start launching the "Max Instance Size Exceeded" error.
    Is there any way to fetch this information? Any API or SQL Query where I can find this information?
    Thanks for any help.
    Regards,
    Luiz
    Edited by: Luiz Rocha on 30/04/2009 16:50

    Hi, Luiz!
    In order to estimate the instance size, you will have to sum up all the instance variables size.
    From the practical prospective it may be difficult to use the algorithm in cases when your instance variables are not simple types.
    For already existing instances, you can look the instance size in the database:
    An instance is just a java object, that is serialized as a byte[] and stored in the engine database, PPROCINSTANCE table, column INSTDATA.
    You can write a simple program (either java or SQL plus) that would read the instance from the BLOB and measure the size.
    And yes, with large instances you can have performance problems.

  • How does BR get current date, user, and associate member with Attribute Dim

    I need use Business rules or Calc script to implement following functions:
    1. Get current date and the name of user who is running the BR and save the information to cube.
    I don't find any functions to get current date and users.
    Also, since text and date is store in relational database and essbase cube only stores the index, it looks that the value can't be changed or stored by using "==" directly, is there any function to change value of members with Text or Date types in BR/Calc script?
    2. End users select attribute value (via smart list) of products or projects(Sparse dimensions) in data form, run BR to update the association of these members with attribute dimension.
    I don't find any functions to change the attribute association in BR/Calc, is there any CDF (Custom Defined Function) that can do it?
    Thanks!

    Hi,
    For the date functionality, check out the post below.
    Re: Days behaviour between two dates
    As for the username, there is a little tricky way that requires an unused or a new dimension along with a smart list of user names. It's also possible to capture the user name from the cookies and pass it on to the form. The latter is possible through validatedata.js however requires hefty coding here and there.
    As for the attributes, it's not possible to update metadata through business rule. So no luck in there.
    Cheers,
    Alp

  • How can I Deny permissions to logon to Remote Desktop Session Host server in powershell script?

    I am need of some assistance please. I am a system admin and I am trying to create a script that will assist with the tedious tasks I have to do with disabling a user that no longer works for the company.
    I have created a script so far that will reset the users passwords and remove them from all groups (minus domain users).
    I am trying to make it where it will deny permissions to logon to Remote Desktop Session Host server as well as give full mailbox permission to the manager in Exchange Server 2010.
    I know with Exchange 2010, I will need to add the Powershell snapin. Is there a way for this to be added into the script? I am thinking to add the code:
    add-pssnapin Microsoft.exchange.management.powershell.e2010
    Is there another way to do this? Any help or recommendations would be much appreciated.
    $ou = Get-ADUser -SearchBase "<*OU info here*>" -Filter * |
    Set-ADAccountPassword -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "<*Password here*>" -Force)
    foreach ($user in $ou) {
    $UserDN = $user.DistinguishedName
    Get-ADGroup -LDAPFilter "(member=$UserDN)" | foreach-object {
    if ($_.name -ne "Domain Users") {remove-adgroupmember -identity $_.name -member $UserDN -Confirm:$False} }

    Why not just disable the account?Why are you searching an OU foro users when you just want to terminate one user?
    You can remotely connect an exchange session and manipulate the mailbox permissions.  You do not load a snap-in except on the Exchange server.
    $Session=New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<FQDN of Exchange 2013 Client Access server>/PowerShell/
    Import-PSSession $Session
    # exchange commands here
    \_(ツ)_/
    We have a checklist we have to go through with the tasks listed. We have to keep to the account enabled until HR changes
    the status which is usually 30-90 days depending. Managers sometimes need to access the accounts to retrieve information, etc. We put the users in an OU; once we are given permission from the manager we move forward in the removal. 

  • How to Read the "text file and csv file" through powershell Scripts

    Hi All
    i need to add a multiple users in a particular Group through powershell Script how to read the text and CSV files in powershell
    am completly new to Powershell scripts any one pls respond ASAP.with step by step process pls
    Regards:
    Rajeshreddy.k

    Hi Rajeshreddy.k,
    To add multiple users to one group, I wouldn't use a .csv file since the only value you need from a list is the users to be added.
    To start create a list of users that should be added to the group, import this list in a variable called $users, the group distinguishedName in a variable called $Group and simply call the ActiveDirectory cmdlet Add-GroupMember.
    $Users = Get-Content -Path 'C:\ListOfUsernames.txt'
    $Group = 'CN=MyGroup,OU=MyOrg,DC=domain,DC=lcl'
    Add-ADGroupMember -Identity $Group -Members $Users

  • How to Display a List of Users Currently Logged On to Domain

    Windows Server 2008 R2 Enterprise.
    I had a snap-in in Windows Server 2003 that allowed me to see who was currently logged onto the domain.  How can I do this in Windows Server 2008 R2?  I am the administrator, so there is no privilege issue.  Thanks for any help.
    Doug Pruiett Good News Jail & Prison Ministry Richmond, Virginia www.goodnewsjail.org

    Hi,
    If you want to list current logon users from all servers in a domain, I’m afraid there is no existing tool from Microsoft to support you do that. However, you can use LsaEnumerateLogonSessions
    API like
    http://msdn.microsoft.com/en-us/library/windows/desktop/aa375400(v=vs.85).aspx to enumerate all logon sessions on the clients and gather all information together to achieve your target.  
    If you want to find out who is logged on to a specific terminal server, there are four methods:
    1. check event log
    2. write some PowerShell scripts as:
    http://technet.microsoft.com/en-us/library/ff730963.aspx
    http://learn-powershell.net/2010/11/01/quick-hit-find-currently-logged-on-users/
    3. use WMI&VBScript as:
    http://blogs.msdn.com/b/alejacma/archive/2008/03/04/how-to-get-the-logged-on-user-with-wmi-vbscript.aspx
    4. use some third-party tools like others suggested.
    In addition, here is another thread who used scripts to realize this for your reference:
    http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/c956a5d9-8895-4042-8047-78a44ffc1d53
    Regards,
    Cicely

Maybe you are looking for

  • Firefox will not open. Process runs, but browser window will not open.

    I am unable to get the Firefox browser window to launch. The application will attempt to open, but the browser window will not appear. Task manager indicates that the process is running, it just will not launch the window. No amount of restarts or re

  • Not able to convert to PDF using ShareAPI.addFile

    Hi, I am using Java library to use acrobat.com service of converting word doc to PDF. I am using following call to upload the doc for converion: node= shareAPI.addFile(acrobatUser, uploadFile, uploadFile.getName(), "Test doc",null, false,true); node.

  • Remove sub total text in ALV.

    Hello, How can I remove the text in sub total in ALV? I want to show only sub total values, not any explanation. Thanks.

  • BAPI to insert text EENO_DYNP-ZEILE

    Dear All. We are facing an scenario in which we need to insert text in field EENO_DYNP-ZEILE vía BAPI or BATCH for CONNECTION OBJECT AND DEVICE LOCATION. Our question is *¿Do you know the BAPI-FIELD which store this long text? Thanks in Advance. Nico

  • Deleting everything prior to a spot in a song

    I want to split a song and delete the first part. How can I do this?