PoshCode Archive  Artifact [4c866f4c71]

Artifact 4c866f4c7104e4b68de4bba38e0b29f3939093fea27609f5c9599e29139dd31b:

  • File Get-Computer.ps1 — part of check-in [e07ab49b56] at 2018-06-10 12:59:31 on branch trunk — The Get-Computer cmdlet retrieves basic information such as computer name, domain or workgroup name, and whether or not the computer is on a workgroup or a domain for the local computer without using WMI. It uses Netapi32.dll to get the domain/workgroup name and .Net for the computername. (user: Tome toenuff T size: 2812)

# encoding: ascii
# api: powershell
# title: Get-Computer
# description: The Get-Computer cmdlet retrieves basic information such as computer name, domain or workgroup name, and whether or not the computer is on a workgroup or a domain for the local computer without using WMI.  It uses Netapi32.dll to get the domain/workgroup name and .Net for the computername.
# version: 0.1
# type: function
# author: Tome toenuff T
# license: CC0
# function: Get-Computer
# x-poshcode-id: 1635
# x-archived: 2011-03-25T02:18:49
# x-published: 2011-02-13T14:15:00
#
#
function Get-Computer {
<#
  .Synopsis
    Retrieves basic information about a computer. 
   .Description
    The Get-Computer cmdlet retrieves basic information such as
    computer name, domain or workgroup name, and whether or not the computer
    is on a workgroup or a domain for the local computer.
   .Example
    Get-Computer
    Returns comptuer name, domain or workgroup name, and isDomainName
    .Example
    Get-Computer
    Returns comptuer name, domain or workgroup name, and isDomainName
      .OutPuts
    [PSObject]
   .Notes
    NAME:  Get-Computer
    AUTHOR: Tome Tanasovski
    LASTEDIT: 2/13/2010
    KEYWORDS:
   .Link
     Http://powertoe.wordpress.com
 #Requires -Version 2.0
 #>
$sig = @"
[DllImport("Netapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
public static extern int NetGetJoinInformation(string server,out IntPtr domain,out int status);
"@
    $type = Add-Type -MemberDefinition $sig -Name Win32Utils -Namespace NetGetJoinInformation -PassThru 
    $ptr = [IntPtr]::Zero
    $status = 0
    $type::NetGetJoinInformation($null, [ref] $ptr, [ref]$status) |Out-Null
    $returnobj = New-Object PSObject
    
    $workdomainname = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($ptr)
    
    $returnobj |Add-Member NoteProperty ComputerName ([System.Windows.Forms.SystemInformation]::Computername)

        switch ($status) {
        0 {
            Write-Warning "NetJoin status is type is Unknown.  Cannot determine whether or not this computer is on a domain or workgroup"
            $returnobj |Add-Member Noteproperty isDomain $null
        }
        1 {
            Write-Warning "NetJoin status is type is Unjoined.  Cannot determine whether or not this computer is on a domain or workgroup"
            $returnobj |Add-Member Noteproperty isDomain $null
        }
        2 {
            $returnobj |Add-Member Noteproperty isDomain $false
            $returnobj |Add-Member Noteproperty WorkgroupName $workdomainname
        }
        3 {
            $returnobj |Add-Member Noteproperty isDomain $true
            $returnobj |Add-Member Noteproperty DomainName $workdomainname
        }
    }

    return $returnobj
}