Getting DN's of PCs instead of their CN's 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") 
$_.Properties.Item("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()



  • Edited by Net Ranger Wednesday, June 24, 2015 1:13 PM
June 24th, 2015 12:33pm

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.



Free Windows Admin Tool Kit Click here and download it now
June 24th, 2015 1:56pm

Hi,

As you mentioned that you just got one sheet on the excel, so I examed your cmdlets to create the excel.

It turns out that there are 2 mistakes. The first one is $c = $b.Worksheets.Item(), between parentheses should has a input as 1. And the second one is $e = $b.Worksheets.Add(1) ,you should use $e = $b.Worksheets.Add() instead. Hereby I attached the correct cmdlets to create the excel file. Please have a check.

$xl = new-object -comobject excel.application
$xl.visible = $true 
$b = $xl.Workbooks.Add()
$c = $b.Worksheets.Item(1)
$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() 
$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"

Best Regards,

Elaine

July 1st, 2015 4:59am

Thanks for the answer, I did it just after few hours, as posted my question here. It's really helps. However it's didn't solved my actually problem.
Free Windows Admin Tool Kit Click here and download it now
July 1st, 2015 8:22am

Hi

What the actually problem is? Did you examed each function you defined and did you check if you are able to get the correct data returns from the pcs?

You can test them one by one and then you can fine which line might have error and then you can narrow down the issue.

Best Regards,

Elaine

July 2nd, 2015 5:55am

Hi,

allow me to say that the original issue was solved with my first reply, the question was about getting DN instead of CN, then other issues had been reported. I tried to have other threads opened for them but the poster didn't want to.

If we keep changing subject here the thread will be confusing for other vis

Free Windows Admin Tool Kit Click here and download it now
July 2nd, 2015 6:00am

My actual problem as the name of this thread. Yes, i'm examed  each function I'm defined. And I'm debuged this code. I have figure out, that's my script returns like "Cann't find pc.domain.com in domain.com" when I changed Computername method to (get-adcomputer $dnshostname).distinguishedname and it's points on this.

ComputerName = $DNSHostName

to

ComputerName = (get-adcomputer $dnshostname).distinguishedname

July 2nd, 2015 6:32am

Open a new thread
Free Windows Admin Tool Kit Click here and download it now
July 2nd, 2015 6:40am

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

Other recent topics Other recent topics