简体   繁体   English

PowerShell登录脚本以管理员身份

[英]PowerShell logon script as admin

I have a PowerShell script that I need to run at user logon. 我有一个PowerShell脚本,需要在用户登录时运行。 The script uses the AD module to see if a users is part of a group and get other info, plus also I get local variables like: 该脚本使用AD模块来查看用户是否属于某个组并获取其他信息,此外我还会获取局部变量,例如:

$env:DOMAINNAME
$env:USERNAME
$env:APPDATA
$env:TEMP

And is why I need it to run at logon but I think is a permission issue. 这就是为什么我需要它在登录时运行,但我认为这是一个权限问题。

Any idea how could I do this? 知道我该怎么做吗? I tried adding the script under User Configuration → Policies → Windows Settings → Scripts → Logon , and it did not work, also tried under Computer Configuration . 我尝试在“ 用户配置”→“策略”→“ Windows设置”→“脚本”→“登录”下添加脚本,但是该脚本不起作用,也尝试在“ 计算机配置”下尝试。

Added -ExecutionPolicy Bypass as parameter and still nothing. 添加了-ExecutionPolicy Bypass作为参数,但仍然没有。

I also tried to add is as a scheduled task and run it as other user but still no luck. 我还尝试将is作为计划任务添加,并以其他用户身份运行它,但还是没有运气。

Since you shared your code in one of the other comments, I can make some assumptions about what you're trying to achieve and help with that. 由于您在其他注释中共享了您的代码,因此我可以对您要实现的目标和提供的帮助做出一些假设。 So let's break it down. 因此,让我们分解一下。

ActiveDirectory Module ActiveDirectory模块

The first thing we need to do is remove the dependency on the ActiveDirectory module. 我们需要做的第一件事是删除对ActiveDirectory模块的依赖。 As explained by some of the comments here, the ActiveDirectory module is a built-in component of the RSAT suite. 如此处的一些评论所解释,ActiveDirectory模块是RSAT套件的内置组件。 When this script is running on your client computers, this won't be available and we don't want to make it available as it creates a large dependency overhead and exposes admin utilities to your users. 当此脚本在您的客户端计算机上运行时,该脚本将不可用,我们也不想使其可用,因为它会产生较大的依赖项开销,并向您的用户公开管理实用程序。

The cmdlets you're using are simply Get-ADUser , Get-ADGroup and Get-ADPrincipalGroupMembership . 您使用的cmdlet只是Get-ADUserGet-ADGroupGet-ADPrincipalGroupMembership These are search cmdlets using the Lightweight Directory Access Protocol (LDAP), and so we can replace them with our own search functions using .NET classes. 这些是使用轻型目录访问协议(LDAP)的搜索cmdlet,因此我们可以使用.NET类将它们替换为我们自己的搜索功能。

Get-ADUser Get-ADUser

We can replace the Get-ADUser search cmdlet with our own function defined at runtime like so: 我们可以用在运行时定义的我们自己的函数替换Get-ADUser搜索cmdlet,如下所示:

function Get-ADUser 
{
    Param ( [string]$Identity = $null )
    IF ($Identity)
    {

        $UserSearcher = New-Object DirectoryServices.DirectorySearcher
        $UserSearcher.SearchRoot = "LDAP://$("DC=$(($ENV:USERDNSDOMAIN).Replace(".",",DC="))")"
        $UserSearcher.Filter = "(&(objectCategory=person)(SAMAccountName=$Identity))"

        $UserSearcher.FindAll() | foreach {New-Object PSObject -Property:$_.Properties}
    }
}

Get-ADGroup 获取广告组

We also need to replace the Get-ADGroup cmdlet so that we can grab the AD Groups you've specified in your $OfficeLocations list. 我们还需要替换Get-ADGroup cmdlet,以便可以获取在$OfficeLocations列表中指定的AD组。

function Get-ADGroup 
{
    Param ( [string]$Identity = $null )
    IF ($Identity)
    {

        $GroupSearcher = New-Object DirectoryServices.DirectorySearcher
        $GroupSearcher.SearchRoot = "LDAP://$("DC=$(($ENV:USERDNSDOMAIN).Replace(".",",DC="))")"
        $GroupSearcher.Filter = "(&(objectCategory=group)(SAMAccountName=$Identity))"

        $GroupSearcher.FindAll() | foreach {New-Object PSObject -Property:$_.Properties}
    }
}

