ahaydon / Qlik-Cli-Windows

Qlik-Cli for Windows
https://adamhaydon.uk/Qlik-Cli-Windows/
GNU General Public License v2.0
110 stars 51 forks source link

Issue in executing below code - # Remove Professional Access passes over date threshold #180

Open ShalakaKshirsagar opened 2 years ago

ShalakaKshirsagar commented 2 years ago

Hi @ahaydon ,

I am trying to remove the Professional Access passes over date threshold date. However not working and getting below error.

Code

Set the inactivity threshold. This is interactive. A non-interactive version can use this approach:

$InactivityThreshold = '30' # This is for 30 days

$InactivityThreshold = Read-Host -Prompt 'Input the username date threshold for inactivity (e.g. 90)'

Get date format for 90 days ago

$date = Get-Date $date = $date.AddDays(-$InactivityThreshold) $date = $date.ToString("yyyy/MM/dd") $time = Get-Date $time = $time.GetDateTimeFormats()[109] $inactive = $date + ' ' + $time

Connect using Qlik-CLI

Connect-Qlik | Out-Null

Write-Host $inactive

Remove Professional Access passes over date threshold

Get-QlikProfessionalAccessType -filter "lastUsed lt '$inactive'" -full | Remove-QlikProfessionalAccessType

=============================================================================

Error

Remove-QlikProfessionalAccessType : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input. At line:21 char:76

ahaydon commented 2 years ago

It looks like the issue is with the command $time.GetDateTimeFormats()[109], when I run this it returns nothing and so the date being used in the filter is '2022/03/12 '. Without the space at the end it would work as it assumes midnight, but the space is causing it to fail.

If I run $time.GetDateTimeFormats().Count I get a result of 30, so there is no 109th item in the list to select.

ShalakaKshirsagar commented 2 years ago

Hi @ahaydon

many thanks for the response.

$date = Get-Date $date = $date.AddDays(-$InactivityThreshold) $date = $date.ToString("yyyy-MM-dd") $time = Get-Date $time = $time.GetDateTimeFormats()[109] $inactive = $date + ' ' + $time

Variable "$inactive" contains "2022-04-01 09:38" e.g. I entered 10 as the input and I am getting "2022-04-01 09:38" value which is being passed to the forloop

I am actually referring to below script by Levi Turner

https://github.com/levi-turner/QlikSenseScripts/blob/master/qs-cli/qs-cli-remove_professional_analyzer_licenses.ps1

Request you to guide me which date format is needed to be passed "Remove-QlikProfessionalAccessType" for below comment.

Get-QlikProfessionalAccessType -filter "lastUsed lt '$inactive'" -full | Remove-QlikProfessionalAccessType

ahaydon commented 2 years ago

It looks like this depends on the version of PowerShell being used, since that works for you I assume you are using PowerShell 5. The correct format should look like this '2022-04-11T10:58:54', so you can use $time.GetDateTimeFormats()[87] on PowerShell 5. For PowerShell 7 it seems that it should be 21 rather than 87, but not sure how consistent it is.

A more consistent approach would be to use $time = Get-Date -Format 'yyyy-MM-ddTHH:mm:ss' as it should work on any version of PowerShell.

ShalakaKshirsagar commented 2 years ago

Great !!

Many thanks for the quick input. I tried both the suggested options.. However still no luck ! :(

BEFORE removing, i am trying to print the UserId which will be removed (just for the testing) using foreach loop. However its not returns any records

$InactivityThreshold = Read-Host -Prompt 'Input the username date threshold for inactivity (e.g. 90)'

Get date format for 90 days ago

$inactive = Get-Date $inactive = $inactive.AddDays(-$InactivityThreshold)

Write-Host $inactive

$inactive =Get-Date $inactive -Format 'yyyy-MM-ddTHH:mm:ss'

Connect using Qlik-CLI

Connect-Qlik | Out-Null

Remove Professional Access passes over date threshold

Get-QlikProfessionalAccessType -filter "lastUsed lt '$inactive'" -full | Remove-QlikProfessionalAccessType

$a=Get-QlikProfessionalAccessType -filter "lastUsed lt '$inactive'" -full

foreach ($b in $a){ $c = $b.user.userId Write-Host $c }

anythign missing here?

ShalakaKshirsagar commented 2 years ago

Hello @ahaydon

I am getting LastUsed value something like - "lastUsed":"2022-03-26T15:15:06.468Z"

Do i need to convert this format ?

ShalakaKshirsagar commented 2 years ago

Hello @ahaydon ,

Below simple script is also not working.. seems its not executing for loop. Anything missing?

$QSUsers=Get-QlikUser -filter "inactive eq true" -full

foreach ($QSUser in $QSUsers){

#$c = $b.userId
Write-Host  $QSUsers.userId

}

ahaydon commented 2 years ago

I would simplify your test further, what is returned when you just run Get-QlikProfessionalAccessType -filter "lastUsed lt '$inactive'" -full?