Getting DN's of PCs instead of their names in Excel

Hi there. I want to get DistinguishedName of PCs instead of their CN. I tried everything, and nothing is work. Please help to modify this code in way to display DN's:

function Get-ActivationStatus {
[CmdletBinding()]
        [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] 
        [string]$DNSHostName = $Env:COMPUTERNAME 
    )
    process {
        try {
            $wpa = Get-WmiObject SoftwareLicensingProduct -ComputerName $DNSHostName `
            -Filter "ApplicationID = '55c92734-d682-4d71-983e-d6ec3f16059f'" `
            -Property LicenseStatus,Description -ErrorAction Stop
        }
        catch {
            $status = New-Object ComponentModel.Win32Exception ($_.Exception.ErrorCode)
            $wpa = $null
        } 
        $out = New-Object psobject -Property @{ 
            ComputerName = $DNSHostName
            Status = [string]::Empty
			Type = [string]::Empty
        }
        if ($wpa) {
            :outer foreach($item in $wpa) { 
                switch ($item.LicenseStatus) { 
                    0 {$out.Status = "Unlicensed"} 
                    1 {$out.Status = "Licensed"; break outer}
                    default {$out.Status = "Unknown value"}
                }
            }
			$out.Type = ($item.Description.Split(",")[1] -replace " channel").Trim() 
        } else {$out.Status = $status.Message} 
        $out
    }
}

Function Using-Culture (
[System.Globalization.CultureInfo]$culture = (throw "USAGE: Using-Culture -Culture culture -Script {scriptblock}"),
[ScriptBlock]$script= (throw "USAGE: Using-Culture -Culture culture -Script {scriptblock}"))
{
    $OldCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture
    trap 
    {
        [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture
    }
    [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture
    Invoke-Command $script
    [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture
}

$code={
$x= $y= $z = 2
$filter = "(&(objectclass=computer)(dnshostname=*)(!operatingsystem=Windows XP Professional)(name=w*)(!userAccountControl:1.2.840.113556.1.4.803:=2))"
$sr = [adsisearcher]$filter
$sr.SearchRoot = [ADSI]"LDAP://dc=domain,dc=com"
$pcs = ([adsisearcher]$filter).FindAll() | Foreach {
	$o = $_.Properties.Item("operatingsystem")
	$_.Properties.Item("dnshostname") 
$sr.FindAll()|%{$_.Properties.distinguishedname}| Get-ActivationStatus  | Select ComputerName,DistinguishedName,Status,Type,@{n="OperatingSystem";e={$o}} 
}
$xl = new-object -comobject excel.application
$xl.visible = $true 
$b = $xl.Workbooks.Add()
$c = $b.Worksheets.Item()
$c.Name = "Undefined 2015" 
$c.columns.item(1).columnWidth = 75 
$c.columns.item(2).columnWidth = 75 

$c.cells.item(1,1) = "Name"
$c.cells.item(1,2) = "Status"

$d = $b.Worksheets.Add() 
$d.Name = "No licenses 2015"
$d.columns.item(1).columnWidth = 75 
$d.columns.item(2).columnWidth = 75

$d.cells.item(1,1) = "Name"
$d.cells.item(1,2) = "OS" 

$e = $b.Worksheets.Add(1) 
$e.Name = "Licenses 2015" 
$e.columns.item(1).columnWidth = 75 
$e.columns.item(2).columnWidth = 75 
$e.columns.item(3).columnWidth = 75 

$e.cells.item(1,1) = "Name" 
$e.cells.item(1,2) = "License type" 
$e.cells.item(1,3) = "OS" 


foreach($pc in $pcs) 
{
	
	switch($pc.Status)
	{
		"Licensed" 		{ 
			$e.cells.item($x,1) = $pc.ComputerName 
			$e.cells.item($x,2) = $pc.Type
			$e.cells.item($x,3) = $pc.OperatingSystem
			$x++
		}
		"Unlicensed" 	{ 
			$d.cells.item($y,1) = $pc.ComputerName 
			$d.cells.item($y,2) = $pc.OperatingSystem 
			$y++
		}
		default { 
			$c.cells.item($z,1) = $pc.ComputerName 
			$c.cells.item($z,2) = $pc.Status 
			$z++
		}
	}
}

$b.saveas("d:\License.xls") 
}
Using-Culture en-us $code
[gc]::collect()
[gc]::WaitForPendingFinalizers()

I'm can get DN's of PC, but I cann't figure out how to use this data for put in Excel's cells (all data on 1 sheet). Data are very... awful.



June 24th, 2015 10:08am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics