I am putting together a simple form for our service desk to use to standardize the process of offboarding users.
Part of the process is to disable any account not used for 30 days, so I have put together code to list all accounts not contained in 2 specific OUs (Service accounts and another OU containing accounts with interactive logon disabled). This works in native Powershell, but as soon as I insert it into the form to run when a button is clicked it gives me an error.
The code I am using is
As I said, running this without the Out-String in native Powershell gives me a list of accounts, running it in the form, with or without the Where-Object conditions gives me this error
Out-String : A positional parameter cannot be found that accepts argument 'True'.
At line:228 char:133
+ ... stamp).ToString('MM-dd-yyyy') } } | Out-String $DisplayWindow.Focus()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Out-String], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.OutStringCommand
I have other forms I have created for other purposes where I have used Out-String to display results and it has worked, it still works using the machine I am getting the error on the new form.
What is causing this? The only "true" in this whole thing is telling it to only run against accounts that are enabled, if I remove this it still gives the same error.
I modified the script to export the results to a csv, that works just fine using the same code other than export-csv rather than out-string.
Part of the process is to disable any account not used for 30 days, so I have put together code to list all accounts not contained in 2 specific OUs (Service accounts and another OU containing accounts with interactive logon disabled). This works in native Powershell, but as soon as I insert it into the form to run when a button is clicked it gives me an error.
The code I am using is
Code:
{
$InactiveDays = 30
$Days = (Get-Date).Adddays(-($InactiveDays))
Get-ADUser -Filter { LastLogonTimeStamp -lt $Days -and enabled -eq $true } -SearchBase 'DC=company,DC=LOCAL' -Properties LastLogonTimeStamp | Where-Object { ($_.DistinguishedName -notlike "*OU=OU,OU=OU,DC=company,DC=local") } | Where-Object { ($_.DistinguishedName -notlike "*OU=OU,OU=OU,OU=Special Users,DC=company,DC=local") } |
select-object Name, @{ Name = "Date"; Expression = { [DateTime]::FromFileTime($_.lastLogonTimestamp).ToString('MM-dd-yyyy') } } | Out-String $DisplayWindow.Focus()
}
Out-String : A positional parameter cannot be found that accepts argument 'True'.
At line:228 char:133
+ ... stamp).ToString('MM-dd-yyyy') } } | Out-String $DisplayWindow.Focus()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Out-String], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.OutStringCommand
I have other forms I have created for other purposes where I have used Out-String to display results and it has worked, it still works using the machine I am getting the error on the new form.
What is causing this? The only "true" in this whole thing is telling it to only run against accounts that are enabled, if I remove this it still gives the same error.
I modified the script to export the results to a csv, that works just fine using the same code other than export-csv rather than out-string.
Statistics: Posted by GarethDavies — Thu Feb 08, 2024 1:19 pm — Replies 2 — Views 54