【问题标题】:Jenkins automation with powershell script on ADJenkins 自动化与 AD 上的 powershell 脚本
【发布时间】:2020-10-30 09:08:22
【问题描述】:

我正在尝试在 jenkins 中使用以下脚本,但出现错误:

错误:

Running as SYSTEM
Building in workspace C:\Program Files (x86)\Jenkins\workspace\Move_Disable_Inactive_Computers
[Move_Disable_Inactive_Computers] $ powershell.exe -NonInteractive -ExecutionPolicy Bypass -File C:\Windows\TEMP\jenkinsxxxxxxxxxxxxxxx.ps1
Disable-ADAccount : Insufficient access rights to perform the operation
At C:\Windows\TEMP\jenkins8240077775170239915.ps1:14 char:19
+ $StaleComputers | Disable-ADAccount
+                   ~~~~~~~~~~~~~~~~~

脚本:

Import-Module ActiveDirectory

$ErrorActionPreference = 'Stop'
$Password = $env:Password | ConvertTo-SecureString -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $env:UserName, $Password

$DaysInactive = 45
$DestinationOU = "OU=test,OU=test,DC=test,DC=test"
$time = (Get-Date).Adddays(-($DaysInactive))

$StaleComputers = Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -ResultPageSize 2000 -resultSetSize $null -Properties LastLogonTimeStamp 
$StaleComputers | Export-CSV C:\Inactive_Disabled_Computers_list_$((Get-Date).ToString('MM-dd-yyyy')).csv -NoTypeInformation -Encoding UTF8
$StaleComputers | Disable-ADAccount
$StaleComputers | %{ Move-ADObject -Identity $_.DistinguishedName -TargetPath $DestinationOU }

如果我注释最后 2 个命令行,则脚本正在运行,但仍然无法执行我真正需要的操作、禁用和移动。

【问题讨论】:

  • 几乎域中的每个帐户都具有对 AD 的读取访问权限。所以Get-* 的东西通常可以由任何公认的帐户运行。但是,进行更改的 cmdlet 通常需要特定的权限……而运行该代码的帐户显然 没有这些权限
  • 抱歉,我还是不明白为什么我不能从 jenkins 的“Disable-ADAccount”命令中运行。我编写了另一个脚本,但仅使用此命令仍然存在相同的问题,但直接在 AD 服务器上脚本正在运行。
  • 您是否在两次运行中使用完全相同的帐户?这不是通常发生的情况……通常情况下,Jenkins 将使用具有严格限制权限的服务帐户。
  • 我在我的 Jenkins Build 中使用字符串参数选项和 AD 用户凭据连接到 AD 服务器。我使用的字符串参数是主机名、用户名和密码。问题可能是我的构建是以系统用户而不是 AD 用户身份运行的吗?我如何使用 AD 用户运行它?
  • 如果不使用同一个帐户或帐户具有不同的权限,那么您很可能会得到您报告的结果。您必须拥有相同的有效权限才能获得相同的访问权限。 ///// 看来您找到了获得所需权限的方法……好!很高兴看到您发现并解决了问题... [grin]

标签: powershell jenkins automation active-directory


【解决方案1】:

好的,我不确定这是否是最正确的解释方式,但我通过以下步骤解决了它: 我安装了插件“授权项目” 在 Configuration Global Security/Security Realm 中,我选择了 Active Directory(插入我的 AD 的所有选项)。 然后在“构建的访问控制”中,我选择了“以触发构建的用户身份运行”。

然后我更改了我的脚本,现在它看起来像这样并且一切正常。

    Import-Module ActiveDirectory
    
    $ErrorActionPreference = 'Stop'
    $Password = $env:Password | ConvertTo-SecureString -AsPlainText -Force
    $credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $env:UserName, $Password
    
    Invoke-Command -ComputerName $env:HostName -Credential $credentials -ScriptBlock {
    $ou = "DC=domain,DC=com"
    $DestinationOU = "OU=test,DC=domain,DC=com"
    $DaysInactive = 45
    $time = (Get-Date).Adddays(-($DaysInactive))

# path to the log file
$logpath = "C:\test_$((Get-Date).ToString('MM-dd-yyyy')).csv"
$findcomputers = Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -ResultPageSize 2000 -resultSetSize $null -Properties LastLogonTimeStamp
# Create a CSV containg all the Stale Computer Information
$findcomputers | export-csv $logpath
# Disable the Stale Computer Accounts
#$findcomputers | set-adcomputer -Description $description –passthru | Disable-ADAccount
$findcomputers | Disable-ADAccount
# Find all the Stale Computer Accounts we just disabled
$disabledAccounts = Search-ADAccount -AccountDisabled -ComputersOnly -SearchBase $ou
# Move the Disabled accounts to $disabledOU
$disabledAccounts | Move-ADObject -TargetPath $DestinationOU
}

【讨论】:

    猜你喜欢
    • 2021-10-29
    • 2013-11-23
    • 1970-01-01
    • 2017-08-13
    • 2022-10-05
    • 1970-01-01
    • 2014-06-09
    • 1970-01-01
    • 2021-11-23
    相关资源
    最近更新 更多