Get user Hash key over mail using Power shell script

Mahesh Jaiswal 86 Reputation points
2021-10-08T11:34:53.687+00:00

Dear All,

We are planning to collect user systems hash key details for which i am using below steps.

1) Created One folder in C drive with name "Autopilot"
2) Under this folder i have save batch script, when user clicks on this batch file it's generating the hash key for user in csv format in same folder

below is the command i have given to the .bat file

"PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command C:\Autopilot\win.ps1 -ComputerName $env:computername -OutputFile C:\Autopilot\computers.csv -append"

Now my requirement is that when ever user run this command it will generate the hash keys for user system and send it to me over mail.

Requesting you to please help us/let me know how can I achieve this using power shell script.

power shell script:

<#PSScriptInfo  

.VERSION 1.3  

.GUID ebf446a3-3362-4774-83c0-b7299410b63f  

.AUTHOR Michael Niehaus  

.COMPANYNAME Microsoft  

.COPYRIGHT  

.TAGS Windows AutoPilot  

.LICENSEURI  

.PROJECTURI  

.ICONURI  

.EXTERNALMODULEDEPENDENCIES  

.REQUIREDSCRIPTS  

.EXTERNALSCRIPTDEPENDENCIES  

.RELEASENOTES  
Version 1.0: Original published version.  
Version 1.1: Added -Append switch.  
Version 1.2: Added -Credential switch.  
Version 1.3: Added -Partner switch.  

#>  

<#  
.SYNOPSIS  
Retrieves the Windows AutoPilot deployment details from one or more computers  
.DESCRIPTION  
This script uses WMI to retrieve properties needed by the Microsoft Store for Business to support Windows AutoPilot deployment.  
.PARAMETER Name  
The names of the computers. These can be provided via the pipeline (property name Name or one of the available aliases, DNSHostName, ComputerName, and Computer).  
.PARAMETER OutputFile  
The name of the CSV file to be created with the details for the computers. If not specified, the details will be returned to the PowerShell  
pipeline.  
.PARAMETER Append  
Switch to specify that new computer details should be appended to the specified output file, instead of overwriting the existing file.  
.PARAMETER Credential  
Credentials that should be used when connecting to a remote computer (not supported when gathering details from the local computer).  
.PARAMETER Partner  
Switch to specify that the created CSV file should use the schema for Partner Center (using serial number, make, and model).  
.EXAMPLE  
.\Get-WindowsAutoPilotInfo.ps1 -ComputerName MYCOMPUTER -OutputFile .\MyComputer.csv  
.EXAMPLE  
.\Get-WindowsAutoPilotInfo.ps1 -ComputerName MYCOMPUTER -OutputFile .\MyComputer.csv -Append  
.EXAMPLE  
.\Get-WindowsAutoPilotInfo.ps1 -ComputerName MYCOMPUTER1,MYCOMPUTER2 -OutputFile .\MyComputers.csv  
.EXAMPLE  
Get-ADComputer -Filter * | .\GetWindowsAutoPilotInfo.ps1 -OutputFile .\MyComputers.csv  
.EXAMPLE  
Get-CMCollectionMember -CollectionName "All Systems" | .\GetWindowsAutoPilotInfo.ps1 -OutputFile .\MyComputers.csv  
.EXAMPLE  
.\Get-WindowsAutoPilotInfo.ps1 -ComputerName MYCOMPUTER1,MYCOMPUTER2 -OutputFile .\MyComputers.csv -Partner  

#>  

[CmdletBinding()]  
param(  
    [Parameter(Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True,Position=0)][alias("DNSHostName","ComputerName","Computer")] [String[]] $Name = @($env:ComputerName),  
    [Parameter(Mandatory=$False)] [String] $OutputFile = "",   
    [Parameter(Mandatory=$False)] [Switch] $Append = $false,  
    [Parameter(Mandatory=$False)] [System.Management.Automation.PSCredential] $Credential = $null,  
    [Parameter(Mandatory=$False)] [Switch] $Partner = $false,  
    [Parameter(Mandatory=$False)] [Switch] $Force = $false  
)  

Begin  
{  
    # Initialize empty list  
    $computers = @()  
}  

Process  
{  
    foreach ($comp in $Name)  
    {  
        $bad = $false  

        # Get the common properties.  
        Write-Verbose "Checking $comp"  
        $serial = (Get-WmiObject -ComputerName $comp -Credential $Credential -Class Win32_BIOS).SerialNumber  

        # Get the hash (if available)  
        $devDetail = (Get-WMIObject -ComputerName $comp -Credential $Credential -Namespace root/cimv2/mdm/dmmap -Class MDM_DevDetail_Ext01 -Filter "InstanceID='Ext' AND ParentID='./DevDetail'")  
        if ($devDetail -and (-not $Force))  
        {  
            $hash = $devDetail.DeviceHardwareData  
        }  
        else  
        {  
            $bad = $true  
            $hash = ""  
        }  

        # If the hash isn't available, get the make and model  
        if ($bad -or $Force)  
        {  
            $cs = Get-WmiObject -ComputerName $comp -Credential $Credential -Class Win32_ComputerSystem  
            $make = $cs.Manufacturer.Trim()  
            $model = $cs.Model.Trim()  
            if ($Partner)  
            {  
                $bad = $false  
            }  
        }  
        else  
        {  
            $make = ""  
            $model = ""  
        }  

        # Getting the PKID is generally problematic for anyone other than OEMs, so let's skip it here  
        $product = ""  

        # Depending on the format requested, create the necessary object  
        if ($Partner)  
        {  
            # Create a pipeline object  
            $c = New-Object psobject -Property @{  
                "Device Serial Number" = $serial  
                "Windows Product ID" = $product  
                "Hardware Hash" = $hash  
                "Manufacturer name" = $make  
                "Device model" = $model  
            }  
            # From spec:  
            # "Manufacturer Name" = $make  
            # "Device Name" = $model  

        }  
        else  
        {  
            # Create a pipeline object  
            $c = New-Object psobject -Property @{  
                "Device Serial Number" = $serial  
                "Windows Product ID" = $product  
                "Hardware Hash" = $hash  
            }  
        }  

        # Write the object to the pipeline or array  
        if ($bad)  
        {  
            # Report an error when the hash isn't available  
            Write-Error -Message "Unable to retrieve device hardware data (hash) from computer $comp" -Category DeviceError  
        }  
        elseif ($OutputFile -eq "")  
        {  
            $c  
        }  
        else  
        {  
            $computers += $c  
        }  

    }  
}  

End  
{  
    if ($OutputFile -ne "")  
    {  
        if ($Append)  
        {  
            if (Test-Path $OutputFile)  
            {  
                $computers += Import-CSV -Path $OutputFile  
            }  
        }  
        if ($Partner)  
        {  
            $computers | Select "Device Serial Number", "Windows Product ID", "Hardware Hash", "Manufacturer name", "Device model" | ConvertTo-CSV -NoTypeInformation | % {$_ -replace '"',''} | Out-File $OutputFile  
            # From spec:  
            # $computers | Select "Device Serial Number", "Windows Product ID", "Hardware Hash", "Manufacturer Name", "Device Name" | ConvertTo-CSV -NoTypeInformation | % {$_ -replace '"',''} | Out-File $OutputFile  
        }  
        else  
        {  
            $computers | Select "Device Serial Number", "Windows Product ID", "Hardware Hash" | ConvertTo-CSV -NoTypeInformation | % {$_ -replace '"',''} | Out-File $OutputFile  
        }  
    }  
}  

Thanks in Advanced .

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,604 questions
{count} votes

Accepted answer
  1. Rich Matheisen 47,501 Reputation points
    2021-10-09T14:39:39.377+00:00

    There's no need to modify the script. Doing what you want should be a simple as this:

    .\Get-WindowsAutoPilotInfo.ps1 -ComputerName MYCOMPUTER -OutputFile .\UserName.csv -Append
    $props = @{
        From = "sender"
        To = "IT Team"
        Subject = "Here's a file for you"
        Attachments = ".\UserName.csv"
        Body = "Here's more junk for you to read!"
        SmtpServer = "smtp.yourdomain.tld"
        Port = 587
        UseSSL = $true
        # Credential = "You may need to use this with port 587"
    }
    Send-MailMessage @props
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.