Wednesday 13 January 2016

Extract job titles from AD using powershell

For a given list of userids in a file called users.txt use the following powershell command in the same directory


Get-Content users.txt | foreach {Get-ADUser -Identity $PSItem -Properties mail,title | select -Property mail, title} |Export-Csv users.csv

Or use this to find new starters in the organisation...

$lastWeek = (get-date).AddDays(-31);

Get-ADUser -Filter "Description -like 'London'"  -Properties Name, Title, Manager, whenCreated, DistinguishedName, LastLogonDate | ? whenCreated  -gt $lastWeek | select Name, Title, Manager, whenCreated, DistinguishedName, LastLogonDate | fl

Note: For windows 7 desktop users you’ll need to install the active directory modules for powershell by following the installation steps here
https://www.microsoft.com/en-gb/download/details.aspx?id=7887
For all active directory attributes / propery names, see this link here
http://www.kouti.com/tables/userattributes.htm
Another way to have done it would have been usng SQL via a linked server
https://www.mssqltips.com/sqlservertip/2580/querying-active-directory-data-from-sql-server/
e.g.
SELECT     *
FROM OPENQUERY( ADSI,
    'SELECT samaccountname, mail, title, sn
     FROM ''LDAP://dc=companyname,dc=com''
     WHERE objectCategory = ''Person'' AND objectClass= ''user''
     AND userprincipalname = ''*'' AND mail = ''*'' AND SN = ''*''
         ORDER BY useraccountcontrol
      ')
      Where
      samaccountname= ‘myaccountloginname’

Wednesday 6 January 2016

Powershell cruise control build output parser

<#
.Synopsis
   Write-XmlPathOutput processes all files in a directory for a given XPath expression and puts the results to .txt files of the same name
.EXAMPLE
   Write-XmlPathOutput -DirectoryPath DirectoryPath -XPath //*/project
.INPUTS
   DirectoryPath - the path to the directory
   XPath - the xpath you wish to use
.OUTPUTS
   .txt text files in the same directory
#>
function Write-XmlPathOutput
{
    [CmdletBinding(DefaultParameterSetName='Parameter Set 1',
                  SupportsShouldProcess=$false,
                  PositionalBinding=$false,
                  HelpUri = 'http://merrickchaffer.blogspot.com/',
                  ConfirmImpact='Medium')]
    [OutputType([String])]
    Param
    (
        # Param1 help description
        [Parameter(Mandatory=$true,
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true,
                   ValueFromRemainingArguments=$false,
                   Position=0,
                   ParameterSetName='Parameter Set 1')]
        [ValidateNotNull()]
        [Alias("p")]
        [String]
        $DirectoryPath = (Get-Location).Path,

        # xpath help description
        [Parameter(Mandatory=$false, ParameterSetName='Parameter Set 1', Position=1,
                    ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true,
                   ValueFromRemainingArguments=$false )]
        [Alias("x")]
        [String]
        $XPath = "//*/project/@file"

    )

    Begin
    {
    }
    Process
    {
        if ($pscmdlet.ShouldProcess("Target", "Operation"))
        {
            $files = Get-ChildItem (Get-ChildItem $DirectoryPath\*.xml)
            if ($files -ne $null -and $files.Length -gt 0) {
               
                ForEach ($file in $files) {
                 
                  $OutFile = [string]::Concat($file.FullName, ".txt")
                 
                  Select-Xml -Path $file.FullName -XPath $XPath | Select -ExpandProperty Node | Out-File $OutFile

                }
            }

        }
    }
    End
    {
    }
}

Write-XmlPathOutput -DirectoryPath C:\temp -XPath "//*/project/@file"

How to find the last interactive logons in Windows using PowerShell

Use the following powershell script to find the last users to login to a box since a given date, in this case the 21st April 2022 at 12pm un...