Wednesday, October 26, 2022

Powershell to kill a hung MMC on Windows

Situation: Hung MMC console is unable to be closed.

Use get-process to find the MMC process:

get-process -Name mmc -IncludeUserName

Then use "kill" command with ID number to stop-process or kill it.

Handles      WS(K)   CPU(s)     Id UserName               ProcessName                                                                                                    
-------      -----   ------     -- --------               -----------                                                                                                    
    543      41508    19.66  14048 ME                     mmc          
    
kill 14048

Friday, October 14, 2022

Powershell function to lookup a single computer by ComputerName

This is a short function to create a function called "Show-Computer".


#
# Show a single computer
#
function Show-Computer
{
    param (
        [Parameter(Mandatory)] $Computername
        )

    try{
        $test = Get-ADComputer -Identity $Computername -Properties CanonicalName, CN, Description, Created, IPv4Address, MemberOf, OperatingSystem, OperatingSystemVersion,LastLogontimestamp,LastLogonDate -ErrorAction Stop |
            Select CanonicalName, CN, Description, Created, Enabled, IPv4Address, MemberOf, Name, ObjectClass, OperatingSystem, OperatingSystemVersion, LastLogonDate, @{n="lastLogontimestamp";e={[datetime]::FromFileTime($_.lastLogontimestamp)}}
        if ($test) {
            Write-Output $test
        }
    } catch {
        Write-Output "Computer object $Computername does not exist in AD"
    }
}

Execute the powershell script to create the command, then you should be able to use it in powershell like this:


C:\windows\system32> Show-Computer -Computername XZY

Hope you find that useful.

Powershell function to look up an Active Directory user with SamAccountName

This is a short function to create a function called "Show-User".

#
# Show a single user
#
function Show-User
{
    param (
        [Parameter(Mandatory)] $Username
        )

    try{
        $test = Get-ADUser -Identity $Username -Prop CN,description,LastLogon,whenCreated,LastLogontimestamp,LastLogonDate,SamAccountName, userPrincipalName,SmartcardLogonRequired,PasswordNeverExpires, targetAddress, EmailAddress,homeDirectory, CanonicalName, adminDescription, msDS-cloudExtensionAttribute1 -ErrorAction Stop | 
            Select CN, SamAccountName, description, userPrincipalName, SmartcardLogonRequired, PasswordNeverExpires, whenCreated, adminDescription, LastLogonDate, Employee-ID, @{n="lastLogontimestamp";e={[datetime]::FromFileTime($_.lastLogontimestamp)}},  @{n="lastLogon";e={[datetime]::FromFileTime($_.LastLogon)}},AccountExpirationDate, targetAddress, EmailAddress,homeDirectory, CanonicalName
        if($test) {
            Write-Output $test
        }
    } catch {
        Write-Output "User object $Username does not exist in AD"
    }
}

Execute the powershell script to create the command, then you should be able to use it in powershell like this:

C:\windows\system32> Show-User -Username john.doe

Hope you find that useful.

Powershell to copy files from one file server share to another file server share

Instead of using Windows File Explorer to copy large files/folders from one \\fileserver\share to another, it is problematic when you continually get prompted to overwrite files or other errors cause the process to stop until user responds to the prompt.  I found that using powershell works better.

Copy-Item -Path "\\Fileserver01\share$\john.doe" -Destination "\\Fileserver02\share$\" -Recurse -Force -WhatIf

# Remove -WhatIf flag to actually do it.
# -Recurse copies files and subfolders.
# -Force copies/overwrites files even if destination is readonly.

Hope you find that useful.