Get-ADPrincipalGroupMembership Get-ADPrincipalGroupMembership

This cmdlet doesn't need to be replaced - not because it will work on it's own, but instead because you don't require it to achieve your desired end result. 此cmdlet不需要替换-不是因为它可以单独使用,而是因为您不需要它来达到所需的最终结果。 You have used it to retrieve group memberships from an AD User, however the AD User has a list of their memberships attached to their AD User object. 您已使用它从AD用户检索组成员身份,但是AD用户将其成员身份列表附加到其AD User对象。 So in essence, we can get a list of an AD User's memberships directly from the AD User object, and we will do that like this: 因此,从本质上讲,我们可以直接从AD User对象获取AD用户的成员资格列表,我们将像这样进行操作:

$userObject = Get-ADUser -Identity $env:USERNAME
$objGroup = $userObject.memberOf

What you will notice at this point is that the result set is not the group names, but their distinguished names. 此时您会注意到,结果集不是组名,而是它们的专有名称。 This is also just a string array of those distinguished names, and you will have to make some modifications to the rest of your code's comparison operators that filters this list of groups. 这也只是这些专有名称的字符串数组,您将必须对过滤此组列表的其余代码比较运算符进行一些修改。

Deploying your code 部署代码

The second thing we need to look at is how you're deploying this code. 我们需要查看的第二件事是您如何部署此代码。 There are numerous ways that you can achieve this, but let's assume that you're able to get the script deployed to the end user and assume that it runs. 您可以通过多种方法来实现此目的,但是假设您能够将脚本部署到最终用户并假定它可以运行。

Context 语境

The method you use to deploy this code needs to align with the code itself. 您用于部署此代码的方法需要与代码本身保持一致。 If your script makes assumptions about the context it's running in (such as using $env:USERNAME to gather the user's SAMAccountName for AD), then you need to ensure that the deployment method makes this assumption as well. 如果您的脚本对运行上下文进行了假设(例如使用$env:USERNAME收集用户的AD的SAMAccountName),那么您需要确保部署方法也进行了假设。 The script as it is right now is making that assumption - the assumption that it's the user's own context running this code. 现在的脚本正在做这个假设-假设它是用户自己的上下文来运行此代码。

To support this assumption, we need to make sure that the deployment method is going to run the script as the user . 为了支持此假设,我们需要确保部署方法将以user身份运行脚本。

Dependencies 依存关系

Now we know that the code is running as the user in their own context on the user's machine, and we know that the code accesses external resources such as Active Directory, we need to make sure that the user has the rights to these dependencies the code has, to ensure that the code will work. 现在我们知道代码在用户的计算机上以他们自己的上下文身份作为用户运行,并且我们知道代码访问诸如Active Directory之类的外部资源,我们需要确保用户对这些依赖项拥有权限,以确保代码能够正常工作。

Make sure you go through your code and list out all the tasks that the code performs. 确保您遍历代码并列出该代码执行的所有任务。 By that, I mean note that the code will read the user's own AD object, and it will read a network location to access signature files, and it will read & write to the user's own registry, as well as write to the user's appdata path. 那样的话,我的意思是请注意,代码将读取用户自己的AD对象,并且将读取网络位置以访问签名文件,并且将读取写入用户自己的注册表,以及写入用户的appdata路径。 。

In Conclusion 结论

With those dependencies verified, and with the deployment method lining up with the code, you should have yourself a working product. 验证了那些依赖项,并在代码中排列了部署方法之后,您应该拥有可以使用的产品。 Your code suggests there is a lot you have to learn about Powershell but if you keep at it and hone your understanding of fundamentals, you'll be ok. 您的代码表明您需要了解许多有关Powershell的知识,但是如果您继续学习并磨练对基本知识的了解,那您会没事的。 Keep learning, keep breaking stuff, and always remember to have fun along the way. 不断学习,不断打破事物,并始终记得在旅途中玩得开心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM