I'm trying to use this script below in jenkins but i'm getting an error:
Error:
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
+ ~~~~~~~~~~~~~~~~~
The Script:
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 }
If i comment the 2 last commands line, the script is working but still missing to do what exactly i need, disabled and move.
Get-*stuff can usually be run by any recognized account. the cmdlets that make changes, however, normally require specific perms ... and the account running that code apparently does not have those perms.