none
Exporting information log RRS feed

  • Question

  • Hi guys.

    I'm trying export a log about users disabled by my script in current day, but the file generated always stay blank.

    Someone knows what is wrong?

    $jobs = Get-Content C:\Temp\jobs.txt
    $Date = (Get-Date -format "dd-MM-yyyy")
    
    $Lista = Import-csv C:\Temp\layout_unico.csv | ForEach {
    
    $Ausencia = $_.'Tipo Ausencia';
    $Email = $_.'E-mail Corporativo';
    $Afastamento = $_.Afastamento;
    $Cargo = $_.Cargo;
    $Nome = $_.Nome;
    $Usuario = Get-ADUser -Filter {EmailAddress -eq $Email} -Properties Name,Description,Enabled,Title,EmployeeID,Emailaddress;
    
    if ($Ausencia -ne $Null -and $_.Cargo -notin $jobs) {$Usuario | Set-ADUser -Description ("Colaborador ausente. Status no SAP:" + "$Ausencia")}
    if ($Ausencia -ne $Null -and $_.Cargo -notin $jobs) {$Usuario | Disable-ADAccount}
    if ($Afastamento -ne $Null -and $_.Cargo -notin $jobs) {$Usuario | Set-ADUser -Description ("Colaborador afastado. Status no SAP:" + "$Afastamento" + " $Date")}
    if ($Afastamento -ne $Null -and $_.Cargo -notin $jobs) {$Usuario | Disable-ADAccount}
    if ($Usuario.Description -like "*$Date*") {New-Object PSObject -Property @{Nome=$Usuario.Name;Ausencia=$Usuario.Description;Afastamento=$Usuario.Description;StatusAD=$Usuario.Enabled;Cargo=$Cargo;Matricula=$Usuario.EmployeeID;Email=$Usuario.Emailaddress | Export-csv C:\Temp\Log_Afastados_Ausentes_$Date.csv -NoTypeInformation -Encoding Unicode}}
    
    }
    
    

    Thanks.


    David Soares MCTS:MBS - MCTS - MCITP

    • Moved by Bill_Stewart Friday, September 22, 2017 2:45 PM This is not "fix/debug/rewrite my script for me" forum
    Tuesday, August 15, 2017 5:47 PM

All replies

  • If you were to format your code correctly it might be easier for us to understand what you are asking.  What is visible shows no log files being created.


    \_(ツ)_/

    Tuesday, August 15, 2017 6:00 PM
  • This is where your syntax error is:

    	if ($Usuario.Description -like "*$Date*") { 
    		New-Object PSObject -Property @{ Nome = $Usuario.Name;
    										Ausencia = $Usuario.Description; 
    										Afastamento = $Usuario.Description; 
    										StatusAD = $Usuario.Enabled; 
    										Cargo = $Cargo; 
    										Matricula = $Usuario.EmployeeID; 
    										Email = $Usuario.Emailaddress | 
    				Export-csv C:\Temp\Log_Afastados_Ausentes_$Date.csv -NoTypeInformation -Encoding Unicode 
    		} 
    	}

    A better way - as I tried to show you once before - would help you to easily see errors.

    The following would be easier to understand and manage:

    $jobs = Get-Content C:\Temp\jobs.txt
    $Date = (Get-Date -format "dd-MM-yyyy")
    
    $props = @{
    	Nome   = $Usuario.Name
    	Ausencia = $Usuario.Description
    	Afastamento = $Usuario.Description
    	StatusAD = $Usuario.Enabled
    	Cargo  = $Cargo
    	Matricula = $Usuario.EmployeeID
    	Email  = $Usuario.Emailaddress
    }
    
    $Lista = Import-csv C:\Temp\layout_unico.csv | 
    	ForEach-Object {
    		$Ausencia = $_.'Tipo Ausencia';
    		$Email = $_.'E-mail Corporativo';
    		$Afastamento = $_.Afastamento;
    		$Cargo = $_.Cargo;
    		$Nome = $_.Nome;
    		$Usuario = Get-ADUser -Filter { EmailAddress -eq $Email } -Properties Name, Description, Enabled, Title, EmployeeID, Emailaddress;
    		
    		if ($Ausencia -and $_.Cargo -notin $jobs) { 
    			$Usuario | Set-ADUser -Description ("Colaborador ausente. Status no SAP:" + "$Ausencia") -PassThru |
    			 	Disable-ADAccount 
    		}
    		if ($Afastamento -and $_.Cargo -notin $jobs) { 
    			$Usuario | Set-ADUser -Description ("Colaborador afastado. Status no SAP:" + "$Afastamento" + " $Date") |
    				Disable-ADAccount
    		}
    		if ($Usuario.Description -like "*$Date*") { 
    			[pscustomobject]$props 
    		} 
    	} |
    	Export-csv C:\Temp\Log_Afastados_Ausentes_$Date.csv -NoTypeInformation -Encoding Unicode
    Never test against $null or $false. Just treat the object like a Boolean.


    \_(ツ)_/


    • Edited by jrv Tuesday, August 15, 2017 6:10 PM
    Tuesday, August 15, 2017 6:09 PM