locked
Multiple (-and) (-or) within a single If statement RRS feed

  • Question

  • Hi guys!

    I'm trying build a script to block temporarily users who are in vacation.

    However, managers and chiefs cannot stay without access.

    How I can use more than one job position in my variable "$Cargo"?

    Import-csv C:\Temp\layout_unico.csv | ForEach {
    
    $Ausencia = $_.'Tipo Ausencia';
    $Email = $_.'E-mail Corporativo';
    $Afastamento = $_.Afastamento;
    $Cargo = $_.Cargo;
    
    if ($Ausencia -ne $Null -and $Cargo -ne "Analista de Infraestrutura II") {Get-ADUser -filter {EmailAddress -eq $Email} | Set-ADUser -Description ("Colaborador ausente. Status no SAP:" + "$Ausencia")}
    if ($Ausencia -ne $Null -and $Cargo -ne "Analista de Infraestrutura II") {Get-ADUser -filter {EmailAddress -eq $Email} | Disable-ADAccount}
    if ($Afastamento -ne $Null -and $Cargo -ne "Analista de Infraestrutura II") {Get-ADUser -filter {EmailAddress -eq $Email} | Set-ADUser -Description ("Colaborador afastado. Status no SAP:" + "$Afastamento")}
    if ($Afastamento -ne $Null -and $Cargo -ne "Analista de Infraestrutura II") {Get-ADUser -filter {EmailAddress -eq $Email} | Disable-ADAccount}
    
    }
    
    Thanks!


    David Soares MCTS:MBS - MCTS - MCITP

    Monday, August 14, 2017 3:31 PM

Answers

  • Hi Fred!

    It's possible include the job names into the code?

    I have a list with 290 differents jobs, and I've another one with name, email, phone etc.

    Regards.


    David Soares MCTS:MBS - MCTS - MCITP

    What do you mean by "include it"?

    You can load $cargo from a file:

    $jobs = Get-Content jobs.txt

    $_.Cargo -notin $jobs


    \_(ツ)_/

    Monday, August 14, 2017 4:02 PM

All replies

  • Hi David,

    you can do that by splitting its contents. Basically, you put multiple positions into the "Cargo" column in the csv and separate them by a common delimiter, similar to the csv in its entirety. For example you could use ";".

    That would change your script like this:

    Import-csv C:\Temp\layout_unico.csv | ForEach {
    	$Ausencia = $_.'Tipo Ausencia'
    	$Email = $_.'E-mail Corporativo'
    	$Afastamento = $_.Afastamento
    	$Cargo = $_.Cargo.Split(";")
    
    	if ($Ausencia -ne $Null -and $Cargo -notcontains "Analista de Infraestrutura II")
    	{
    		$user = Get-ADUser -filter {EmailAddress -eq $Email}
    		$user | Set-ADUser -Description "Colaborador ausente. Status no SAP:$Ausencia"
    		$user | Disable-ADAccount
    	}
    	if ($Afastamento -ne $Null -and $Cargo -notcontains "Analista de Infraestrutura II")
    	{
    		$user = Get-ADUser -filter {EmailAddress -eq $Email}
    		$user | Set-ADUser -Description "Colaborador afastado. Status no SAP:$Afastamento"
    		$user | Disable-ADAccount
    	}
    }

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Monday, August 14, 2017 3:43 PM
  • The key is to simplify.

    Import-csv C:\Temp\layout_unico.csv | ForEach-Object { if ($_.'Tipo Ausencia' -and $_.Cargo -notmatch 'Analista de Infraestrutura II') { Get-ADUser -filter { EmailAddress -eq $_.'E-mail Corporativo' } | Set-ADUser -Description "Colaborador ausente. Status no SAP: $($_.'Tipo Ausencia')" -PassThru | Disable-ADAccount }elseif ($_.Afastamento -and $_.Cargo -notmatch 'Analista de Infraestrutura II') { Get-ADUser -filter { EmailAddress -eq $_.'E-mail Corporativo' } | Set-ADUser -Description "Colaborador afastado. Status no SAP: $($_.Afastamento)" -PassThru | Disable-ADAccount }else{
    Write-Host 'No match'
    } }



    \_(ツ)_/







    • Edited by jrv Monday, August 14, 2017 3:50 PM
    Monday, August 14, 2017 3:45 PM
  • Hi Fred!

    It's possible include the job names into the code?

    I have a list with 290 differents jobs, and I've another one with name, email, phone etc.

    Regards.


    David Soares MCTS:MBS - MCTS - MCITP

    Monday, August 14, 2017 3:56 PM
  • Hi Fred!

    It's possible include the job names into the code?

    I have a list with 290 differents jobs, and I've another one with name, email, phone etc.

    Regards.


    David Soares MCTS:MBS - MCTS - MCITP

    What do you mean by "include it"?

    You can load $cargo from a file:

    $jobs = Get-Content jobs.txt

    $_.Cargo -notin $jobs


    \_(ツ)_/

    Monday, August 14, 2017 4:02 PM
  • Thank you!



    David Soares MCTS:MBS - MCTS - MCITP

    Monday, August 14, 2017 5:50 PM